stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.5k stars 1.77k forks source link

modbus_receive_confirmation() and unsupported requests. #493

Open alextingle opened 5 years ago

alextingle commented 5 years ago

I want to write a client that uses the "Diagnostic" function (FC 0x08). modbus_receive_confirmation() always stops reading data before the end of the response.

libmodbus doesn't support FC8, so I'm constructing the request myself. modbus_send_raw_request() sends the request message correctly, but modbus_receive_confirmation() always reports bad CRC. That's because it stops reading before the end of the response.

The function's documentation says "This function can be used to receive request not handled by the library." But that doesn't seem to be true, because _modbus_receive_msg() doesn't read the whole response, rather it uses compute_meta_length_after_function() and compute_data_length_after_meta() to compute how long it thinks the message ought to be. Obviously that's always going to be wrong when the FC is unknown to the library!

At the very least, you need to fix the documentation!

Given your design, I'm not sure what the cleanest way to add a receiver for modbus_send_raw_request() would be. Perhaps a whole new function that simply reads the input until the inter-message timeout, and does nothing but check CRC and return it?

If you can suggest an approach, I'll have a go at implementing it.

karlp commented 5 years ago

Duplicate of https://github.com/stephane/libmodbus/issues/343 and the linked issues there.