Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollo3zehn committed Nov 17, 2022
2 parents 2485395 + 24317f5 commit 40b6a56
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
## v5.0.2 - 2022-11-14

### Bugs Fixed

- The Modbus RTU client did not correctly detect response frames (thanks @zhangchaoza, fixes https://github.com/Apollo3zehn/FluentModbus/issues/83)

## v5.0.1 - 2022-11-14

### Bugs Fixed

- The Modbus server did not correctly detect request frames (thanks @jmsqlr, https://github.com/Apollo3zehn/FluentModbus/pull/75#issuecomment-1304653670)
- The Modbus RTU server did not correctly detect request frames (thanks @jmsqlr, https://github.com/Apollo3zehn/FluentModbus/pull/75#issuecomment-1304653670)

## v5.0.0 - 2022-09-08

Expand Down
56 changes: 52 additions & 4 deletions src/FluentModbus/ModbusUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public static bool DetectRequestFrame(byte unitIdentifier, Memory<byte> frame)

public static bool DetectResponseFrame(byte unitIdentifier, Memory<byte> frame)
{
/* Correct response frame (min. 6 bytes)
//

/* Response frame for read methods (0x01, 0x02, 0x03, 0x04, 0x17) (min. 6 bytes)
* 00 Unit Identifier
* 01 Function Code
* 02 Byte count
Expand All @@ -120,6 +122,17 @@ public static bool DetectResponseFrame(byte unitIdentifier, Memory<byte> frame)
* 05 CRC Byte 2
*/

/* Response frame for write methods (0x05, 0x06, 0x0F, 0x10) (8 bytes)
* 00 Unit Identifier
* 01 Function Code
* 02 Address
* 03 Address
* 04 Value
* 05 Value
* 06 CRC Byte 1
* 07 CRC Byte 2
*/

/* Error response frame (5 bytes)
* 00 Unit Identifier
* 01 Function Code + 0x80
Expand All @@ -130,10 +143,12 @@ public static bool DetectResponseFrame(byte unitIdentifier, Memory<byte> frame)

var span = frame.Span;

// absolute minimum frame size
if (span.Length < 5)
return false;

if (unitIdentifier != 255) // 255 means "skip unit identifier check"
// 255 means "skip unit identifier check"
if (unitIdentifier != 255)
{
var newUnitIdentifier = span[0];

Expand All @@ -142,8 +157,41 @@ public static bool DetectResponseFrame(byte unitIdentifier, Memory<byte> frame)
}

// Byte count check
if (span[1] < 0x80 && span.Length < span[2] + 5)
return false;
if (span[1] < 0x80)
{
switch (span[1])
{
// Read methods
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x17:

if (span.Length < span[2] + 5)
return false;

break;

// Write methods
case 0x05:
case 0x06:
case 0x0F:
case 0x10:

if (span.Length < 8)
return false;

break;
}
}

// Error (only for completeness, length >= 5 has already been checked above)
else
{
if (span.Length < 5)
return false;
}

// CRC check
var crcBytes = span.Slice(span.Length - 2, 2);
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "5.0.1",
"version": "5.0.2",
"suffix": ""
}

0 comments on commit 40b6a56

Please sign in to comment.