|
OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
|
Connect using a serial port. More...
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.
| |||||
| override async Task< int > | ReadAsync (byte[] buffer, CancellationToken token) | ||||
Read from connection.
| |||||
| 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.
| |||||
| Task | WriteAsync (byte[] buffer) | ||||
Write to connection.
| |||||
| 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. | |
Connect using a serial port.
Definition at line 11 of file SerialPortOsdpConnection.cs.
|
inline |
Initializes a new instance of the T:OSDP.Net.Connections.SerialPortOsdpConnection class.
| portName | Name of the port. |
| baudRate | The baud rate. |
| T:System.ArgumentNullException | portName |
Definition at line 33 of file SerialPortOsdpConnection.cs.
Referenced by EnumBaudRates().
|
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.
|
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.
| portName | Name of the port |
| rates | Optional 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 |
Definition at line 51 of file SerialPortOsdpConnection.cs.
References SerialPortOsdpConnection(), and StandardBaudRates.
|
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.
|
inline |
Read from connection.
| buffer | Array of bytes to read |
| token | Cancellation token to end reading of bytes |
Implements OSDP.Net.Connections.IOsdpConnection.
Definition at line 192 of file SerialPortOsdpConnection.cs.
|
inline |
Changes the baud rate of the connection, retuning the port in place if it is open.
| baudRate | The new baud rate. |
| ArgumentOutOfRangeException | The 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.
|
inline |
Definition at line 205 of file SerialPortOsdpConnection.cs.
|
inline |
Waits until buffered output has been handed to the hardware and had time to clear it.
| frameByteCount | Number of bytes most recently written, used to work out how long they occupy the line. |
| token | Token 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.
|
inline |
Write to connection.
| buffer | Array of bytes to write |
Implements OSDP.Net.Connections.IOsdpConnection.
Definition at line 178 of file SerialPortOsdpConnection.cs.
References DiscardBuffersBeforeWrite.
|
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().
|
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().