stephane / libmodbus

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

_modbus_rtu_check_integrity seems to fail? #21

Closed karlp closed 13 years ago

karlp commented 13 years ago

I'm having some issues with libmodbus, that as best I can tell, are problems coming from the rtu CRC check.

If I simply ignore the return codes, everything works just fine. in debug mode, I see the expected bytes and all the devices respond appropriately. However, the return code is always -1, and errno is always set to 9 Bad file descriptor.

Opening /dev/ttyUSB000 at 115200 bauds (N, 8, 1) [01][06][00][01][00][FA][58][49] Waiting for a confirmation...

<01><06><00><01><00><58><49> write failed: 9 Bad file descriptor [01][06][00][00][00][37][C8][1C] Waiting for a confirmation... <01><06><00><00><00><37><1C> write failed: 9 Bad file descriptor It all works though, which is annoying. I have the same behaviour with an AVR modbus slave, and also with a second libmodbus fake slave, connected via socat PTY,link=COM8 PTY,link=COM9 Using the calculators at http://www.lammertbies.nl/comm/info/crc-calculation.html I can manually enter the modbus frame, and see that for...<01><06><00><01><00><58><49>, the crc16 (modbus) is 0x4958. Which I _guess_ is ok, and matches <58><49> ? Any idea what I'm likely to be missing? libmodbus5 version 3.0.1-2 (debian squeeze/stable) sample code is at https://gist.github.com/1201476
karlp commented 13 years ago

User error. Two problems, masking each other :(

Firstly, if (rc = -1) { report failure } Found that as soon as I enabled -Wall Secondly, I was checking for success with == 0 for write_register, when it should have been checking for != -1

Ugh, total user error.

stephane commented 13 years ago

I've just found this error in your gist (!= 0) but you've already found it.