示例#1
0
        /// <summary>
        /// Opens the UART slave device.
        /// </summary>
        /// <param name="port">The UART port number that the slave device is connected.</param>
        public SerialPort(int port)
        {
            var ret = NativeUart.Open(port, out _handle);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
        }
示例#2
0
        /// <summary>
        /// Disposes Uart object.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            NativeUart.Close(_handle);
            _disposed = true;
        }
示例#3
0
        /// <summary>
        /// Writes the bytes data to the UART slave device.
        /// </summary>
        /// <param name="buffer">The byte array that contains the data to write to the device.</param>
        /// <param name="offset">The offset in buffer at which to begin copying bytes.</param>
        /// <param name="count">The number of bytes to write</param>
        public int Write(byte[] buffer, int offset, int count)
        {
            if (count > buffer.Length - offset)
            {
                throw new Exception("Can not write more bytes than the buffer holds.");
            }
            byte[] tmpBuffer = new byte[count];
            Array.Copy(buffer, offset, tmpBuffer, 0, count);
            var length = Convert.ToUInt32(count);
            var ret    = NativeUart.Write(_handle, tmpBuffer, length);

            if (ret < Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
            return((int)ret);
        }
示例#4
0
        /// <summary>
        /// Reads the bytes data from the UART slave device.
        /// </summary>
        /// <param name="buffer">The output byte array.</param>
        /// <param name="offset">The offset in buffer at which to write the bytes.</param>
        /// <param name="count">The number of bytes to read.</param>
        /// <returns>The number of bytes read.</returns>
        public int Read(byte[] buffer, int offset, int count)
        {
            if (count > buffer.Length - offset)
            {
                throw new Exception("Can not read more bytes than the buffer can hold.");
            }
            byte[] tmpBuffer = new byte[count];
            var    length    = Convert.ToUInt32(count);
            var    ret       = NativeUart.Read(_handle, tmpBuffer, length);

            if (ret < Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
            Array.Copy(tmpBuffer, 0, buffer, offset, (int)ret);
            return((int)ret);
        }