Skip to main content

UartController Class

Namespace: GHIElectronics.TinyCLR.Devices.Uart · Assembly: GHIElectronics.TinyCLR.Devices.Uart

Represents a UART (serial) port. Configure via SetActiveSettings(UartSetting), then Enable the port and exchange bytes through Read(byte[]) / Write(byte[]). Subscribe to DataReceived for event-driven receive instead of polling BytesToRead.

public class UartController : IDisposable

Properties

Provider

public IUartControllerProvider Provider { get; }

The low-level provider backing this controller.

WriteBufferSize

public int WriteBufferSize { get; set; }

Size in bytes of the transmit buffer.

ReadBufferSize

public int ReadBufferSize { get; set; }

Size in bytes of the receive buffer.

BytesToWrite

public int BytesToWrite { get; }

Bytes currently queued for transmission.

BytesToRead

public int BytesToRead { get; }

Bytes currently available to read.

IsRequestToSendEnabled

public bool IsRequestToSendEnabled { get; set; }

Drives the RTS line when hardware handshaking is enabled.

ClearToSendState

public bool ClearToSendState { get; }

Current state of the CTS line.

Methods

GetDefault()

public static UartController GetDefault()

Returns the default UART controller for this device.

Returns UartController

FromName(string name)

public static UartController FromName(string name)

Returns a UART controller identified by its native API name.

ParameterTypeDescription
namestring

Returns UartController

FromProvider(IUartControllerProvider provider)

public static UartController FromProvider(IUartControllerProvider provider)

Creates a controller from a custom IUartControllerProvider.

ParameterTypeDescription
providerIUartControllerProvider

Returns UartController

Dispose()

public void Dispose()

Releases the underlying provider.

Enable()

public void Enable()

Powers on the port. Call after SetActiveSettings(UartSetting).

Disable()

public void Disable()

Powers off the port.

SetActiveSettings(UartSetting setting)

public void SetActiveSettings(UartSetting setting)

Applies a complete set of serial settings (baud, framing, handshake, polarity).

ParameterTypeDescription
settingUartSettingSettings to apply.

Flush()

public void Flush()

Blocks until all buffered TX bytes have been shifted out.

Read(byte[] buffer)

public int Read(byte[] buffer)

Reads up to buffer.Length bytes; returns the count actually read.

ParameterTypeDescription
bufferbyte[]

Returns int

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

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

Reads up to length bytes into buffer at offset.

ParameterTypeDescription
bufferbyte[]
offsetint
lengthint

Returns int — Number of bytes actually read (may be less than requested).

Write(byte[] buffer)

public int Write(byte[] buffer)

Writes buffer.Length bytes.

ParameterTypeDescription
bufferbyte[]

Returns int

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

public int Write(byte[] buffer, int offset, int length)

Writes length bytes from buffer at offset.

ParameterTypeDescription
bufferbyte[]
offsetint
lengthint

Returns int

Write(byte[] buffer, int offset, int length, TimeSpan breakDuration)

public int Write(byte[] buffer, int offset, int length, TimeSpan breakDuration)

Writes a slice and optionally follows it with a break condition. A non-zero breakDuration flushes the TX FIFO first.

ParameterTypeDescription
bufferbyte[]Source buffer.
offsetintStarting offset.
lengthintNumber of bytes to write.
breakDurationTimeSpanLength of the break condition to assert after the write; TimeSpan.Zero means no break.

Returns int — Number of bytes actually queued.

ClearWriteBuffer()

public void ClearWriteBuffer()

Empties the transmit buffer.

ClearReadBuffer()

public void ClearReadBuffer()

Empties the receive buffer.

Events

ClearToSendChanged

public event ClearToSendChangedEventHandler ClearToSendChanged

Raised when the CTS input changes state.

DataReceived

public event DataReceivedEventHandler DataReceived

Raised when receive data is available. DataReceivedEventArgs.Count gives the number of bytes that have just been buffered; call Read(byte[]) from the handler to consume them.

ErrorReceived

public event ErrorReceivedEventHandler ErrorReceived

Raised when the controller detects a frame, parity, or buffer error.