seg fault & invalid tid

200 views
Skip to first unread message

Orhan Özbek

unread,
Apr 13, 2017, 7:14:54 AM4/13/17
to libmodbus
Hello ,

I've been trying to read from registers on a linux machine and having some problems with it. Here is the response

[00][01][00][00][00][06][F7][03][00][00][00][05]
Waiting for a confirmation...
<80><01><00><00><00><06><F7><03><00>
Invalid TID received 0x8001 (not 0x1)
63 bytes flushed
Invalid data

below you can find the code:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <modbus.h>
int main(int argc, char *argv[])
{
modbus_t *ctx;
uint16_t tab_reg[64];
int rc;
int i;
ctx = modbus_new_tcp("192.168.1.11", 9100);
modbus_set_error_recovery(ctx, MODBUS_ERROR_RECOVERY_LINK |MODBUS_ERROR_RECOVERY_PROTOCOL);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
rc = modbus_set_slave(ctx,247);
if (rc == -1) {
    fprintf(stderr, "Invalid slave ID\n");
    modbus_free(ctx);
    return -1;
}
modbus_set_debug(ctx, 1);
//modbus_set_response_timeout(ctx, 0, 200000);
rc = modbus_read_registers(ctx,0,5, tab_reg);
if (rc == -1) {
fprintf(stderr, "%s\n", modbus_strerror(errno));
return -1;
}

for (i=0; i < rc; i++) {
printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
}

free(tab_reg);
modbus_close(ctx);
modbus_free(ctx);
return 0;
}
i think the problem is caused by response time but whenever i try to set it via modbus_set_response_timeout() i get a segmentation fault. I'm using  libmodbus-3.1.4

any possible way to solve this ?

thanks in advance

Stéphane Raimbault

unread,
Apr 15, 2017, 1:07:27 PM4/15/17
to libm...@googlegroups.com
Be sure to use the libmodbus 3.1.4 documentation, the API has changed.

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "libmodbus".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+unsubscribe@googlegroups.com.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

Orhan Özbek

unread,
Apr 19, 2017, 3:20:04 AM4/19/17
to libmodbus
Checked it, but could not find change you mentioned. Can you point me what is wrong with it ?

Thanks
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.

Stéphane Raimbault

unread,
Apr 19, 2017, 11:07:44 AM4/19/17
to libm...@googlegroups.com

Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+unsubscribe@googlegroups.com.

Stéphane Raimbault

unread,
Apr 19, 2017, 11:10:45 AM4/19/17
to libm...@googlegroups.com
Are you sure to use libmodbus 3.1.4?

Orhan Özbek

unread,
Apr 20, 2017, 2:23:08 AM4/20/17
to libmodbus
I read almost everything on your website&github i believe i'm using it correctly below you can see the output of version.

Compiled with libmodbus version 3.1.4 (03010400)
Linked with libmodbus version 3.1.4
The functions to read/write float values are available (2.1.0).
Oh gosh, brand new API (2.1.1)!

i compile using this command:
gcc test5.c -o test5 `pkg-config --libs --cflags libmodbus`

Orhan Özbek

unread,
Apr 20, 2017, 3:18:03 AM4/20/17
to libmodbus
i just tried to send a raw request and the output looks like below i hope it helps
Connecting to 192.168.1.11
[00][00][00][00][00][06][F7][03][04][4B][00][05]
Waiting for a confirmation...
<80><00><00><00><00><06><F7><03><04><4C><00><05><00>

Stéphane Raimbault

unread,
Apr 20, 2017, 6:27:24 AM4/20/17
to libm...@googlegroups.com
OK so you use the right set_response_timeout API with the right libmodbus library. Do you have a segfault on modbus_set_response_timeout call?

To avoid any segfaults on read, you must allocate at least MODBUS_MAX_READ_REGISTERS (uint16_t tab_reg[MODBUS_MAX_READ_REGISTERS];).

Why do you need to set the slave? TCP proxy to RTU?

Which remote device do you have as server?

It seems the remote device set the exception code in TCP Modbus header at index 0 instead of at function index
<80><01><00><00><00><06><F7><03><00>
should be
<00><01><00><00><00><06><F7><83>...


Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+unsubscribe@googlegroups.com.

Orhan Özbek

unread,
Apr 20, 2017, 7:36:49 AM4/20/17
to libmodbus
segfault remained with allocation the remote device is called fx0-gmod by flexisoft/sick. There is a table on operation manual of the product there it says unit id=1 so i tried with setting slave to 1,247 some other values and without setting slave just in case. here is the table i mentioned http://de.tinypic.com/r/6idv6o/9 

---output---
Response timeout sec,usec: 0,500000
Byte timeout sec,usec: 0,500000
Segmentation fault (core dumped)

---without set response timout---
Response timeout sec,usec: 0,500000
Byte timeout sec,usec: 0,500000
Connecting to 192.168.1.11
[00][01][00][00][00][06][FF][03][04][4C][00][05]
Waiting for a confirmation...
<00><2E><00><32><00><00><00><00><00>
Invalid TID received 0x2E (not 0x1)
63 bytes flushed
ret:-1



