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