OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
OSDP.Net.Connections.SerialPortOsdpConnection Class Reference

Connect using a serial port. More...

Inheritance diagram for OSDP.Net.Connections.SerialPortOsdpConnection:
OSDP.Net.Connections.OsdpConnection OSDP.Net.Connections.IRetunableOsdpConnection OSDP.Net.Connections.IOsdpConnection OSDP.Net.Connections.IOsdpConnection

Public Member Functions

 SerialPortOsdpConnection (string portName, int baudRate)
 Initializes a new instance of the T:OSDP.Net.Connections.SerialPortOsdpConnection class.
override async Task Open ()
 Open the connection for communications.
void SetBaudRate (int baudRate)
 Changes the baud rate of the connection, retuning the port in place if it is open.
async Task WaitForTransmitCompleteAsync (int frameByteCount, CancellationToken token=default)
 Waits until buffered output has been handed to the hardware and had time to clear it.
override Task Close ()
 Close the connection for communications.
override async Task WriteAsync (byte[] buffer)
 Write to connection.
Parameters
bufferArray of bytes to write

override async Task< int > ReadAsync (byte[] buffer, CancellationToken token)
 Read from connection.
Parameters
bufferArray of bytes to read
tokenCancellation token to end reading of bytes
Returns
Number of actual bytes read

override string ToString ()
Public Member Functions inherited from OSDP.Net.Connections.OsdpConnection
Task Close ()
 Close the connection for communications.
Task Open ()
 Open the connection for communications.
Task< int > ReadAsync (byte[] buffer, CancellationToken token)
 Read from connection.
Parameters
bufferArray of bytes to read
tokenCancellation token to end reading of bytes
Returns
Number of actual bytes read

Task WriteAsync (byte[] buffer)
 Write to connection.
Parameters
bufferArray of bytes to write

void Dispose ()

Static Public Member Functions

static IEnumerable< SerialPortOsdpConnection > EnumBaudRates (string portName, int[] rates=null)
 A helper method that returns a lazily instantiated set of SerialPortOsdpConnection instances, each one configured for a different baud rate.

Static Public Attributes

static readonly int[] StandardBaudRates = [9600, 19200, 38400, 57600, 115200, 230400, 460800]
 Baud rates to try when searching for a device: 9600, 19200, 38400, 57600, 115200, 230400, 460800.

Properties

bool DiscardBuffersBeforeWrite = true [get, set]
 Gets or sets a value indicating whether the receive and transmit buffers are discarded before every write.
Properties inherited from OSDP.Net.Connections.OsdpConnection
int BaudRate [get, protected set]
 Speed of the connection.
virtual bool IsOpen [get, protected set]
 Is the connection open and ready to communicate.
TimeSpan ReplyTimeout = TimeSpan.FromMilliseconds(200) [get, set]
 Timeout value waiting for a reply.

Additional Inherited Members

Protected Member Functions inherited from OSDP.Net.Connections.OsdpConnection
 OsdpConnection (int baudRate)
 Creates an instance of OsdpConnection.
virtual void Dispose (bool disposing)
 Disposes the OsdpConnection instance.

Detailed Description

Connect using a serial port.

Definition at line 11 of file SerialPortOsdpConnection.cs.

Constructor & Destructor Documentation

◆ SerialPortOsdpConnection()

OSDP.Net.Connections.SerialPortOsdpConnection.SerialPortOsdpConnection ( string portName,
int baudRate )
inline

Initializes a new instance of the T:OSDP.Net.Connections.SerialPortOsdpConnection class.

Parameters
portNameName of the port.
baudRateThe baud rate.
Exceptions
T:System.ArgumentNullExceptionportName

Definition at line 33 of file SerialPortOsdpConnection.cs.

Referenced by EnumBaudRates().

Member Function Documentation

◆ Close()

override Task OSDP.Net.Connections.SerialPortOsdpConnection.Close ( )
inline

Close the connection for communications.

Implements OSDP.Net.Connections.IOsdpConnection.

Definition at line 168 of file SerialPortOsdpConnection.cs.

References OSDP.Net.Connections.OsdpConnection.IsOpen.

◆ EnumBaudRates()

IEnumerable< SerialPortOsdpConnection > OSDP.Net.Connections.SerialPortOsdpConnection.EnumBaudRates ( string portName,
int[] rates = null )
inlinestatic

A helper method that returns a lazily instantiated set of SerialPortOsdpConnection instances, each one configured for a different baud rate.

The primary use case for this method is in conjunction with ControlPanel.DiscoverDevice(IEnumerable<IOsdpConnection>, PanelCommands.DeviceDiscover.DiscoveryOptions) which expects a set of connections to test for a device.

Parameters
portNameName of the port
ratesOptional parameter identifying a set of baud rates to enumerate over. If not specified, the list from OSDP spec (9600, 19,200, 38,400, 57,600, 115,200, 230,400, 460,800) will be used by default
Returns
An enumerable that will lazily generate SerialPortOsdpConnection instances for a given set of baud rates (see description of "rates" parameter)

