05-20-2025 09:50 AM - edited 05-20-2025 09:51 AM
I know this isn't necessarily a LabVIEW question, but I am communicating with Modbus device using LabVIEW and I'm doing it from an NI RT target and I know a lot of you use Modbus for various items, so this seemed like as good a place as any to ask the following question:
I'm talking to multiple devices and for each device it's possible I will request something around 10k holding registers. It's gotta be done in a timely manner. My code is currently set up to to open asynchronous calls for each device it needs to talk to, get the data required, and then close. During the instance, it'll establish the TCP connection and Close it when it's done. Then some time will pass and it'll do it again (possibly 15 to 30 seconds). Typically I just maintain TCP connections, but if memory is a potential issue on the RT target, would this be so bad Disconnecting/Reconnecting to the Slave MB unit? Is there some delay there to establish the comm? Are the savings in memory just absolutely not worth it because they are so incredibly small? Or does the TCP communication get established pretty quickly and Slave units have to problem responding to clients quickly?
05-21-2025 01:37 AM - edited 05-21-2025 01:40 AM
Each connect takes time yes. This can be between several ms when on a local network to several seconds when on a remote network where the packets have to travel through network hubs, gateways, and firewalls. This is because every connect requires a handshake protocol where signaling packets have to travel several times back and forth before the connection is established. Disconnect also has a handshake protocol, so has some delay too and the extra difficulty that sockets are left lingering for a while so if you rapidly open and close connections, the socket library may run out of resources eventually. However once every few seconds a new connection should not run in this unless you have a really resource constrained system.
Basically, yes frequently connecting and disconnecting has a real cost too, but in comparison to reading 10000 Modbus registers this is likely peanuts.
Personally I still wouldn’t do it but I also never assume that the connection will simply stay alive forever once opened. Shit happens and a connection can go stale (temporary cable disconnect or network routing change, or the remote side simply disconnected for whatever reason). So I do the network communication and whenever I detect an error other than timeout, I disconnect and reconnect again.