TCP/IP Server
TCP/IP is a connection-oriented communication type (peer-to-peer connection). In this example we illustrate how an external connection is established with the Beckhoff controller. The required function blocks are FB_IpStartSession, FB_IpReceive and optionally FB_IpSend, FB_IpClose and FB_IpEndSession.
It is advisable to program a step sequence as illustrated in the example below.
The Beckhoff controller acts as server and the partner device as TCP client. The client 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. The length of the data to be received should be known or a protocol with a start/end ID should be used so that the start and end can be identified from the data stream.
eSocketProtocol: For TCP/IP "IPPROTO_TCP" should be used
iPort: Receiving 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_IpReceive
Received data must be stored in a PLC variable. To this end a pointer to pBuffAddr is required, and the size of the variable is specified 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.
Example
Download VB6 program as TCP/IP client zip file (sample file)
Download TwinCAT project as TCP/IP server prx file (sample file)