Skip to main content

SerialPort Class

NuGet: GHIElectronics.TinyCLR.Devices.Uart
Assembly: System.IO.Ports
Namespace: System.IO.Ports

.NET-style serial port. Same surface as System.IO.Ports.SerialPort; internally routes through TinyCLR's GHIElectronics.TinyCLR.Devices.Uart.UartController.

public class SerialPort : IDisposable

Constructors

SerialPort()

public SerialPort() : this( )

Creates a port using "COM1".

SerialPort(string portName)

public SerialPort(string portName)

Creates a port for the given port name.

SerialPort(string portName, int baudRate)

public SerialPort(string portName, int baudRate) : this(portName)

Creates a port with the given name and baud rate.

SerialPort(string portName, int baudRate, Parity parity)

public SerialPort(string portName, int baudRate, Parity parity) : this(portName, baudRate)

Creates a port with the given name, baud rate, and parity.

SerialPort(string portName, int baudRate, Parity parity, int dataBits)

public SerialPort(string portName, int baudRate, Parity parity, int dataBits) : this(portName, baudRate, parity)

Creates a port with the given name, baud rate, parity, and data bits.

SerialPort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)

public SerialPort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits) : this(portName, baudRate, parity, dataBits)

Creates a port with the given name, baud rate, parity, data bits, and stop bits.

Properties

PortName

public string PortName { get; set; }

The port name, e.g. "COM1" or a TinyCLR UART API name.

BaudRate

public int BaudRate { get; set; }

The baud rate. Defaults to 9600.

Parity

public Parity Parity { get; set; }

The parity scheme. Defaults to none.

DataBits

public int DataBits { get; set; }

Bits per byte. Defaults to 8.

StopBits

public StopBits StopBits { get; set; }

Number of stop bits. Defaults to one.

Handshake

public Handshake Handshake { get; set; }

The flow-control method. Defaults to none.

ReadTimeout

public int ReadTimeout { get; set; }

Read timeout in milliseconds, or InfiniteTimeout.

WriteTimeout

public int WriteTimeout { get; set; }

Write timeout in milliseconds, or InfiniteTimeout.

ReadBufferSize

public int ReadBufferSize { get; set; }

Receive buffer size in bytes. Defaults to 256.

WriteBufferSize

public int WriteBufferSize { get; set; }

Transmit buffer size in bytes. Defaults to 256.

ReceivedBytesThreshold

public int ReceivedBytesThreshold { get; set; }

Bytes that must be buffered before the data-received event fires. Defaults to 1.

NewLine

public string NewLine { get; set; }

The line terminator used by ReadLine and WriteLine. Defaults to "\n".

Encoding

public Encoding Encoding { get; set; }

The text encoding used by the string read/write methods. Defaults to UTF-8.

IsOpen

public bool IsOpen { get; }

True if the port is open.

BytesToRead

public int BytesToRead { get; }

Number of bytes available to read.

BytesToWrite

public int BytesToWrite { get; }

Number of bytes waiting to be sent.

CtsHolding

public bool CtsHolding { get; }

State of the Clear-to-Send line.

CDHolding

public bool CDHolding { get; }

State of the Carrier-Detect line. Not supported.

DsrHolding

public bool DsrHolding { get; }

State of the Data-Set-Ready line. Not supported.

BreakState

public bool BreakState { get; set; }

Whether the port is in a break state. Not supported.

DtrEnable

public bool DtrEnable { get; set; }

Whether the Data-Terminal-Ready line is enabled. Not supported.

RtsEnable

public bool RtsEnable { get; set; }

Whether the Request-to-Send line is enabled.

Methods

Open()

public void Open()

Opens the port using the current settings.

Close()

public void Close()

Closes the port.

Dispose()

public void Dispose()

Closes the port and releases its resources.

Read(byte[] buffer, int offset, int count)

public int Read(byte[] buffer, int offset, int count)

Reads up to count bytes into the buffer. Returns the number of bytes read.

Returns int

ReadByte()

public int ReadByte()

Reads a single byte. Throws on timeout.

Returns int

ReadChar()

public int ReadChar()

Reads a single character. Throws on timeout.

Returns int

Read(char[] buffer, int offset, int count)

public int Read(char[] buffer, int offset, int count)

Reads characters into the buffer. Not supported.

Returns int

ReadExisting()

public string ReadExisting()

Reads all bytes currently available and returns them as a string.

Returns string

ReadLine()

public string ReadLine()

Reads up to the NewLine terminator and returns the line.

Returns string

ReadTo(string value)

public string ReadTo(string value)

Reads up to the given delimiter and returns the text before it.

Returns string

Write(string text)

public void Write(string text)

Writes a string using the current encoding.

WriteLine(string text)

public void WriteLine(string text)

Writes a string followed by the NewLine terminator.

Write(byte[] buffer, int offset, int count)

public void Write(byte[] buffer, int offset, int count)

Writes count bytes from the buffer.

Write(char[] buffer, int offset, int count)

public void Write(char[] buffer, int offset, int count)

Writes count characters from the buffer using the current encoding.

DiscardInBuffer()

public void DiscardInBuffer()

Discards the contents of the receive buffer.

DiscardOutBuffer()

public void DiscardOutBuffer()

Discards the contents of the transmit buffer.

GetPortNames()

public static string[] GetPortNames()

Returns the available port names. Not supported.

Returns string[]

Events

DataReceived

public event SerialDataReceivedEventHandler DataReceived

Raised when data is received.

ErrorReceived

public event SerialErrorReceivedEventHandler ErrorReceived

Raised when a receive error occurs.

PinChanged

public event SerialPinChangedEventHandler PinChanged

Raised when a control pin changes.

Fields

InfiniteTimeout

public const int InfiniteTimeout

Value meaning "no timeout" for the timeout properties.