Check byte count in DetectFrame method. - #75
Conversation
|
Good catch & thanks for the PR! Your solution makes sense. However, could you please extend your frame length check to make sure it is not a Modbus error frame? I think something like Here is the layout of both frame types: |
|
Thank you for reminding the error response case! |
|
@Apollo3zehn, after your approval in branch |
Check byte count in DetectFrame method.
|
@iberisoft The package is released now, also as v3.2.2. |
|
Best regards. |
|
@jmsqlr, it should be solved now. When I started developing FluentModbus, I was on Windows and had no easy way to test the Modbus RTU part automatically. Now I am on Linux and the project is compiled using GitHub Actions. So now we can maybe link two virtual COM ports to run Modbus RTU unit tests to avoid issues like the one you described. |

Hello @Apollo3zehn,
I have found an issue with the current logic of how the Modbus RTU frame is detected. This issue occurs under seldom circumstances, let me share an example.
We have a temperature sensor with unit identifier 1 providing data thru input register 0. The corresponding Modbus request frame is
01 04 00 00 00 01 31 CA. Response frames depend on the actual temperature getting as Celsius degrees multiplied by 100. However, there is a special value that we must focus on: 7.69 degrees. The Modbus response frame for value 769 is01 04 02 03 01 78 00.The
DetectFramemethod is called from the response pump. Our sensor returns a frame as entirely (7 bytes) as divided into segments (e.g. 6+1 bytes). The described issue occurs when theDetectFramemethod is called for the response frame mentioned in the previous paragraph without the last byte (6-byte incomplete segment).Theoretically, such trimmed frames should be identified in
DetectFramedue to relying on CRC. Unfortunately, the CRC-based check is not enough for this particular case. Look at the discussed response frame trimmed to01 04 02 03 01 78. The frame-detection code treats this frame as successfully detected because sequence01 04 02 03generates CRC0x7801. Obviously,01 04 02 03 01 78is incomplete by the standard because it indicates 2 bytes of data in the third byte but it has only one data byte.So I have added a couple of lines into the method to additionally check the byte count field declared by the Modbus RTU specification.