Good afternoon ! I looked with the help of strace, in which place the data exchange is stuck when the communication channel is lost.
[080516ac] send(4, "\0\254\0\0\0\5\n\3\2\0 ", 11, MSG_NOSIGNAL) = 11
[080756ce] write(1, "Waiting for a indication...\n", 28Waiting for a indication...
) = 28
[08076048] _newselect(5, [4], NULL, NULL, NULL) = 1 (in [4])
[0805164c] recv(4, "\0\255\0\0\0\6\n\3", 8, 0) = 8
[08076048] _newselect(5, [4], NULL, NULL, {tv_sec=0, tv_usec=500000}) = 1 (in [4], left {tv_sec=0, tv_usec=499990})
[0805164c] recv(4, "\0\32\0\1", 4, 0) = 4
[080756ce] write(1, "<00><AD><00><00><00><06><0A><03>"..., 49<00><AD><00><00><00><06><0A><03><00><1A><00><01>
) = 49
[080756ce] write(1, "Temp1=32\n", 9Temp1=32
) = 9
[080756ce] write(1, "Pressure Hi=1\n", 14Pressure Hi=1
) = 14
[080756ce] write(1, "Pressure Lo=24\n", 15Pressure Lo=24
) = 15
[080756ce] write(1, "Temp DHT22 =43\n", 15Temp DHT22 =43
) = 15
[080756ce] write(1, "Hym DHT22=11\n", 13Hym DHT22=11
) = 13
[080756ce] write(1, "Temp LM75_Local=44\n", 19Temp LM75_Local=44
) = 19
[080756ce] write(1, "Temp LM75_Remote=66\n", 20Temp LM75_Remote=66
) = 20
[080756ce] write(1, "Lamp_Switch=0\n", 14Lamp_Switch=0
) = 14
[080756ce] write(1, "SLAVE: regs[] =\t32 1 24 43 11 44"..., 43SLAVE: regs[] = 32 1 24 43 11 44 66 0 0 0
) = 43
[080756ce] write(1, "[00][AD][00][00][00][05][0A][03]"..., 45[00][AD][00][00][00][05][0A][03][02][00][00]
) = 45
[080516ac] send(4, "\0\255\0\0\0\5\n\3\2\0\0", 11, MSG_NOSIGNAL) = 11
[080756ce] write(1, "Waiting for a indication...\n", 28Waiting for a indication...
) = 28
[08076048] _newselect(5, [4], NULL, NULL, NULL
You can not connect to the application, it does not respond. Help to understand what's wrong.
--
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.
You just forget to provide error handling to your code...
While it was not possible to solve the problem.
I found a person with a similar situation:
https://groups.google.com/forum/#!topic/libmodbus/bfioPGAGOaA
Unfortunately there is also very little inormation.
I added error processing to your code in the code:
modbus_set_error_recovery(ctx,MODBUS_ERROR_RECOVERY_LINK);
It did not help.
I could not use the second advice. I'm not very good at progamating network communication.
When the network link breaks, the processing gets stuck on the function rc = modbus_receive (ctx, query) ;.
I'm using this application to poll the server. https://play.google.com/store/apps/details?id=net.sagram.hmi_modbus_viewer&hl=en Access to the server is via OpenVPN
help with my application on the server.
=================================================================================================================================================================
/*
* slave.c - a MODBUS slave
*/
#include <stdio.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <modbus.h>
// modbus_mapping_t *mb_mapping;
int main(void)
{
int i;
int s = -1;
modbus_t *ctx;
modbus_mapping_t *mb_mapping;
uint32_t tv_sec = 0;
uint32_t tv_usec = 0;
lol:
ctx = modbus_new_tcp("0.0.0.0", 502);
modbus_set_debug(ctx, TRUE);
mb_mapping = modbus_mapping_new(0, 0, 500, 500);
if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
//modbus_set_error_recovery(ctx,MODBUS_ERROR_RECOVERY_PROTOCOL);
modbus_set_error_recovery(ctx,MODBUS_ERROR_RECOVERY_LINK);
// This code is for version 3.1.2
modbus_get_response_timeout(ctx, &tv_sec, &tv_usec);
printf("Default response timeout:%d sec %d usec \n",tv_sec,tv_usec );
tv_sec = 30;
tv_usec = 10000;
modbus_set_response_timeout(ctx, tv_sec,tv_usec);
modbus_get_response_timeout(ctx, &tv_sec, &tv_usec);
printf("Set response timeout:%d sec %d usec \n",tv_sec,tv_usec );
mb_mapping->tab_registers[0x01] = 32;
mb_mapping->tab_registers[0x02] = 1;
mb_mapping->tab_registers[0x04] = 43;
mb_mapping->tab_registers[0x05] = 11;
mb_mapping->tab_registers[0x06] = 44;
mb_mapping->tab_registers[0x07] = 66;
printf("Start Listen !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
s = modbus_tcp_listen(ctx, 1);
printf("End Listen !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("Start Accept !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
modbus_tcp_accept(ctx, &s);
printf("End Accept !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
for (;;) {
uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
int rc;
modbus_set_error_recovery(ctx,MODBUS_ERROR_RECOVERY_LINK);
printf(" Start Recive !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
rc = modbus_receive(ctx, query);
fprintf(stderr, "Failed modbus: %s\n", modbus_strerror(errno));
// fprintf(stderr, "Failed modbus: %s\n", ctx->error_recovery );
printf(" End Recive !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
if ( rc == -1) /* where code result blocked in case of cable disconnection */
{
printf("[ERROR] modbus_receive !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
}
mb_mapping->tab_registers[0x03]++;
printf ("Temp1=%d\n",mb_mapping->tab_registers[0x01]);
printf ("Pressure Hi=%d\n",mb_mapping->tab_registers[0x02]);
printf ("Pressure Lo=%d\n",mb_mapping->tab_registers[0x03]);
printf ("Temp DHT22 =%d\n",mb_mapping->tab_registers[0x04]);
printf ("Hym DHT22=%d\n",mb_mapping->tab_registers[0x05]);
printf ("Temp LM75_Local=%d\n",mb_mapping->tab_registers[0x06]);
printf ("Temp LM75_Remote=%d\n",mb_mapping->tab_registers[0x07]);
printf ("Lamp_Switch=%d\n",mb_mapping->tab_registers[0x1A]);
printf("SLAVE: regs[] =\t");
for(i = 1; i != 11; i++) { // looks like 1..n index
printf("%d ", mb_mapping->tab_registers[i]);
}
printf("\n");
if (rc > 0) {
/* rc is the query size */
printf("Start Replay !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
modbus_reply(ctx, query, rc, mb_mapping);
printf("End Replay !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
} else if (rc == -1) {
/* Connection closed by the client or error */
printf ("Client Error !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1\n");
break;
}
}
printf("Quit the loop: %s\n", modbus_strerror(errno));
if (s != -1) {
close(s);
}
printf("Free Mapping !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
modbus_mapping_free(mb_mapping);
modbus_close(ctx);
modbus_free(ctx);
goto lol;
return 0;
}
https://github.com/stephane/libmodbus/blob/master/src/modbus.c#L185
Good afternoon !
With a frowning new 2018 year!
Why is everyone silent?
Prompt, there I inserted processing errors?
At me all the priest still:
After the connection is lost (VPN), the application can not work.
Somewhere on the Internet I found, modified a little.
This is a test application to solve my problem:
After the link disappears during the exchange of information, re-connect to the application is no longer possible.
It helps only restart the application.
I do not know how to write, so that the application does not stick.
I tried everything that I got ....
Yes, it is above.
In the second version, the function modbus_set_error_recovery (ctx, MODBUS_ERROR_RECOVERY_LINK);
Everything works until the connection breaks.
After a communication failure, interaction with the server is not restored. :(
Good afternoon !
tell me, can I get around this problem without writing a full-fledged server application with fork () to handle connections?
Thank you
Good afternoon !
What to do?!
Processing requests from the client is stuck in the function rc = modbus_receive (ctx, query) ;, and management in my program is not returned ...
tcp 13 0 172.30.100.30:502 192.168.250.1:38350 CLOSE_WAIT
tcp 0 256 172.30.100.30:22 172.17.242.88:64003 ESTABLISHED
tcp 13 0 172.30.100.30:502 192.168.250.1:38346 CLOSE_WAIT
tcp 0 0 172.30.100.30:502 192.168.250.1:38317 ESTABLISHED
The Chinese board for 8 relays, normally fulfills the loss of the communication channel and normally continues to respond to requests after the restoration of the channel ....
Adding the modbus_set_error_recovery (ctx, MODBUS_ERROR_RECOVERY_LINK) function to the code ;, but this did not help ...
Stephane Raimbault, tell me how to write applications in a regular way so that sessions do not stick out?