Leaving Communiation Handle Open

Sorry if this has been answered but I couldn’t find the answer.

In PAC Control - the Modbus Integration Kit.

I noticed the subroutines (like O22Modbus03ReadHoldingRegistersAsInt) don’t appear to close the communication. Would this cause problems If I were to keep calling the SR for new Modbus devices? I guess worst case for me would be 25.


No that is the way you want to do it. You don’t want to close comm handles unless you are done communicating.

What if you’re writing to a local file? I noticed one of OptoMary’s old posts is writing to a file and it opens and then closes the comm handle on each write.

That should be okay - it is more on TCP that you want to leave the handle open. Typically if you are using the comm handle in a tight loop - writing every few seconds or faster, then leave it open. If you are writing a log entry once a minute, then you could probably close it.

I just checked one of my strategies that writes a file and I close it after each write. It writes once every 15 minutes though.

Great, That’s similar to how I’m using it too.

I believe the reason for this is because the TCP specification states there is a dwell time or a short time period after the session is shut that the session sort of lives on. It gives any ignored packets time to die.
So, if you open and close too quick, you will run out of sessions (in the TCP stack).

TCP is supposed to let the other side know the connection is being closed by sending a FIN packet, which in turn gets a FIN and an ACK, which then responds with a final FIN, so there is some data exchanged to close the connection and the speed at which this happens depends on the latency of the connection.

What I don’t know is if Closing communication in Opto waits for this process to finish, or if it is done asynchronously. Based on the session resources running out if done in a tight loop, I would assume the latter.

1 Like