FB_SocketAccept
The remote client connection requests arriving at the TwinCAT TCP/IP Connection Server have to be acknowledged (accepted). The function block FB_SocketAccept accepts the incoming remote client connection requests, opens a new remote client socket and returns the associated connection handle. The connection handle is required by the function blocks FB_SocketSend and FB_SocketReceive in order to exchange data with the remote client, for example. All incoming connection requests first have to be accepted. If a connection is no longer required or undesirable, it can be closed with the function block FB_SocketClose.
A server implementation requires at least one instance of this function block. This instance has to be called cyclically (polling) from a PLC task. The block can be activated cyclically via a rising edge at the bExecute input (e.g. every 5 seconds).
If successful, the bAccepted output is set, and the connection handle to the new remote client is returned at the hSocket output. No error is returned if there are no new remote client connection requests. Several remote clients can establish a connection with the server at the same time. The connection handles of several remote clients can be retrieved sequentially via several function block calls. Each connection handle for a remote client can only be retrieved once. It is recommended to keep the connection handles in a list (array). New connections are added to the list, and closed connections must be removed from the list.
VAR_INPUT
VAR_INPUT
sSrvNetId : T_AmsNetId := '';
hListener : T_HSOCKET;
bExecute : BOOL;
tTimeout : TIME := T#5s;
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.
hListener: Connection handle of the listener sockets. This handle must first be requested via the function block FB_SocketListen.
bExecute: The block is activated by a rising edge at this input.
tTimeout: Maximum time allowed for the execution of the function block.
VAR_OUTPUT
VAR_OUTPUT
bAccepted : BOOL;
bBusy : BOOL;
bError : BOOL;
nErrId : UDINT;
hSocket : T_HSOCKET;
END_VAR
bAccepted: This output is set if a new connection to a remote client was established.
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: Connection handle of a new remote client.
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) |