We are using a CR6 datalogger to poll a collection of controllers via Modbus TCP. Example below:
'Once per minute query Walchem 600 controllers If TimeIntoInterval(0,60,Sec) 'Loop through all 40 tanks For i = 1 to 40 'Attempt to connect to Walchem controller ModBusPort = TCPOpen("192.168.16." & (10 + i),502,1,5) ModbusMaster(ModBusResult,ModBusPort,9600,1,04,Walchem(i),9219,1,3,150,0) TCPClose(ModBusPort) Next i EndIf
Are we better off to establish and maintain the sockets, rather than opening and closing them repeatedly? For example, TCPOpen a collection of sockets at the start of the program, maintain the list of sockets in an array, and perhaps monitor (ping) and refresh sockets in a slow sequence?
I understand the TCPOpen function has the following ConnectHandle parameter. Should I be using the ConnectHandle to keep an array of TCP client handles? Does successive calls to TCPOpen create another socket. How can I monitor/maintain the sockets in case they go stale?
ConnectHandle
ConnectHandle is a variable or array to which resource handles associated with the
socket connection(s) will be stored. The argument should be declared as a variable
or array of type LONG. This parameter must be used if a single TCPOpen instruction
is to be used in a loop structure (typically) for the purpose of opening or serving
multiple concurrent connections; the alternative would be to use multiple independent
TCPOpen instructions within the program. This feature makes it easier to loop through
an array of IPAddr and TCPPort combinations for the purpose of initiating multiple
TCP client connections.
I have run into issues keeping a large number of TCP ports open for Modbus. It is best to close them as you are doing.