-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathSerialPortStreamAdapter.cs
More file actions
56 lines (46 loc) · 1.56 KB
/
SerialPortStreamAdapter.cs
File metadata and controls
56 lines (46 loc) · 1.56 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using NModbus.IO;
namespace NModbus.SerialPortStream
{
/// <summary>
/// An adapter for the SerialPortStream class. Useful for getting serial port access on non-Windows devices.
/// </summary>
public class SerialPortStreamAdapter : IStreamResource
{
private readonly RJCP.IO.Ports.SerialPortStream _serialPortStream;
public SerialPortStreamAdapter(RJCP.IO.Ports.SerialPortStream serialPortStream)
{
_serialPortStream = serialPortStream;
}
public int InfiniteTimeout => RJCP.IO.Ports.SerialPortStream.InfiniteTimeout;
public int ReadTimeout
{
get => _serialPortStream.ReadTimeout;
set => _serialPortStream.ReadTimeout = value;
}
public int WriteTimeout
{
get => _serialPortStream.WriteTimeout;
set => _serialPortStream.WriteTimeout = value;
}
public void DiscardInBuffer()
{
_serialPortStream.DiscardInBuffer();
}
public int Read(byte[] buffer, int offset, int count)
{
int result = _serialPortStream.Read(buffer, offset, count);
if (result == 0)
throw new TimeoutException();
return result;
}
public void Write(byte[] buffer, int offset, int count)
{
_serialPortStream.Write(buffer, offset, count);
}
public void Dispose()
{
_serialPortStream.Dispose();
}
}
}