TCP_Connect block

Short summary

Name

TCP_Connect

→POU type

→function

Category

more system blocks, network blocks, block with internal error diagnostic

→Namespace

logicals.system.network.tcp

Graphical interface

images/download/attachments/429719943/TCP_Connect-version-3-modificationdate-1562746898650-api-v2.png

Available since

version 3.2.2 (for library Standard )
images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/warning.svg This block is supported for these target systems or platforms:

  • Raspberry Pi, Revolution Pi

  • platform WindowsX86 (incl. the built-in PLC under Windows)

  • platform LinuxX86

Functionality

The block establishes a connection to a →TCP server.

Inputs, outputs, return value


Identifier

→Data type

Description

Inputs:


destinationAddress

DWORD

destination IP address

destinationPort

UINT

destination IP port

interfaceAddress

DWORD

network interface IP address

interfacePort

UINT

network interface IP port

Outputs:

clientHandle

TCP_ClientHandle

handle of the reserved address and port

Return value:

rc

TCP_ErrorCode

returns the status of the TCP operation:

  • succeeded: The operation is successful.

  • cannotCreateSocket: The socket cannot be created.

  • cannotBindSocket: The socket for the interface cannot be bound.

  • cannotConnectToServer: The connection to the server cannot be established.

Input EN and output ENO are available when →calling the block. See "Execution control: EN, ENO" for information on input EN and output ENO.

See:

Internal error diagnostic for block

The block checks the following error cases:

  • The socket cannot be created.

  • The socket for the interface cannot be bound.

  • The connection to the server cannot be established.

In such an error case, the output ENO of the block is set to value FALSE (or an equivalent). Moreover, the block returns the appropriate code (see the above table).

Example for usage within ST-editor

Program with calls of TCP blocks
PROGRAM TCP_Client
USING logicals.system.network.tcp;
VAR
rcTCPConnect : TCP_ErrorCode := invalidHandle;
clientCommData : tcpClientCommData;
receiveTestMsg : STRING[30] := '';
init : bool := true;
END_VAR
 
IF init THEN
clientCommData.serverAddress := INET_ATON('127.0.0.1');
clientCommData.serverPORT := 9999;
END_IF;
 
IF rcTCPConnect <> succeeded THEN
rcTCPConnect := TCP_Connect(destinationAddress:=clientCommData.serverAddress, destinationPort:=clientCommData.serverPORT, clientHandle=>clientCommData.clientHandle);
ELSE
TCP_Receive(clientHandle:=clientCommData.clientHandle, data:=receiveTestMsg);
END_IF;
END_PROGRAM
 
TYPE
tcpServerCommData : STRUCT
address : DWORD;
port : UINT;
serverHandle : logicals.system.network.tcp.TCP_ServerHandle;
clientHandle : logicals.system.network.tcp.TCP_ClientHandle;
END_STRUCT;
 
tcpClientCommData : STRUCT
serverAddress : DWORD;
serverPORT : UINT;
clientHandle : logicals.system.network.tcp.TCP_ClientHandle;
END_STRUCT;
END_TYPE

When creating your application within the ST-editor, enter a call of a block by typing the text as requested by the syntax or use Content Assist.