4using System.Threading.Tasks;
14 private readonly
string _portName;
15 private SerialPort _serialPort;
25 _portName = portName ??
throw new ArgumentNullException(nameof(portName));
29 public override Task
Open()
31 if (_serialPort ==
null)
33 _serialPort =
new SerialPort(_portName,
BaudRate)
37 StopBits = StopBits.One,
39 Handshake = Handshake.None
46 return Task.CompletedTask;
52 if (_serialPort !=
null)
55 _serialPort.Dispose();
60 return Task.CompletedTask;
69 throw new NotSupportedException(
70 "This is a read-only connection for passive monitoring. Write operations are not supported.");
74 public override async Task<int>
ReadAsync(
byte[] buffer, CancellationToken token)
76 if (_serialPort ==
null || !
IsOpen)
78 throw new InvalidOperationException(
"Connection is not open.");
81 var task = _serialPort.BaseStream.ReadAsync(buffer, 0, buffer.Length, token);
83 if (await Task.WhenAny(task, Task.Delay(-1, token)) == task)
85 return await task.ConfigureAwait(
false);
88 throw new TimeoutException();
94 return $
"{_portName} (Read-Only)";
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.
ReadOnlySerialPortOsdpConnection(string portName, int baudRate)
Initializes a new instance of the ReadOnlySerialPortOsdpConnection class.
override Task Open()
Open the connection for communications.
override string ToString()
override Task Close()
Close the connection for communications.
override Task WriteAsync(byte[] buffer)
Write to connection.
override async Task< int > ReadAsync(byte[] buffer, CancellationToken token)
Read from connection.Number of actual bytes read