-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSerialModbusStream.cs
More file actions
38 lines (31 loc) · 1.07 KB
/
SerialModbusStream.cs
File metadata and controls
38 lines (31 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using NModbus.Interfaces;
using System.IO.Ports;
namespace NModbus.Transport.Serial
{
public class SerialModbusStream : IModbusStream
{
private const string NewLine = "\r\n";
private readonly SerialPort serialPort;
public SerialModbusStream(SerialPort serialPort)
{
this.serialPort = serialPort;
this.serialPort.NewLine = NewLine;
}
public Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default)
{
var read = serialPort.Read(buffer, offset, count);
return Task.FromResult(read);
}
public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default)
{
serialPort.Write(buffer, offset, count);
return Task.CompletedTask;
}
public ValueTask DisposeAsync()
{
serialPort.Dispose();
GC.SuppressFinalize(this);
return default;
}
}
}