-
Notifications
You must be signed in to change notification settings - Fork 464
fix: robust Modbus Receive with desync detection and resync ability #2362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
- retry parsing with full buffered data when initial MBAP length is wrong - treat EOF as incomplete data and avoid dropping partial frames - keep discarding truly unparsable packets with diagnostic logging
fix: attempt to resynchronize the read stream if desynchronized
fix: io.EOF is a trap, we checked fragmentation above
|
Could you please add a bit more information on what the problem is, that you're trying to solve? Possibly this is also interesting for other languages. |
🤦♂️ yeah, I should have done that. I added details above. |
|
Geee ... Modbus seems to be the "Standard" with so many "mandatory" things, that nobody really seems to care about. I know in the PLC4J we have some code to try to recover from situations like this (However we have that in the SPI, no idea if we're using that in the Modbus driver however) ... if you want I can have a look after I return from the indusry frair I'm currently at (would be Thursday) .... if you're in a hurry ... I have no objections to merging this PR ... I'll just try and have a look if anything needs porting to other languages. |
|
I had considered if there was some way to have the mspec protocol generators add new protocol functions like The issue was I don't know nearly enough about mspec or the generators, and I only had this one live environment with Modbus to test against. I'm not in a rush if you want to hold on merging this for a better or more generalized method of handling this. |
In a rather large deployment that includes ~30 Global Control 5 iSMA-B-MIX38-IP gateways that each have 10-30 Conto D6 energy meters on the RTU bus behind them, and PLC4Go running on a Yocto device, we're seeing two issues:
.Receive()call to deal with. The old code obviously didn't like this.To deal with both of these, I've updated MessageCodec to try its best to detect these scenarios where the stream becomes desynchronized and to burn bytes in an attempt to resynchronize the stream.
One related additional issue, which I'll submit a fix for in a separate PR:
(EDIT: looks like @sruehl beat me to this next one)
If you look through all my changes (sorry, please squash), I started with just dealing with the CRC16 issue by calculating CRC16 and then peeking the next two bytes... but then ran into the second issue and realized the desync/sync should be more generic. That said, the next PR will catch the padding leak issue when the connection is sitting in cache, but it won't help with padding leaks if a lessee is holding the connection lease but making requests spaced enough that a keep-alive sneaks in.