modbus_receive problems

522 views
Skip to first unread message

Roberto Peci

unread,
Feb 21, 2017, 8:55:36 AM2/21/17
to libm...@googlegroups.com

Good morning,


I’m trying to modify a qt application with the communication part based on Libmodbus 3.0.6 (my instrument is a server) and I’m finding a problem with the function modbus_receive: I would like to set a timeout because my loop result blocked in case of cable disconnected/broken/… Surfing the web I found this problem in many posts and many of Stephan answer, but honestly, I’m not able to understand how can I modify my code.

I tried migrating to Libmodbus 3.1.4 and using modbus_set_response_timeout function, but the problem still persists, so I think I’m not using correctly the functions. I’m a newbie about this world and I would like you to revise my code/suggest updated example because I lost myself in posts dated from 2010 to 2014!


Here is my code:


//initialization and declarations

[…]


       socket = modbus_tcp_listen(ctx, 1);

       if(socket == -1)

       {

           fprintf(stderr, "Unable to listen %s\n", modbus_strerror(errno));

           modbus_free(ctx);

           return;

       }

       if(modbus_tcp_accept(ctx, &socket) == -1)

       {

           fprintf(stderr, "Unable to accept %s\n", modbus_strerror(errno));

           go_to_free = 1;

       }



modbus_get_response_timeout(ctx, &response_timeout);

printf("\nresponse timeout: %ld.%06ld\n",response_timeout.tv_sec,response_timeout.tv_usec);


new_response_timeout.tv_sec = 5;

new_response_timeout.tv_usec = 0;

modbus_set_response_timeout(ctx, &new_response_timeout);


/* Control new timeout */

modbus_get_response_timeout(ctx, &response_timeout);

printf("\nresponse timeout: %ld.%06ld\n",response_timeout.tv_sec,response_timeout.tv_usec);

while (go_to_free == 0)

{

if (modbus_receive(ctx, query) == -1) /* where code result blocked in case of cable disconnection */

{

printf("[ERROR] modbus_receive\n");

break;

}


if (config_data.modbus_enabled == false)

{

break;

}


if (modbus_reply(ctx, query, rc, mb_mapping) == -1)

{

printf("[ERROR] modbus_reply\n");

break;

}



for (I=REG_IDX_PUMP_FLOW; I<=holding_register_limit; I++)

{

// decoding messages from client

[…] 



Thanks and best regards,

Roberto

Stéphane Raimbault

unread,
Feb 23, 2017, 8:48:42 AM2/23/17
to libm...@googlegroups.com
You can use http://libmodbus.org/docs/v3.1.4/modbus_set_error_recovery.html with the options given in the example and let the magic begin (automatic handling of network errors, etc).

--
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.

RP Ita

unread,
Mar 3, 2017, 8:39:43 AM3/3/17
to libmodbus
I'm really sorry, I didn't say that I'm programming a Server/Slave and, as indicated in documentation "It’s not recommended to enable error recovery for slave/server.". What can I do?

RP Ita

unread,
Mar 3, 2017, 8:41:31 AM3/3/17
to libmodbus
Or well, I tried adding 

modbus_set_error_recovery(ctx, (modbus_error_recovery_mode)(MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL));

after "modbus_set_response_timeout()" function, but nothing's changing...
Reply all
Reply to author
Forward
0 new messages