eterey / pymodbus3

A full Modbus protocol written in Python 3.
http://uzumaxy.github.io/pymodbus3/
BSD 3-Clause "New" or "Revised" License
40 stars 11 forks source link

Fix for when pyserial is faster at reading the response than the server is supplying it. #5

Closed tomkcook closed 7 years ago

tomkcook commented 7 years ago

When using this library against a Modbus/RTU server, I have a problem that usually the client is faster processing the response than the server is supplying it. Because ModbusRtuFramer.header_size is set to 1, the block at transaction.py:111 only reads a single byte before switching to the state FramerState.ReadingContent. In the process, the call to self.framer.check_frame() at line 123 fails, as not enough data has been read to know how long the frame is (at least for frames that encode their own size within the frame).

To fix this, I've added a check while in the FramerState.ReadingContent state: If size == 0, indicating that the whole expected frame has been read, it calls self.framer.check_frame() again. This is because size could be zero because the whole frame has been read, or it could be zero because we don't yet know how big the frame should be; in this case, the subsequent recalculation of size at line 141 will either increase size substantially (in the case where we now know how long the frame should be) or by 1, in the case where we still don't know how long the frame should be (because in this case self.framer.get_frame_size() returns however much it's read so far + 1).

eterey commented 7 years ago

Thanks!