CRC error on RTU response doesent return negative

Asked by Henrik Munktell

Hi (this is my first post hero on launchpad, hope I post in right place)

I have libmodbus 2.0.3 and some problems with CRC errors. I'm writing a modbus Master that communicate with RTU slaves over RS485.
When I issue a

"modbus_ret = read_holding_registers(&mb_param,id_slave,(start_addr-1),qty,tab_rp_registers);"

and the return has invalid CRC, libmodbus prompts as it should with

"ERROR Invalid crc received E800 - crc_calc 4716 (-16)" , for example.

But, the return value from read_holding_registers is 1, not -16, indicating a GOOD answer from slave and messing up my Masters registers. And yes, the modbus_ret is signed.

This must be a bug, right?

Regards
Henrik

Question information

Language:
English Edit question
Status:
Solved
For:
libmodbus Edit question
Assignee:
No assignee Edit question
Solved by:
Henrik Munktell
Solved:
Last query:
Last reply:
Revision history for this message
Stéphane Raimbault (sra) said :
#1

Yes, you're right!
I've created a real bug report from your question and I've fixed that on trunk version the return of check_crc16 was ignored but it's 0 for GOOD answer not 1 and I think it was already the case in 2.0.3 version.

Thank you very much for your bug report. Could you test the fix, please? I don't able have any RTU device.
I can send you a tarball or you can get the latest code with the command 'bzr branch lp:libmodbus'

The API has changed on trunk and I intend to release a new version shortly (2.2.0).
Do you need the fix for 2.0.X series?

Revision history for this message
Henrik Munktell (henrik-munktell) said :
#2

Many thanks.
I'm now trying to test the trunk version got by bzr. But I'm failing in the ./configure;make;make install process.
"bash: ./configure: No such file or directory"

Trying waf fails on the build step with:
henrik@aopen:~/libmodbus$ sudo ./waf build
[ 1/15] cc: src/modbus.c -> build/default/src/modbus_1.o
../src/modbus.c:31:20: error: config.h: No such file or directory
Build failed
 -> task failed (err #1):
 {task: cc modbus.c -> modbus_1.o}

What can I do wrong?

Then another thing, reading the MIGRATION document and I see that the slave adress is declared in the modbus_init_rtu/tcp. This makes no sense to me at first glance as the modbus PDU is formed with slave_adress, function, quantity, start adress and so forth. Extracting the slave adress to the init makes it some kind of fragmented in my opinion. However, I have not worked with TCP slaves and it maybe makes more sense in those situations.

Regards
Henrik

Revision history for this message
Yishin Li (yishin-li) said :
#3

Hello Henrik,

Please try ./autogen.sh; make

Yishin

Revision history for this message
Henrik Munktell (henrik-munktell) said :
#4

Doesent work, see output below

henrik@aopen:~/libmodbus$ sudo ./autogen.sh
[sudo] password for henrik:
autoreconf2.50: Entering directory `.'
autoreconf2.50: configure.ac: not using Gettext
autoreconf2.50: running: aclocal --force
/usr/share/aclocal/libmcrypt.m4:17: warning: underquoted definition of AM_PATH_LIBMCRYPT
  run info '(automake)Extending aclocal'
  or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
aclocal:configure.ac:9: warning: macro `AM_DISABLE_STATIC' not found in library
autoreconf2.50: configure.ac: tracing
autoreconf2.50: configure.ac: not using Libtool
autoreconf2.50: running: /usr/bin/autoconf --force
configure.ac:9: error: possibly undefined macro: AM_DISABLE_STATIC
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
configure.ac:15: error: possibly undefined macro: AC_PROG_LIBTOOL
autoreconf2.50: /usr/bin/autoconf failed with exit status: 1
henrik@aopen:~/libmodbus$ sudo make
make: *** No targets specified and no makefile found. Stop.

Revision history for this message
Yishin Li (yishin-li) said :
#5

Your autoconf version is 2.50, which is too old for libmodbus. You have to upgrade the autotools (autoconf, automake...) to 2.59 or above.
-yishin

Revision history for this message
Henrik Munktell (henrik-munktell) said :
#6

Upgraded autotools,autoconf and autoreconf to 2.61, with no sucess.

henrik@aopen:~/libmodbus$ autoconf -V
autoconf (GNU Autoconf) 2.61
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by David J. MacKenzie and Akim Demaille.
henrik@aopen:~/libmodbus$ ./autogen.sh
autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force
/usr/share/aclocal/libmcrypt.m4:17: warning: underquoted definition of AM_PATH_LIBMCRYPT
/usr/share/aclocal/libmcrypt.m4:17: run info '(automake)Extending aclocal'
/usr/share/aclocal/libmcrypt.m4:17: or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
configure.ac:9: warning: macro `AM_DISABLE_STATIC' not found in library
autoreconf: configure.ac: tracing
autoreconf: configure.ac: not using Libtool
autoreconf: running: /usr/bin/autoconf --force
configure.ac:9: error: possibly undefined macro: AM_DISABLE_STATIC
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
configure.ac:15: error: possibly undefined macro: AC_PROG_LIBTOOL
autoreconf: /usr/bin/autoconf failed with exit status: 1
henrik@aopen:~/libmodbus$

Revision history for this message
Stéphane Raimbault (sra) said :
#7

I am going to send you a tarball (after autogen.sh running) by mail.

Revision history for this message
Stéphane Raimbault (sra) said :
#8

Henrik,

You've raised an interesting comment about the slave adress (I post an email to collect feedback about this change on modbus list), is it annoying in your case (with RTU device and in with your application)?

The waf build is broken because it doesn't contain the config.h generation.

Revision history for this message
Henrik Munktell (henrik-munktell) said :
#9

I'm happy to tell that the tarball worked for me and that the CRC issue is gone in 2.2.0.

In my case the slave address is not a big issue due to that I have all modbus communication in just one place. But if you where to have modbus functions all over your program and forgetting to set the slave address just once, which will lead to problems. With the old 2.0.x you where forced to set the address en each call, minimizing the risk of addressing wrong slave.