FB_SocketConnect

FB_SocketConnect 1:

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.

VAR_INPUT

VAR_INPUT
    sSrvNetId       : T_AmsNetId := '';
    sRemoteHost     : STRING(15);
    nRemotePort     : UDINT;
    bExecute        : BOOL;
    tTimeout        : TIME := T#45s;(*!!!*)
END_VAR

sSrvNetId: string containing the network address of the TwinCAT TCP/IP Connection Server. For the local computer (default) an empty string may be specified.

sRemoteHost: IP address (Ipv4) of the remote server as a string (e.g. '172.33.5.1'). An empty string can be entered on the local computer for a server.

nRemotePort: IP port number of the remote server (e.g. 200).

bExecute: the function block is enabled via a positive edge at this input.

tTimeout: maximum time that may not be exceeded when the function block is executed.

FB_SocketConnect 2:

The tTimeout value should not be set too low, since timeout periods of > 30 s may occur in the event 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.

VAR_OUTPUT

VAR_OUTPUT
    bBusy       : BOOL;
    bError      : BOOL;
    nErrId      : UDINT;
    hSocket     : T_HSOCKET;
END_VAR

bBusy: When the function block is activated this output is set. It remains set until and acknowledgement is received.

bError: If an error should occur during the transfer of the command, then this output is set once the bBusy output was reset.

nErrId : If the bError output is set, this parameter returns the TwinCAT TCP/IP Connection Server error number.

hSocket: TCP/IP connection handle for the newly opened local client socket.

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT version 2.8.0 or higher

PC or CX (x86)

TcpIp.Lib

( Standard.Lib; TcBase.Lib; TcSystem.Lib are included automatically )

TwinCAT v2.10.0 Build >= 1301

CX (ARM)