Skip to content

Commit 9820dbb

Browse files
committed
Fixed setting stream delay after stream change
1 parent cdd6256 commit 9820dbb

4 files changed

Lines changed: 31 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [2.3.1] - 2024-01-06
8+
### Fixed
9+
- Setting stream delay after stream change.
10+
711
## [2.3.0] - 2024-01-05
812
### Added
913
- Properties for communication settings.

PL.Modbus/Client.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.IO.Ports;
8+
using System.Net;
89

910
namespace PL.Modbus
1011
{
@@ -28,11 +29,9 @@ public class Client : IDisposable
2829
/// <param name="stationAddress">Server station address.</param>
2930
public Client(SerialPort serialPort, Protocol protocol = Protocol.Rtu, byte stationAddress = 1)
3031
{
31-
_stream = new SerialStream(serialPort);
3232
_protocol = protocol;
3333
_stationAddress = stationAddress;
34-
if (protocol == Protocol.Rtu)
35-
_stream.DelayBeforeWrite = serialPort.BaudRate < 19200 ? 28000 / serialPort.BaudRate : 2;
34+
Stream = new SerialStream(serialPort);
3635
}
3736

3837
/// <summary>
@@ -44,11 +43,9 @@ public Client(SerialPort serialPort, Protocol protocol = Protocol.Rtu, byte stat
4443
/// <param name="stationAddress">Server station address.</param>
4544
public Client(string ipAddress, ushort port = 502, Protocol protocol = Protocol.Tcp, byte stationAddress = 255)
4645
{
47-
_stream = new NetworkStream(ipAddress, port);
4846
_protocol = protocol;
4947
_stationAddress = stationAddress;
50-
if (protocol == Protocol.Rtu)
51-
_stream.DelayBeforeWrite = 2;
48+
Stream = new NetworkStream(ipAddress, port);
5249
}
5350

5451
/// <summary>
@@ -62,6 +59,14 @@ public Stream Stream
6259
lock (this)
6360
{
6461
_stream = value;
62+
63+
if (Protocol == Protocol.Rtu)
64+
{
65+
if (_stream is SerialStream serialStream)
66+
_stream.DelayBeforeWrite = serialStream.Port.BaudRate < 19200 ? 28000 / serialStream.Port.BaudRate : 2;
67+
else
68+
_stream.DelayBeforeWrite = 2;
69+
}
6570
}
6671
}
6772
}

PL.Modbus/PL.Modbus.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
<RepositoryUrl>https://github.com/plasmapper/modbus-dotnet</RepositoryUrl>
1212
<RepositoryType>git</RepositoryType>
1313
<PackageTags>modbus;client</PackageTags>
14-
<PackageReleaseNotes>Added
15-
- Properties for communication settings.</PackageReleaseNotes>
14+
<PackageReleaseNotes>Fixed
15+
- Setting stream delay after stream change.</PackageReleaseNotes>
1616
<PackageLicenseExpression>MIT</PackageLicenseExpression>
17-
<VersionPrefix>2.3.0</VersionPrefix>
17+
<VersionPrefix>2.3.1</VersionPrefix>
1818
</PropertyGroup>
1919

2020
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

PL.Modbus/SerialStream.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace PL.Modbus
99
/// </summary>
1010
public class SerialStream : Stream
1111
{
12-
private readonly SerialPort _serialPort;
13-
1412
/// <summary>
1513
/// Initializes a new instance of the SerialStream class.
1614
/// </summary>
17-
/// <param name="serialPort">Serial port.</param>
18-
public SerialStream(SerialPort serialPort) => _serialPort = serialPort;
15+
/// <param name="port">Serial port.</param>
16+
public SerialStream(SerialPort port) => Port = port;
17+
18+
public SerialPort Port { get; }
1919

2020
/// <summary>
2121
/// Reads data from a serial stream. Timeout is increased depending on the number of bytes to read and baud rate.
@@ -26,10 +26,10 @@ public override byte[] Read(int byteCount)
2626
{
2727
byte[] buffer = new byte[byteCount];
2828
int offset = 0;
29-
_serialPort.ReadTimeout = _serialPort.BaudRate == 0 ? ReadTimeout : ReadTimeout + byteCount * 11 * 1000 / _serialPort.BaudRate;
29+
Port.ReadTimeout = Port.BaudRate == 0 ? ReadTimeout : ReadTimeout + byteCount * 11 * 1000 / Port.BaudRate;
3030
while (byteCount > 0)
3131
{
32-
int bytesRead = _serialPort.Read(buffer, offset, byteCount);
32+
int bytesRead = Port.Read(buffer, offset, byteCount);
3333
offset += bytesRead;
3434
byteCount -= bytesRead;
3535
}
@@ -38,13 +38,13 @@ public override byte[] Read(int byteCount)
3838

3939
public override void ReadAvailableData()
4040
{
41-
if (_serialPort.BytesToRead == 0)
41+
if (Port.BytesToRead == 0)
4242
return;
43-
_serialPort.ReadTimeout = 1;
43+
Port.ReadTimeout = 1;
4444
try
4545
{
4646
while (true)
47-
_serialPort.ReadByte();
47+
Port.ReadByte();
4848
}
4949
catch { }
5050
}
@@ -54,18 +54,18 @@ public override void Write(byte[] buffer)
5454
ReadAvailableData();
5555
if (DelayBeforeWrite > 0)
5656
Thread.Sleep(DelayBeforeWrite);
57-
_serialPort.Write(buffer, 0, buffer.Length);
57+
Port.Write(buffer, 0, buffer.Length);
5858
}
5959

6060
public override void Open()
6161
{
62-
if (!_serialPort.IsOpen)
63-
_serialPort.Open();
62+
if (!Port.IsOpen)
63+
Port.Open();
6464
}
6565

6666
public override void Close() { }
6767

68-
public override IDisposable Lock() => new SerialPortLock(_serialPort);
68+
public override IDisposable Lock() => new SerialPortLock(Port);
6969

7070
private class SerialPortLock : IDisposable
7171
{

0 commit comments

Comments
 (0)