FB_SocketConnect

Using the function block FB_SocketConnect, a local client can establish a new TCP/IP connection to a remote server via the TwinCAT TCP/IP Connection Server. If successful, a new socket is opened, and the associated connection handle is returned at the hSocket output. The connection handle is required by the function blocks FB_SocketSend and FB_SocketReceive, for example, in order to exchange data with a remote server. If a connection is no longer required, it can be closed with the function block FB_SocketClose. Several clients can establish a connection with the remote server at the same time. For each new client, a new socket is opened and a new connection handle is returned. The TwinCAT TCP/IP Connection Server automatically assigns a new IP port number for each client.
Inputs
VAR_INPUT
sSrvNetId : T_AmsNetId := '';
sRemoteHost : T_IPv4Addr := '';
nRemotePort : UDINT;
bExecute : BOOL;
tTimeout : TIME := T#45s;(*!!!*)
END_VAR
Name | Type | Description |
---|---|---|
sSrvNetId | T_AmsNetId | String containing the network address of the TwinCAT TCP/IP Connection Server. For the local computer (default) an empty string may be specified. |
sRemoteHost | T_IPv4Addr | IP address (Ipv4) of the remote server in the form of a string (e.g. '172.33.5.1'). An empty string can be entered on the local computer for a server. |
nRemotePort | UDINT | IP port number of the remote server (e.g. 200). |
bExecute | BOOL | The function block is activated by a positive edge at this input. |
tTimeout | TIME | Maximum time allowed for the execution of the function block. |
![]() | Setting the maximum execution time of the function block Do not set the value "tTimeout" too low, as timeout periods of > 30 s can occur in case of a network interruption. If the value is too low, command execution would be interrupted prematurely, and ADS error code 1861 (timeout elapsed) would be returned instead of the Winsocket error WSAETIMEDOUT. |
Outputs
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
nErrId : UDINT;
hSocket : T_HSOCKET;
END_VAR
Name | Type | Description |
---|---|---|
bBusy | BOOL | This output is active if the function block is activated. It remains active until acknowledgement. |
bError | BOOL | If an error should occur during the transfer of the command, then this output is set once the bBusy output was reset. |
nErrId | UDINT | If an bError output is set, this parameter returns the TwinCAT TCP/IP Connection Server error number. |
hSocket | T_HSOCKET | TCP/IP connection handle to the newly opened local client socket. |
Requirements
Development environment |
Target system type |
PLC libraries to include (category group) |
---|---|---|
TwinCAT v3.1.0 |
PC, or CX (x86, X64, ARM) |
Tc2_TcpIp (communication) |