2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using NativeUart = Interop.Peripheral.Uart;
20 namespace Tizen.Peripheral.Uart
23 /// Enumeration for baud rate.
27 /// <summary>The number of signal in one second is 0.</summary>
30 /// <summary>The number of signal in one second is 50.</summary>
33 /// <summary>The number of signal in one second is 75.</summary>
36 /// <summary>The number of signal in one second is 110.</summary>
39 /// <summary>The number of signal in one second is 134.</summary>
42 /// <summary>The number of signal in one second is 150.</summary>
45 /// <summary>The number of signal in one second is 200.</summary>
48 /// <summary>The number of signal in one second is 300.</summary>
51 /// <summary>The number of signal in one second is 600.</summary>
54 /// <summary>The number of signal in one second is 1200.</summary>
57 /// <summary>The number of signal in one second is 1800.</summary>
60 /// <summary>The number of signal in one second is 2400.</summary>
63 /// <summary>The number of signal in one second is 4800.</summary>
66 /// <summary>The number of signal in one second is 9600.</summary>
69 /// <summary>The number of signal in one second is 19200.</summary>
72 /// <summary>The number of signal in one second is 38400.</summary>
75 /// <summary>The number of signal in one second is 57600.</summary>
78 /// <summary>The number of signal in one second is 115200.</summary>
81 /// <summary>The number of signal in one second is 230400.</summary>
86 /// Enumeration for byte size.
90 /// <summary>5 data bits.</summary>
93 /// <summary>6 data bits.</summary>
96 /// <summary>7 data bits.</summary>
99 /// <summary>8 data bits.</summary>
104 /// Enumeration for parity bit.
108 /// <summary>No parity is used.</summary>
111 /// <summary>Even parity is used.</summary>
114 /// <summary>Odd parity is used.</summary>
119 /// Enumeration for stop bits.
123 /// <summary>One stop bit.</summary>
126 /// <summary>Two stop bits.</summary>
131 /// Enumeration for hardware flow control.
133 public enum HardwareFlowControl
135 /// <summary>No hardware flow control.</summary>
138 /// <summary>Automatic RTS/CTS hardware flow control.</summary>
143 /// Enumeration for software flow control.
145 public enum SoftwareFlowControl
147 /// <summary>No software flow control.</summary>
150 /// <summary>XON/XOFF software flow control.</summary>
155 /// The class allows applications to communicate via UART platform's bus.
157 /// <privilege>http://tizen.org/privilege/peripheralio</privilege>
158 public class Uart : IDisposable
160 private BaudRate _baudRate;
161 private DataBits _dataBits;
162 private Parity _parity;
163 private StopBits _stopBits;
164 private HardwareFlowControl _hardwareFlowControl;
165 private SoftwareFlowControl _softwareFlowControl;
166 private bool _disposed = false;
169 /// Native handle to I2c.
171 private IntPtr _handle;
174 /// Opens the UART slave device.
176 /// <param name="port">The UART port number that the slave device is connected.</param>
177 public Uart(int port)
179 var ret = NativeUart.Open(port, out IntPtr handle);
180 if (ret != Internals.Errors.ErrorCode.None)
181 throw ExceptionFactory.CreateException(ret);
187 /// Closes the UART slave device.
195 /// Closes the UART slave device.
197 public void Close() => Dispose();
200 /// Disposes Uart object.
202 public void Dispose()
205 GC.SuppressFinalize(this);
209 /// Disposes Uart object.
211 protected virtual void Dispose(bool disposing)
218 NativeUart.Close(_handle);
223 /// Reads the bytes data from the UART slave device.
225 /// <param name="buffer">The output byte array.</param>
226 /// <param name="offset">The offset in buffer at which to write the bytes.</param>
227 /// <param name="count">The number of bytes to read.</param>
228 /// <returns>The number of bytes read.</returns>
229 public int Read(byte[] buffer, int offset, int count)
231 if (count > buffer.Length - offset)
232 throw new Exception("Can not read more bytes than the buffer can hold.");
233 byte[] tmpBuffer = new byte[count];
234 var length = Convert.ToUInt32(count);
235 var ret = NativeUart.Read(_handle, tmpBuffer, length);
236 if (ret < Internals.Errors.ErrorCode.None)
237 throw ExceptionFactory.CreateException(ret);
238 Array.Copy(tmpBuffer, 0, buffer, offset, (int)ret);
243 /// Writes the bytes data to the UART slave device.
245 /// <param name="buffer">The byte array that contains the data to write to the device.</param>
246 /// <param name="offset">The offset in buffer at which to begin copying bytes.</param>
247 /// <param name="count">The number of bytes to write</param>
248 public int Write(byte[] buffer, int offset, int count)
250 if (count > buffer.Length - offset)
251 throw new Exception("Can not write more bytes than the buffer holds.");
252 byte[] tmpBuffer = new byte[count];
253 Array.Copy(buffer, offset, tmpBuffer, 0, count);
254 var length = Convert.ToUInt32(count);
255 var ret = NativeUart.Write(_handle, tmpBuffer, length);
256 if (ret < Internals.Errors.ErrorCode.None)
257 throw ExceptionFactory.CreateException(ret);
262 /// Sets or gets baud rate of the UART slave device.
264 /// <remarks>Get value is initialized after successful Set call.</remarks>
265 public BaudRate BaudRate
270 var ret = NativeUart.SetBaudRate(_handle, (NativeUart.BaudRate)value);
271 if (ret != Internals.Errors.ErrorCode.None)
272 throw ExceptionFactory.CreateException(ret);
279 /// Sets or gets byte size of the UART slave device.
281 /// <remarks>Get value is initialized after successful Set call.</remarks>
282 public DataBits DataBits
287 var ret = NativeUart.SetByteSize(_handle, (NativeUart.ByteSize)value);
288 if (ret != Internals.Errors.ErrorCode.None)
289 throw ExceptionFactory.CreateException(ret);
296 /// Sets or gets parity bit of the UART slave device.
298 /// <remarks>Get value is initialized after successful Set call.</remarks>
304 var ret = NativeUart.SetParity(_handle, (NativeUart.Parity)value);
305 if (ret != Internals.Errors.ErrorCode.None)
306 throw ExceptionFactory.CreateException(ret);
313 /// Sets or gets stop bits of the UART slave device.
315 /// <remarks>Get value is initialized after successful Set call.</remarks>
316 public StopBits StopBits
321 var ret = NativeUart.SetStopBits(_handle, (NativeUart.StopBits)value);
322 if (ret != Internals.Errors.ErrorCode.None)
323 throw ExceptionFactory.CreateException(ret);
330 /// Sets or gets hardware flow control of the UART slave device.
332 /// <remarks>Get value is initialized after successful Set call.</remarks>
333 public HardwareFlowControl HardwareFlowControl
335 get => _hardwareFlowControl;
338 var ret = NativeUart.SetFlowControl(_handle, (NativeUart.SoftwareFlowControl)_softwareFlowControl, (NativeUart.HardwareFlowControl)value);
339 if (ret != Internals.Errors.ErrorCode.None)
340 throw ExceptionFactory.CreateException(ret);
342 _hardwareFlowControl = value;
347 /// Sets or gets software flow control of the UART slave device.
349 /// <remarks>Get value is initialized after successful Set call.</remarks>
350 public SoftwareFlowControl SoftwareFlowControl
352 get => _softwareFlowControl;
355 var ret = NativeUart.SetFlowControl(_handle, (NativeUart.SoftwareFlowControl)value, (NativeUart.HardwareFlowControl)_hardwareFlowControl);
356 if (ret != Internals.Errors.ErrorCode.None)
357 throw ExceptionFactory.CreateException(ret);
359 _softwareFlowControl = value;