2using System.Collections.Generic;
6using System.Threading.Tasks;
13 private static readonly TimeSpan OpenRetryTimeout = TimeSpan.FromSeconds(1);
14 private static readonly TimeSpan OpenRetryInterval = TimeSpan.FromMilliseconds(25);
16 private readonly
string _portName;
17 private SerialPort _serialPort;
27 public static readonly
int[]
StandardBaudRates = [9600, 19200, 38400, 57600, 115200, 230400, 460800];
35 _portName = portName ??
throw new ArgumentNullException(nameof(portName));
51 public static IEnumerable<SerialPortOsdpConnection>
EnumBaudRates(
string portName,
int[] rates =
null)
76 public override async Task
Open()
78 if (_serialPort !=
null)
return;
80 var deadline = DateTime.UtcNow + OpenRetryTimeout;
83 var serialPort =
new SerialPort(_portName,
BaudRate);
87 _serialPort = serialPort;
91 catch (UnauthorizedAccessException) when (DateTime.UtcNow < deadline)
101 await Task.Delay(OpenRetryInterval).ConfigureAwait(
false);
121 throw new ArgumentOutOfRangeException(nameof(baudRate),
"Baud rate must be greater than zero.");
126 var serialPort = _serialPort;
127 if (serialPort !=
null && serialPort.IsOpen)
129 serialPort.BaudRate = baudRate;
153 var serialPort = _serialPort;
154 if (serialPort ==
null || !serialPort.IsOpen)
return;
156 while (!token.IsCancellationRequested && serialPort.BytesToWrite > 0)
158 await Task.Delay(1, token).ConfigureAwait(
false);
161 if (frameByteCount <= 0)
return;
163 var wireTime = TimeSpan.FromSeconds(frameByteCount * 10.0 /
BaudRate);
164 await Task.Delay(wireTime, token).ConfigureAwait(
false);
170 _serialPort?.Close();
171 _serialPort?.Dispose();
174 return Task.CompletedTask;
184 _serialPort.DiscardInBuffer();
185 _serialPort.DiscardOutBuffer();
188 await _serialPort.BaseStream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(
false);
192 public override async Task<int>
ReadAsync(
byte[] buffer, CancellationToken token)
194 var task = _serialPort.BaseStream.ReadAsync(buffer, 0, buffer.Length, token);
196 if (await Task.WhenAny(task, Task.Delay(-1, token)) == task)
198 return await task.ConfigureAwait(
false);
201 throw new TimeoutException();
int BaudRate
Speed of the connection.
OsdpConnection(int baudRate)
Creates an instance of OsdpConnection.
virtual bool IsOpen
Is the connection open and ready to communicate.
SerialPortOsdpConnection(string portName, int baudRate)
Initializes a new instance of the T:OSDP.Net.Connections.SerialPortOsdpConnection class.
static IEnumerable< SerialPortOsdpConnection > EnumBaudRates(string portName, int[] rates=null)
A helper method that returns a lazily instantiated set of SerialPortOsdpConnection instances,...
bool DiscardBuffersBeforeWrite
Gets or sets a value indicating whether the receive and transmit buffers are discarded before every w...
override async Task< int > ReadAsync(byte[] buffer, CancellationToken token)
Read from connection.Number of actual bytes read
override Task Close()
Close the connection for communications.
override async Task Open()
Open the connection for communications.
override string ToString()
void SetBaudRate(int baudRate)
Changes the baud rate of the connection, retuning the port in place if it is open.
static readonly int[] StandardBaudRates
Baud rates to try when searching for a device: 9600, 19200, 38400, 57600, 115200, 230400,...
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 async Task WriteAsync(byte[] buffer)
Write to connection.
A connection whose line rate can be changed while it stays open.