C# Modbus Tcp Ip

Active5 years, 8 months ago

I am currently working on an application which communicated with devices using Modbus serial. The application is written in C#. Our devices have been modified to use Ethernet and I have now been tasked with converting this application to use Modbus TCP/IP. How to read PLC data (modbus tcp/ip protocol) by using tcp/ip programming? Archived Forums V > Visual Basic Language.

I have written Windows service, which perform Modbus WriteMultipleRegisters function call over TCP using NModbus library to 3-party devices every 10 minutes (ticks of System.Threading.Timer).

Occasionally this connection hang up open usually during network problems. As the device accepts only one Modbus connection at time and others are refused, connection during all next ticks fail with SocketException - ConnectionRefused.

But the device automatically closes connections which don't respond after short time. Something must keep connection open at my side even for two days. What's more when my Service is restarted, everything is fine again. So there is definitely some forgotten open connection. But I didn't manage to reproduce this bug in dev, so I don't where/when.. connection hang up. I only know that next connection is refused.

I do the modbus function call with this part of code:

device.ip is string containing IP address of device - it's correct, confirmed from SocketException details.

C# modbus tcp/ip

As I'm using using statement dispose is called on both objects.I have looked trough NModbus source code and everything is disposed correctly.

Any idea how its possible that with this code connection is not closed?

RfilipRfilip

3 Answers

I agree with nemec. If you review the documentation for TcpClient.Dispose if does not specifically mention closing the connection. It frees managed and unmanaged resources by default, but it may not correctly tear down the connection.

Try changing your code to:

That way you are doing a clean close before dispose and it should clean up even if the Modbus protocol throws some kind of exception.

ZaXaZaXa
Tcp

did you play with TcpClient.LingerState Property

defualt setting could cause problems with resetting winsock

check it outhttp://msdn.microsoft.com/pl-pl/library/system.net.sockets.tcpclient.lingerstate%28v=vs.110%29.aspx

hesarhesar

This is not an answer, but a comment with code. We have this same issue on some of our installed computers, but not all of them. The issue itself is also very intermittent, sometimes going months without happening. I am hoping someone can find an answer. Here is our brute force destroy / reconnect code that does not work:

Modbus Tcp Client

landonczlandoncz

Not the answer you're looking for? Browse other questions tagged c#tcptcpclientmodbus or ask your own question.

C# Modbus Tcp/ip Vs Ethernet Tcp/ip

  • Great library. Works unbelievably well. Thanks!!

  • GPL makes this untouchable. I would love to promote and contribute to opensource projects that I use. But restricting the use as part of a derived product is a show stopper. I can't see why a company would want to use a GPL library if they had to expose source of derived software that was not related to the GPL library itself.

  • Excellent! I sat down today to write my own Modbus TCP library to connect with Productivity line PLCs. Found this library instead, and all of ten minutes later, I'm reading and writing coils. Awesome!

  • Nice! Thx!

Modbus Tools

Read more reviews >