TCP/IP Client

TCP/IP is a connection-oriented communication type (peer-to-peer connection). In this example we illustrate how a connection is established between the Beckhoff controller and a server. The required function blocks are FB_IpStartSession, FB_IpOpen, FB_IpSend and optionally FB_IpReceive, FB_IpClose and FB_IpEndSession.

It is advisable to program a step sequence as illustrated in the example below.

In this case the Beckhoff controller is the client and the partner device a TCP server. The server is a VB6 program.

Step 1

FB_IpStartSession

The function block allocates resources on the controller for the TCP/IP communication. The function block becomes active with bStart. bBusy is set as long as the function block is busy. iDevice should always be set to zero. iPort is used for the local TCP/IP port number. iMaxConnection indicates the maximum number of connections (up to 3).

INPUT

bStart: A rising edge activates the function block.

iDevice: always "0"

eSocketType: For TCP/IP "SOCK_STREAM" should be used. Please note that the data are stored as a byte stream.
If possible, the length of the received data should be known or a protocol with start and end ID should be used, so that the start and end can be detect unambiguously in the data stream.

eSocketProtocol: For TCP/IP "IPPROTO_TCP" should be used

iPort: Sender port number

iMaxConnection: Number of possible connections (max. 3)

tTimeout: Time after which the attempt is aborted.

OUTPUT

bBusy: This output remains TRUE until execution of the command is complete.

bError: This output is switched to TRUE as soon as an error occurs during the execution of a command. The command-specific error code is contained in iErrorId.

iErrorId: Contains the command-specific error code of the most recently executed command (see table).

iSession: Forwards the session number to all IP function blocks for which this connection was created.

Step 2

FB_IpOpen

Received data must be stored in a PLC variable. To this end a pointer to pBuffAddr is required, and the size of the variable has to be entered in cbBuffLen. A positive edge of bValid indicates that the memory contains data or the data of the associated variable are now valid.

INPUT

bClear: A rising edge clears the memory. The function block is then ready to receive data again.

iSession: Is linked with iSession from function block FB_StartSession.

sRemoteIPAddr: Can be used as filter for approving only a specific IP address

eSocketProtocol: For TCP/IP "IPPROTO_TCP" should be used

iPort: Local port number

iMaxConnection: Number of possible connections (max. 3)

tTimeout: Time after which the attempt is aborted.

OUTPUT

bBusy: This output remains TRUE until execution of the command is complete.

bError: This output is switched to TRUE as soon as an error occurs during the execution of a command. The command-specific error code is contained in iErrorId.

iErrorId: Contains the command-specific error code of the most recently executed command (see table).

iSession: Forwards the session number to all IP function blocks for which this connection was created.

Data can be returned as soon as the first data have been received. This functionality is optional and is used in the example.

Example

TCP/IP Client 1: Download VB6 program as TCP/IP server zip file (sample file)
TCP/IP Client 2: Download TwinCAT project as TCP/IP client prx file (sample file)

tcp server.zip
bx9000_client.prx