CLOSE_WAIT libmodbus v3.1.4

162 views
Skip to first unread message

Nataliya Orlowa

unread,
Jan 4, 2018, 3:45:52 AM1/4/18
to libmodbus

Good afternoon !
I'm using libmodbus v3.1.4.
Operating system: Linux debian 3.16.0-4-686-pae # 1 SMP Debian 3.16.39-1 (2016-12-30) i686 GNU / Linux.
I wrote the test application:
 
If the communication channel is interrupted between the client and the server, the exchange is no longer restored.
You must restart the application on the server.
 
Normal data exchange:
 
root@debian:/usr/src/modbus_slave# netstat -tonp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name Timer
tcp        0     11 172.30.100.30:502       192.168.250.1:40445     ESTABLISHED 2430/test_TCP_modbu on (0,19/0/0)
tcp        0    272 172.30.100.30:22        192.168.250.2:50716     ESTABLISHED 2303/sshd: javorole on (0,45/0/0)
tcp6       1      0 ::1:48391               ::1:631                 CLOSE_WAIT  492/cups-browsed off (0.00/0/0)
 
 
Status after a communication link failure:
 
root@debian:/usr/src/modbus_slave# netstat -tonp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name Timer
tcp        0      0 172.30.100.30:502       192.168.250.1:40485     SYN_RECV    -                on (1,10/1/0)
tcp        0      0 172.30.100.30:502       192.168.250.1:40481     SYN_RECV    -                on (0,50/2/0)
tcp        0      0 172.30.100.30:502       192.168.250.1:40476     SYN_RECV    -                on (1,30/3/0)
tcp        0      0 172.30.100.30:502       192.168.250.1:40484     SYN_RECV    -                on (2,90/2/0)
tcp        0      0 172.30.100.30:502       192.168.250.1:40478     SYN_RECV    -                on (6,50/3/0)
tcp        0      0 172.30.100.30:502       192.168.250.1:40477     SYN_RECV    -                on (3,70/3/0)
tcp        0      0 172.30.100.30:502       192.168.250.1:40465     ESTABLISHED 2430/test_TCP_modbu off (0.00/0/0)
tcp       13      0 172.30.100.30:502       192.168.250.1:40473     CLOSE_WAIT  -                off (0.00/0/0)
tcp       13      0 172.30.100.30:502       192.168.250.1:40468     CLOSE_WAIT  -                off (0.00/0/0)
tcp        0    272 172.30.100.30:22        192.168.250.2:50716     ESTABLISHED 2303/sshd: javorole on (0,42/0/0)
tcp6       1      0 ::1:48391               ::1:631                 CLOSE_WAIT  492/cups-browsed off (0.00/0/0)
 
It is not possible to reset the hung sessions without restarting the application to the server.
Every time you restart the application on the server is not convenient.
On the Internet they write that it is software problem on the server.
Help please ....
In the application is my test program.
 
slave.c

Nataliya Orlowa

unread,
Jan 4, 2018, 9:56:03 AM1/4/18
to libmodbus
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.

Stéphane Raimbault

unread,
Jan 4, 2018, 12:00:41 PM1/4/18
to libm...@googlegroups.com

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

Nataliya Orlowa

unread,
Jan 5, 2018, 3:57:05 AM1/5/18
to libmodbus
On Thursday, 4 January 2018 20:00:41 UTC+3, Stephane Raimbault wrote:

 Good afternoon !
 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







 

Nataliya Orlowa

unread,
Jan 9, 2018, 9:28:40 AM1/9/18
to libmodbus
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.

Patrick Boettcher

unread,
Jan 9, 2018, 9:49:51 AM1/9/18
to Nataliya Orlowa, libm...@googlegroups.com
What code did you use for the server-code? Did you take it from the
test-dir of libmodbus?

--
Patrick.

Nataliya Orlowa

unread,
Jan 9, 2018, 10:46:15 AM1/9/18
to libmodbus
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.

Nataliya Orlowa

unread,
Jan 9, 2018, 10:57:37 AM1/9/18
to libmodbus
I do not know how to write, so that the application does not stick.
I tried everything that I got ....

Patrick Boettcher

unread,
Jan 9, 2018, 11:02:07 AM1/9/18
to Nataliya Orlowa, libm...@googlegroups.com
On Tue, 9 Jan 2018 07:46:14 -0800 (PST)
Nataliya Orlowa <norl...@gmail.com> wrote:

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

If I understand you correctly it's your server-side program which
doesn't work? Am I right?

--
Patrick.

Nataliya Orlowa

unread,
Jan 9, 2018, 1:20:12 PM1/9/18
to libmodbus
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. :(


Nataliya Orlowa

unread,
Jan 10, 2018, 10:11:44 AM1/10/18
to libmodbus
Good afternoon !
tell me, can I get around this problem without writing a full-fledged server application with fork () to handle connections?
Thank you

Patrick Boettcher

unread,
Jan 10, 2018, 10:44:37 AM1/10/18
to Nataliya Orlowa, libm...@googlegroups.com
Sure, a real server handling multiple connections with fork() or with
threads might avoid certain blocking connection-situations you are
facing now.

You can then use the libmodbus-functions to decode and encode
modbus-request/replies.

I didn't see a problem (after having had a quick glance) in
your example-server-code which would explain a blocking second
connection from a client. The problem might be somewhere else.

--
Patrick.

Nataliya Orlowa

unread,
Jan 11, 2018, 8:01:00 AM1/11/18
to libmodbus
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?



web_Relay8.jpg
Reply all
Reply to author
Forward
0 new messages