Topic: DMD0069

Help File Version: 2.10.0.636

OPENTCP - Open TCP Connection


This instruction is NOT used to communicate via Modbus/TCP protocol. This instruction is used to implement a custom protocol over a TCP/IP connection. Use the built-in Modbus/TCP Client instructions Modbus Network Read (MRX) and Modbus Network Write (MWX) to communicate via Modbus/TCP protocol.

 

The Open TCP Connection (OPENTCP) instruction is used to create a TCP Client connection to an external TCP Server at the specified TCP/IP address and Port number. Each time the OPENTCP instruction creates a connection to a TCP remote server it uses a TCP socket. TCP sockets are a finite resource which must be managed by the TCP stack. The main concern with sockets is the frequency of new connections being opened and closed per unit of time. Every socket that has been used is kept around by the TCP stack for some amount of time (defined by the stack). If a connection is opened, some work is done, then closed, and repeated as fast as possible, the list of used sockets gets longer and longer, resulting in significant increases in scan time (on the order of 10s of milliseconds).


Since the most likely scenario for causing problems is a single connection in a round robin of multiple servers, our recommendation is that a separate TCP connection be established for each TCP Server, and that those connections remain open as long as data is being transferred between the TCP Client and the TCP Server. Only after all of the data transfers are complete should the connections be closed. The key thing is keeping the number of socket open / close operations to reasonable amount. Not doing so will cause the scan time to increase because the CPU is having to spin through all of the closed sockets prior to their being purged.

 

Using a Close Device instruction on the TCP device will shut down the connection to the remote TCP server if it is still open, though it is possible for the connection to have already been closed by the server because most Servers will not allow a connection top remain open indefinitely with no activity.

 

 


Parameters:

Note: Use the F9 key or click the 'three dot box' at the right edge of the parameter field to open the Default Element Selection Tool (the Element Picker or the Element Browser) or use the Down-Arrow key (Auto-Complete) on any parameter field to see a complete list of the memory locations that are valid for that parameter of the instruction.

 

TCP Device selects which TCP Client device to use.

no devices available means there are no TCP Client devices. The most likely causes are there are no TCP Client devices configured, or the CPU does not have an on-board Ethernet port.


Select the create device option to open the Device Configuration dialog of the System Configuration to create a new TCP Client device.


To IP Address specifies the IP Address of the TCP Server to open a connection to. The IP Address can be specified in one of the two following ways:

 

  • Variable Address means that each time instruction is executed it will retrieve the IP Address from a memory location in the CPU. This can be any readable DWord numeric location. This allows the IP Address to be changed at runtime. Each octet of the IP Address is stored in one byte of the Variable Address location. In the example below the Initialize Data instruction will use the :UB operator to place the 4 octet values of IP Address 192.168.26.71 into the 4 Bytes of the DWord location D0 in the proper order.

     

     

    The 'IP Address' format selection in a Data View can be used to see the IP Address stored in the DWord location in the traditional dot-decimal notation (000.000.000.000).

     

  • Fixed Address means the IP Address is entered in the instruction as 4 separate decimal numbers, each one ranging from 0 to 255. IP Addresses 0.0.0.0, 127.0.0.1, and 255.255.255.255 are not allowed.


TCP Port Number is the IP Port number to use when opening the connection. This can be any positive integer between 1 and 65535, or any readable numeric location containing a value in that range.

 


The On Success and On Error parameters specify what action to perform when this instruction completes. You do not have to use the same type of selection for both On Success and On Error.

 

If the Set Bit selection is used for either On Success or On Error, the specified BIT location will be SET OFF when the instruction is first enabled and will remain OFF until the instruction completes. Once complete, the appropriate Success or Error bit location ON. The specified Bit location is enabled with a SET (Latch) operation meaning that it will remain ON even if the input logic for the instruction goes OFF.

 

If the JMP to Stage selection is used for either On Success or On Error the target Stage must be in the same Program code-block as this instruction, you cannot specify a target Stage that exists in a different Program code-block. When the operation finishes, the target Stage will be enabled the same way as a standalone Jump to Stage (JMP) instruction would do it. The JMP to Stage option will only be available if this instruction is placed in a Program code-block.

 

On Success selects which of the following actions to perform if the operation is successful:

  • Enable SET Bit then specify any writable bit location.
  • Enable JMP to Stage then specify any Stage number from S0 to S127 in the current Program code-block.

 

On Error selects which of the following actions to perform if the operation is unsuccessful:
 

  • Enable SET Bit then specify writable bit location.
  • Enable JMP to Stage then specify any Stage number from S0 to S127 in the current Program code-block.

 

If either the On Success or On Error selections are set to JMP to Stage, Automatically create the SG box for any NEW stage number will be enabled which will automatically create any target stage that does not already exist.

  • Below this rung will create the new target stage on a new rung following this instruction.
  • At end of code-block will create the new target stage as the last rung of this Program.

 


Status Display:

 

The red triangle in the upper left corner of the status display indicates this is a Fully Asynchronous instruction.

 

The gray triangle at the right end of the input leg indicates the input is edge-triggered, meaning this instruction will execute each time the input logic transitions from OFF to ON.

 


Structure Memory:

The TCP Client has an associated structure that is used by the OPENTCP instruction.

 

 

The syntax for using them is <TCP Device>.<flag name>. The following fields of a TCP Server structure can be used in the project:

.InQueue (Read / write) is a 16-bit unsigned location that contains the number of bytes of data currently in the input buffer. The input buffer is automatically cleared when a new connection is made.
 

.OutQueue (Read / write) is a 16-bit unsigned location that contains the number of bytes of data currently in the output buffer. The output buffer is automatically cleared when a new connection is made.

 

.Open (Read-only) is a Bit location that will be ON if an OPENTCP has been executed and a corresponding CLOSE has not been executed.

 

.Connected (Read-only) is a Bit location that will be ON if the initial connection to the TCP Server is successful. It will be turned OFF when a CLOSE instruction is successfully executed. Note: if the TCP server closes the connection - because of inactivity, the network cable problems, etc. - this Bit will NOT be turned OFF.

 

.CTS (Read-only) is not used by this instruction.

 

.RTS (Read / write) is not used by this instruction.

 

Note: if Connected is ON, you can use the DEVCLEAR - Clear Device instruction to clear the .InQueue and .OutQueue structure members as needed.


See Also:

OPENTCP - Open TCP Connection

 

TCPLISTEN - Start Listening on TCP Port

 

CLOSE - Close Device

 

DEVCLEAR - Clear Device

 


Related Topics:

STREAMIN - Stream In Data from Device

 

STREAMOUT - Stream Out Data to Device

 

String Functions Overview

 

Scripting Language Reference

 


Example Using Stages:

 


Rung Example: