/// <summary> /// Reads a number of bytes from the input buffer and writes those bytes into a byte array at the specified offset. /// </summary> /// <param name="buffer">The byte array to write the input to.</param> /// <param name="offset">The offset in the buffer array to begin writing.</param> /// <param name="count">The number of bytes to read.</param> /// <exception cref="System.InvalidOperationException">Port is not open.</exception> /// <exception cref="System.ArgumentNullException">The buffer passed is null.</exception> /// <exception cref="System.ArgumentOutOfRangeException">The offset or count is outside of valid buffer region.</exception> /// <exception cref="System.TimeoutException">The operation did not complete before the time-out period ended.</exception> /// <exception cref="System.IO.IOException">Communication error.</exception> public int Read(byte[] buffer, int offset, int count) { if (IsOpen == false) { throw new InvalidOperationException("Port is not open."); } if (buffer == null) { throw new ArgumentNullException("buffer", "The buffer passed is null."); } if ((offset < 0) || (count < 0) || (offset + count > buffer.Length)) { throw new ArgumentOutOfRangeException("offset", "The offset or count is outside of valid buffer region."); } if (IsRunningOnMono == false) { var nPtrRead = IntPtr.Zero; var overlapped = new NativeOverlapped(); var result = NativeMethods.ReadFile(Handle, buffer, count, ref nPtrRead, ref overlapped); if (result != true) { throw new IOException("Communication error.", new Win32Exception()); } var nRead = nPtrRead.ToInt32(); if (nRead < count) { throw new TimeoutException("The operation did not complete before the time-out period ended."); } return(nRead); } else { return(FrameworkSerialPort.Read(buffer, offset, count)); } }
/// <summary> /// Closes the port connection. /// </summary> public void Close() { if (IsOpen) { if (IsRunningOnMono == false) { Handle.Close(); } else { FrameworkSerialPort.Close(); } } }
/// <summary> /// Writes a specified number of bytes to the serial port using data from a buffer. /// </summary> /// <param name="buffer">The byte array that contains the data to write to the port.</param> /// <param name="offset">The zero-based byte offset in the buffer parameter at which to begin copying bytes to the port.</param> /// <param name="count">The number of bytes to write.</param> /// <exception cref="System.InvalidOperationException">Port is already open.</exception> /// <exception cref="System.ArgumentNullException">The buffer passed is null.</exception> /// <exception cref="System.ArgumentOutOfRangeException">The offset or count is outside of valid buffer region.</exception> /// <exception cref="System.TimeoutException">The operation did not complete before the time-out period ended.</exception> /// <exception cref="System.IO.IOException">Communication error.</exception> public int Write(byte[] buffer, int offset, int count) { if (IsOpen == false) { throw new InvalidOperationException("Port is not open."); } if (buffer == null) { throw new ArgumentNullException("buffer", "The buffer passed is null."); } if ((offset < 0) || (count < 0) || (offset + count > buffer.Length)) { throw new ArgumentOutOfRangeException("offset", "The offset or count is outside of valid buffer region."); } if (IsRunningOnMono == false) { if ((offset != 0) || (buffer.Length != count)) { byte[] newBuffer = new byte[count]; Buffer.BlockCopy(buffer, offset, newBuffer, 0, count); buffer = newBuffer; } var nPtrWritten = IntPtr.Zero; var overlapped = new NativeOverlapped(); var result = NativeMethods.WriteFile(Handle, buffer, count, ref nPtrWritten, ref overlapped); if (result != true) { throw new Win32Exception("Communication error.", new Win32Exception()); } var nWriten = nPtrWritten.ToInt32(); if (nWriten < count) { throw new TimeoutException("The operation did not complete before the time-out period ended."); } return(nWriten); } else { FrameworkSerialPort.Write(buffer, offset, count); return(count); } }
/// <summary> /// Discards data from the serial driver's transmit buffer. /// </summary> /// <exception cref="System.InvalidOperationException">Port is not open.</exception> /// <exception cref="System.IO.IOException">Communication error.</exception> public void DiscardOutBuffer() { if (IsOpen == false) { throw new InvalidOperationException("Port is not open."); } if (IsRunningOnMono == false) { var result = NativeMethods.PurgeComm(Handle, NativeMethods.PURGE_TXABORT | NativeMethods.PURGE_TXCLEAR); if (result != true) { throw new IOException("Communication error.", new Win32Exception()); } } else { FrameworkSerialPort.DiscardOutBuffer(); } }