#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <modbus.h>

int main(int argc, char *argv[])
{
modbus_t *ctx;
uint16_t tab_reg[MODBUS_MAX_READ_REGISTERS];
uint32_t old_response_to_sec;
uint32_t old_response_to_usec;
uint32_t old_byte_to_sec;
uint32_t old_byte_to_usec;
int rc;
int i;

ctx = modbus_new_tcp("192.168.1.11", 9100);

modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
printf("Response timeout sec,usec: %d,%d\n",old_response_to_sec,old_response_to_usec);
modbus_get_byte_timeout(ctx, &old_byte_to_sec, &old_byte_to_usec);
printf("Byte timeout sec,usec: %d,%d\n",old_byte_to_sec,old_byte_to_usec);

modbus_set_response_timeout(ctx,2,0);
//modbus_set_byte_timeout(ctx, 0, 3000);

modbus_set_debug(ctx, 1);
modbus_set_error_recovery(ctx, 
MODBUS_ERROR_RECOVERY_LINK |MODBUS_ERROR_RECOVERY_PROTOCOL);

if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}

int ret;
ret=modbus_read_registers(ctx,1100,100, tab_reg);
printf("ret:%d\n",ret);

for (i=0; i < rc; i++) {
printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
}

modbus_close(ctx);
modbus_free(ctx);
}


Stéphane Raimbault

unread,
Apr 20, 2017, 10:12:59 AM4/20/17
to libm...@googlegroups.com
2017-04-20 13:36 GMT+02:00 Orhan Özbek <orha...@gmail.com>:
segfault remained with allocation the remote device is called fx0-gmod by flexisoft/sick. There is a table on operation manual of the product there it says unit id=1 so i tried with setting slave to 1,247 some other values and without setting slave just in case. here is the table i mentioned http://de.tinypic.com/r/6idv6o/9 

---output---
Response timeout sec,usec: 0,500000
Byte timeout sec,usec: 0,500000
Segmentation fault (core dumped)

---without set response timout---
Response timeout sec,usec: 0,500000
Byte timeout sec,usec: 0,500000
Connecting to 192.168.1.11
[00][01][00][00][00][06][FF][03][04][4C][00][05]
Waiting for a confirmation...
<00><2E><00><32><00><00><00><00><00>
Invalid TID received 0x2E (not 0x1)
63 bytes flushed
ret:-1


Could you test the joined program?

Output with 3.1.4, (clang/macOS and gcc/Archlinux), no device:

Response timeout sec,usec: 0, 500000
Byte timeout sec,usec: 0, 500000
New response timeout sec, usec: 2, 0
New byte timeout sec,usec: 0, 3000
Connecting to 192.168.1.11:9100
Connection failed: Operation now in progress

-> no segfault

mb-timeout.c
Makefile

Orhan Özbek

unread,
Apr 21, 2017, 12:29:26 PM4/21/17
to libmodbus
Here is the output: connected to the device

Response timeout sec,usec: 0, 500000
Byte timeout sec,usec: 0, 500000
New response timeout sec, usec: 2, 0
New byte timeout sec,usec: 0, 3000
Connecting to 192.168.1.11:9100
[00][01][00][00][00][06][FF][03][04][4C][00][64]
Waiting for a confirmation...
ERROR Connection timed out: select
<80><01><00><00><00><06><FF><03><04><4C><00><64>Bytes flushed (60)
ret: -1

i tried to increase the response time here is the second result
Response timeout sec,usec: 0, 500000
Byte timeout sec,usec: 0, 500000
New response timeout sec, usec: 4, 0
New byte timeout sec,usec: 0, 3000
Connecting to 192.168.1.11:9100
[00][01][00][00][00][06][FF][03][04][4C][00][64]
Waiting for a confirmation...
<00><2E><00><32><00><00><00><00><00>
Invalid transaction ID received 0x2E (not 0x1)
Bytes flushed (63)
ret: -1

Do you have any idea what could be wrong ?

Stéphane Raimbault

unread,
Apr 23, 2017, 6:51:41 AM4/23/17
to libm...@googlegroups.com
OK great, no segfault, the issue was in your original code.

The first response is your initial request with 0x80 prefix, it doesn't make sense at all.
Not sure your device is Modbus compliant, you could try to isolate your fx0-gmod device from the network and analyze network packets with wireshark.

Stéphane

Orhan Özbek

unread,
Apr 24, 2017, 4:15:51 AM4/24/17
to libmodbus
After all i found a solution.. default port was 502 but software indicates that it was 9100 and slave id must be 1 in order to get a correct response from fx0-gmod 

Stephane thank you big time this would be a lot harder without your responses
take care
best regards 
orhan
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.

Stéphane Raimbault

unread,
Apr 24, 2017, 8:42:50 AM4/24/17
to libm...@googlegroups.com
Glad to hear to found out the issue.

Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages