This function block from run in task Back, manages data exchange over the TCP network by acting as a server by accepting connections from client nodes managed by the FB TCPDataExchClient. By activating the FB, the server listens on the port defined in Port, the server accepts the simultaneous number of connections from clients defined in Connections. In DEDefs it is necessary to define the address of the array of structures TCPDEXCHNODEDEFS which identify the slave nodes that can connect to the server.
The communication management will be managed through the variables in the data structure. When a client connects, it is searched for in the array indicated by DEDefs il NodeID of the received message and the two systems "bind" to the data exchange, the bit Active will be activated.
To send data to the client system, the bit must be activated TxData, the bit is automatically reset at the end of transmission. If the bit is active AutoTxD upon variation of the data in the Tx buffer, the data is automatically sent to the client. Upon receiving data from a client we will have the bit RxOk active for a loop.
Function lock
CODESYS: Not available
LogicLab: eLLabDataExchLib
Description
Enable (BOOL) Server activation command.
SpyOn (BOOL) If active, it allows you to spy on the operation of the FB.
Port (UINT) TCP port on which to listen to the server.
Connections (UDINT) Number of connections from FB TCPDataExchClient contemporary projects.
DEDefs (@TCPDEXCHNODEDEFS) Address structure array allocation TCPDEXCHNODEDEFS definition of client nodes.
Enabled (BOOL) Function lock enabled.
Fault (BOOL) Active for a loop if management error.
Spy trigger
Se SpyOn active you can use the spy console to check the operation of the FB. There are various levels of triggers.
Trigger levels
trigger | Description |
---|---|
16 00000001 # | Rx: Frame reception sequences. |
16 00000002 # | Tx: Frame transmission sequences. |
16 40000000 # | Er: Execution errors. |
Examples
How to use the examples.
The program manages the data exchange by acting as a server listening to the 10000 port, it is possible to connect up to 2 client devices (see example program in this article) and exchange data between them. Client systems will need to send data identifying themselves with the 1 and 2 node numbers.
LogicLab (Ptp171, ST_TCPDataExchServer)
PROGRAM ST_TCPDataExchServer
VAR
C1RxBuffer : ARRAY[0..7] OF UINT; (* Rx buffer (Client 1) *)
C1TxBuffer : ARRAY[0..7] OF UINT; (* Tx buffer (Client 1) *)
C2RxBuffer : ARRAY[0..7] OF UINT; (* Rx buffer (Client 2) *)
C2TxBuffer : ARRAY[0..7] OF UINT; (* Tx buffer (Client 2) *)
DEDefs : ARRAY[0..1] OF TCPDEXCHNODEDEFS; (* Data exchange node definitions *)
TCPServer : TCPDataExchServer; (* TCP data exchange server *)
END_VAR
// *****************************************************************************
// PROGRAM "ST_TCPDataExchServer"
// *****************************************************************************
// The program instantiates a "TCPDataExchServer" FB. Up to 2 connections are
// accepted.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// Program initializations.
IF (SysFirstLoop) THEN
// Set the client 1 definitions.
DEDefs[0].NodeID:=1; //Node ID
DEDefs[0].AutoTxD:=TRUE; //Automatic Tx data send
DEDefs[0].RxBuffer:=ADR(C1RxBuffer); //Rx buffer address
DEDefs[0].RxLength:=SIZEOF(C1RxBuffer); //Rx buffer length
DEDefs[0].TxBuffer:=ADR(C1TxBuffer); //Tx buffer address
DEDefs[0].TxLength:=SIZEOF(C1TxBuffer); //Tx buffer length
DEDefs[0].TxHeartbeat:=10; //Tx heartbeat time (S)
// Set the client 2 definitions.
DEDefs[1].NodeID:=2; //Node ID
DEDefs[1].AutoTxD:=TRUE; //Automatic Tx data send
DEDefs[1].RxBuffer:=ADR(C2RxBuffer); //Rx buffer address
DEDefs[1].RxLength:=SIZEOF(C2RxBuffer); //Rx buffer length
DEDefs[1].TxBuffer:=ADR(C2TxBuffer); //Tx buffer address
DEDefs[1].TxLength:=SIZEOF(C2TxBuffer); //Tx buffer length
DEDefs[1].TxHeartbeat:=10; //Tx heartbeat time (S)
// Server configuration.
TCPServer.SpyOn:=TRUE; //Spy command
TCPServer.Port:=10000; //Peer port
TCPServer.Connections:=2; //Accepted connections
TCPServer.DEDefs:=ADR(DEDefs); //Data exchange definitions
END_IF;
// -------------------------------------------------------------------------
// DATA EXCHANGE SERVER
// -------------------------------------------------------------------------
// Manage data exchange server.
TCPServer(Enable:=TRUE); //Manage the server
// [End of file]