Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
465 stars 174 forks source link

Add timing based framing for RTU client and server #210

Closed martijnthe closed 5 years ago

martijnthe commented 6 years ago

The spec says:

In RTU mode, message frames are separated by a silent interval of at least 3.5 character times.

(see Modbus_over_serial_line_V1_02.pdf)

So, if data comes in, then a pause of longer than 3.5 character times and then more data, they should be considered to be separate frames. Right now this is not the case. I noticed this because I've got some noise on the lines and get stray bytes here and there. They end up getting reassembled with whatever comes next (of which parsing fails miserably...). I think the easiest way to implement this is to note the last time (incomplete) data came in. If new data comes, look at the last time. If it's over 3.5 character times, just ignore the data in the reassembly buffer.

stefanpoeter commented 6 years ago

I don't think setting a timestamp is a good Idea. I think we should improve the plausibility tests. With TCP we would simply close a client connection when there is no plausible data, here I would just throw away the first byte when the current buffer head didn't make sense and start again.

martijnthe commented 6 years ago

I would just throw away the first byte when the current buffer head didn't make sense and start again.

Wouldn't that preclude the ability to receive payloads in multiple chunks?

stefanpoeter commented 6 years ago

Have a look at tcp-server-request-handler.js, it is already handled there.