Modbus TCP Server Listen fails sometimes on port 502

871 views
Skip to first unread message

Sarang Kalbande

unread,
Jul 7, 2017, 4:43:06 PM7/7/17
to libmodbus
Team,
I have been using libmodbus on ARM Cortex A9 platform having ubuntu14.04 installed.
I am running Modbus TCP Server thread on device and trying to connect from simply modbus windows based client over port 502.

I am able to connect /disconnect to server and able to get the data but sometimes  [ modbus_tcp_listen(ctx, 1);]

listen is failing.After debugging it thoroughly it has been observed that server is unable to listen on port 502 .


why server stopps listening on this port and sometimes its working.I did not face this problem if I use port other than 502.

so whats special with this port? and how to make sure my application should always get this port all the time.

Note :- I am running my application as root user. 

Also I want to know ways to forcefully deallocate and allocate 502 port to my application.

Chuck Watson

unread,
Jul 7, 2017, 5:03:53 PM7/7/17
to libm...@googlegroups.com

This sounds to me like you have another MODBUS application either on your master or slave that is occasionally colliding with your application for the port.

 

The clue is that you intentionally moved the one you know about to another port and it works fine. The unknown one is still using the standard port 502 and will no longer collide.

--
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+...@googlegroups.com.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

Sarang Kalbande

unread,
Jul 7, 2017, 5:21:22 PM7/7/17
to libmodbus

Not Sure I am gracefully closing my application every time and this application is only one who is opening Port 502 and No other service is holding this port.
but if I do netstat I still see Port is in listening Mode sometimes even after killing the application.
So Is there a way using which I can make sure irrespective of Port is enguged or not My libmodbus application will make this port available for itself
while creating ctx.

Stéphane Raimbault

unread,
Jul 10, 2017, 12:46:02 PM7/10/17
to libm...@googlegroups.com
Port numbers under 1024 require root privileges.

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.

Sarang Kalbande

unread,
Jul 14, 2017, 7:54:00 AM7/14/17
to libmodbus

Its not about privileges.
My application is running in privileged mode.
I am using libmodbus-3.1.4 SDK and Server After Running for sometime always in  CLOSE_WAIT state.
there seems some issue with this SDK.

I am closing and opening ctx properly but seems some issue.
Has anyone faced this before with new sdk?

Sarang Kalbande

unread,
Jul 16, 2017, 8:39:03 AM7/16/17
to libmodbus
Hi Stephane and Chuck,
I have identified the issue,Its not about the port 502
Listen is failing when dhclient starts running.

Any idea of correlation with dhclient and listen server.
and how o handle this?


On Saturday, July 8, 2017 at 2:13:06 AM UTC+5:30, Sarang Kalbande wrote:

Stéphane Raimbault

unread,
Jul 23, 2017, 1:52:42 PM7/23/17
to libm...@googlegroups.com
Excepted a new IP attribution I'm not aware of any correlation.

--

Dirk Lohse

unread,
Jul 27, 2017, 10:05:19 AM7/27/17
to libmodbus
Hi,

may be i have the same problem. I started with stable version 3.0.6 on ARM7 with linux and tried the unit-test-(client/server) example. All works fine.

As i found the function "modbus_mapping_new_start_address" in version 3.1.4, i switched to the unstable version.

Now the unit-tests on port 1502 (default) fail for "tcp" (unit-test-server tcp). tcppi is working fine.

I compared the sources but can't find much difference.

Stéphane Raimbault

unread,
Jul 27, 2017, 11:20:39 AM7/27/17
to libm...@googlegroups.com

Dirk Lohse

unread,
Aug 1, 2017, 5:54:52 AM8/1/17
to libmodbus
Yes:

+If you still want to listen any addresses in your TCP IPv4 server, you must now
+set the IP address to NULL in modbus_new_tcp before listening. Now,
+modbus_tcp_listen only listen the IP address set in the Modbus context (see
+documentation).

changed IP from 127.0.0.1 to NULL works for me (also 0.0.0.0 works).

But the unit-test-server.c from 3.1.4 package contains 127.0.0.1 on line 60, so the example doesn't work.

Best regards,

Dirk Lohse

Stéphane Raimbault

unread,
Aug 4, 2017, 5:15:06 AM8/4/17
to libm...@googlegroups.com
unit-test-server.c is not really an example but a tests suite so I prefer to not expose the service when I run them.

Reply all
Reply to author
Forward
0 new messages