Skip to main content

Class SerialPortBase

Represents a port that is capable of serial (UART) communications. Preserved for legacy API compatibility. For a more modern approach, use SerialMessagePort.

Assembly: Meadow.dll
View Source
Declaration
public abstract class SerialPortBase : ISerialPort, IDisposable

Implements:
Meadow.Hardware.ISerialPort, System.IDisposable

Properties

PortName

Gets the port name used for communications.

View Source
Declaration
public string PortName { get; }

IsOpen

Gets a value indicating the open or closed status of the SerialPort object.

View Source
Declaration
public bool IsOpen { get; }

Parity

Gets or sets the parity-checking protocol.

View Source
Declaration
public Parity Parity { get; set; }

DataBits

Gets or sets the standard length of data bits per byte.

View Source
Declaration
public int DataBits { get; set; }

StopBits

Gets or sets the standard number of stop bits per byte.

View Source
Declaration
public StopBits StopBits { get; set; }

ReadTimeout

The time required for a time-out to occur when a read operation does not finish.

View Source
Declaration
public TimeSpan ReadTimeout { get; set; }

WriteTimeout

The time required for a time-out to occur when a write operation does not finish.

View Source
Declaration
public TimeSpan WriteTimeout { get; set; }

BytesToRead

Gets the number of bytes of data in the receive buffer.

View Source
Declaration
public int BytesToRead { get; }

BaudRate

Gets or sets the serial baud rate.

View Source
Declaration
public int BaudRate { get; set; }

ReceiveBufferSize

The buffer size, in bytes.

View Source
Declaration
public int ReceiveBufferSize { get; }

Fields

_readThread

Thread responsible for reading from the serial port.

View Source
Declaration
protected Thread? _readThread

_baudRate

The baud rate for the serial port.

View Source
Declaration
protected int _baudRate

_accessLock

Lock object for thread synchronization when accessing critical sections of code.

View Source
Declaration
protected object _accessLock

Methods

SetHardwarePortSettings(IntPtr)

Sets the hardware port settings for the specified handle. This method is intended to configure the hardware settings of the serial port.

View Source
Declaration
protected abstract void SetHardwarePortSettings(IntPtr handle)
Parameters
TypeNameDescription
System.IntPtrhandleThe handle to the hardware port.

OpenHardwarePort(string)

Override this method to open a hardware (OS) serial port

View Source
Declaration
protected abstract IntPtr OpenHardwarePort(string portName)
Returns

System.IntPtr: The resulting port handle

Parameters
TypeNameDescription
System.StringportNameThe name of the port

CloseHardwarePort(IntPtr)

Override this method to close a hardware (OS) serial port

View Source
Declaration
protected abstract void CloseHardwarePort(IntPtr handle)
Parameters
TypeNameDescription
System.IntPtrhandleThe port handle

WriteHardwarePort(IntPtr, byte[], int)

Override this method to write data to a hardware serial port

View Source
Declaration
protected abstract int WriteHardwarePort(IntPtr handle, byte[] writeBuffer, int count)
Returns

System.Int32: The number of bytes actually written

Parameters
TypeNameDescription
System.IntPtrhandleThe handle to the port
System.Byte[]writeBufferThe source data buffer
System.Int32countThe number of bytes to write

ReadHardwarePort(IntPtr, byte[], int)

Override this method to read data from a hardware serial port

View Source
Declaration
protected abstract int ReadHardwarePort(IntPtr handle, byte[] readBuffer, int count)
Returns

System.Int32: The actual number of bytes read

Parameters
TypeNameDescription
System.IntPtrhandleThe handle to the port
System.Byte[]readBufferThe buffer to write the data to
System.Int32countThe number of bytes to read

GetSupportedBaudRates()

Gets an array of supported baud rates

View Source
Declaration
public int[] GetSupportedBaudRates()
Returns

System.Int32[]

ToString()

Returns a string that represents the current Meadow.Hardware.SerialPortBase.

View Source
Declaration
public override string ToString()
Returns

System.String: A string that represents the current Meadow.Hardware.SerialPortBase.### Dispose() Releases the resources used by the Meadow.Hardware.SerialPortBase.

View Source
Declaration
public void Dispose()

ClearReceiveBuffer()

Discards data from the serial driver's receive buffer.

View Source
Declaration
public void ClearReceiveBuffer()

Open()

Opens a new serial port connection.

View Source
Declaration
public void Open()

Close()

Closes the port connection and sets the IsOpen property to false.

View Source
Declaration
public void Close()

Write(byte[])

Writes data to the serial port.

View Source
Declaration
public int Write(byte[] buffer)
Returns

System.Int32

Parameters
TypeName
System.Byte[]buffer

Write(byte[], int, int)

Writes a specified number of bytes to the serial port using data from a buffer.

View Source
Declaration
public int Write(byte[] buffer, int index, int count)
Returns

System.Int32

Parameters
TypeNameDescription
System.Byte[]bufferThe byte array that contains the data to write to the port.
System.Int32indexThe zero-based byte offset in the buffer parameter at which to begin copying bytes to the port.
System.Int32countThe number of bytes to write.

Peek()

Returns the next available by in the input buffer but does not consume it.

View Source
Declaration
public int Peek()
Returns

System.Int32: The byte, cast to an Int32, or -1 if there is no data available in the input buffer.### ReadByte() Synchronously reads one byte from the SerialPort input buffer.

View Source
Declaration
public int ReadByte()
Returns

System.Int32: The byte, cast to an Int32, or -1 if the end of the stream has been read.### ReadAll() Reads the entire serial port buffer into an array of bytes. Before calling, make sure that your buffer is large enough by checking BytesToRead property. If your buffer isn't large enough, this will leave bytes in the serial port buffer.

View Source
Declaration
public byte[] ReadAll()
Returns

System.Byte[]

Read(byte[], int, int)

Reads a number of bytes from the SerialPort input buffer and writes those bytes into a byte array at the specified offset.

View Source
Declaration
public int Read(byte[] buffer, int index, int count)
Returns

System.Int32: The number of bytes read.

Parameters
TypeNameDescription
System.Byte[]bufferThe byte array to write the input to.
System.Int32indexThe offset in buffer at which to write the bytes.
System.Int32countThe maximum number of bytes to read. Fewer bytes are read if count is greater than the number of bytes in the input buffer.
Exceptions

System.TimeoutException
No bytes were available to read.

Events

DataReceived

Indicates that data has been received through a port represented by the SerialPort object.

View Source
Declaration
public event SerialDataReceivedEventHandler DataReceived
Event Type

Meadow.Hardware.SerialDataReceivedEventHandler

BufferOverrun

Indicates that the internal data buffer has overrun and data has been lost.

View Source
Declaration
public event EventHandler BufferOverrun
Event Type

System.EventHandler

Implements