Definition at line 51 of file SerialPortOsdpConnection.cs.

References SerialPortOsdpConnection(), and StandardBaudRates.

◆ Open()

override async Task OSDP.Net.Connections.SerialPortOsdpConnection.Open ( )
inline

Open the connection for communications.

Windows can keep a serial port locked for a short time after it is closed, so opening it again straight away fails with UnauthorizedAccessException. That happens whenever a connection is stopped and restarted on the same port, which device discovery does for every baud rate it tries. The open is retried for up to a second before giving up, so a port held by another process still fails, just slightly later.

Implements OSDP.Net.Connections.IOsdpConnection.

Definition at line 76 of file SerialPortOsdpConnection.cs.

References OSDP.Net.Connections.OsdpConnection.BaudRate, and OSDP.Net.Connections.OsdpConnection.IsOpen.

◆ ReadAsync()

override async Task< int > OSDP.Net.Connections.SerialPortOsdpConnection.ReadAsync ( byte[] buffer,
CancellationToken token )
inline

Read from connection.

Parameters
bufferArray of bytes to read
tokenCancellation token to end reading of bytes
Returns
Number of actual bytes read

Implements OSDP.Net.Connections.IOsdpConnection.

Definition at line 192 of file SerialPortOsdpConnection.cs.

◆ SetBaudRate()

void OSDP.Net.Connections.SerialPortOsdpConnection.SetBaudRate ( int baudRate)
inline

Changes the baud rate of the connection, retuning the port in place if it is open.

Parameters
baudRateThe new baud rate.
Exceptions
ArgumentOutOfRangeExceptionThe baud rate is not positive.

Retuning in place avoids closing and reopening the port, which is slow, can leave the handle briefly unavailable on Windows, and toggles the control lines in a way that can disturb the bus. Callers changing rate mid-conversation should first let any pending transmission drain with WaitForTransmitCompleteAsync; changing the divisor while bytes are still in the shift register corrupts the tail of the message.

Implements OSDP.Net.Connections.IRetunableOsdpConnection.

Definition at line 117 of file SerialPortOsdpConnection.cs.

References OSDP.Net.Connections.OsdpConnection.BaudRate.

◆ ToString()

override string OSDP.Net.Connections.SerialPortOsdpConnection.ToString ( )
inline

Definition at line 205 of file SerialPortOsdpConnection.cs.

◆ WaitForTransmitCompleteAsync()

async Task OSDP.Net.Connections.SerialPortOsdpConnection.WaitForTransmitCompleteAsync ( int frameByteCount,
CancellationToken token = default )
inline

Waits until buffered output has been handed to the hardware and had time to clear it.

Parameters
frameByteCountNumber of bytes most recently written, used to work out how long they occupy the line.
tokenToken to observe while waiting.

Windows exposes no equivalent of tcdrain, so this does the best available approximation: wait for the driver's write buffer to empty, then wait out the transmission time of the frame to cover the UART FIFO and shift register that SerialPort.BytesToWrite cannot see.

This matters before SetBaudRate: returning from a write only means the bytes were accepted for transmission, not that they have left the wire.

Implements OSDP.Net.Connections.IRetunableOsdpConnection.

Definition at line 151 of file SerialPortOsdpConnection.cs.

References OSDP.Net.Connections.OsdpConnection.BaudRate.

◆ WriteAsync()

override async Task OSDP.Net.Connections.SerialPortOsdpConnection.WriteAsync ( byte[] buffer)
inline

Write to connection.

Parameters
bufferArray of bytes to write

Implements OSDP.Net.Connections.IOsdpConnection.

Definition at line 178 of file SerialPortOsdpConnection.cs.

References DiscardBuffersBeforeWrite.

Member Data Documentation

◆ StandardBaudRates

readonly int [] OSDP.Net.Connections.SerialPortOsdpConnection.StandardBaudRates = [9600, 19200, 38400, 57600, 115200, 230400, 460800]
static

Baud rates to try when searching for a device: 9600, 19200, 38400, 57600, 115200, 230400, 460800.

OSDP section 5.2 names only the first six; 460800 is a common extension rather than a rate the specification defines, and is included here because devices in the field use it.

Definition at line 27 of file SerialPortOsdpConnection.cs.

Referenced by EnumBaudRates().

Property Documentation

◆ DiscardBuffersBeforeWrite

bool OSDP.Net.Connections.SerialPortOsdpConnection.DiscardBuffersBeforeWrite = true
getset

Gets or sets a value indicating whether the receive and transmit buffers are discarded before every write.

Defaults to true.

Discarding suits an ACU, which drives a strict command/reply cycle and wants a clean slate before each command. It is harmful for a device that replies to an incoming stream, because the next command may already be arriving while the previous reply is being written, and discarding throws it away.

Implements OSDP.Net.Connections.IRetunableOsdpConnection.

Definition at line 66 of file SerialPortOsdpConnection.cs.

Referenced by WriteAsync().


The documentation for this class was generated from the following file: