TCPComm
TCPComm Library
The TCPComm library is a wrapper for the AsTCP library provided by B&R. This library manages and simplifies the TCP communication interface.
TCPComm Data Structures and Enums
Mode Enums
- TCPCOMM_MODE_SERVER - Set CPU as the TCP communication server.
- TCPCOMM_MODE_CLIENT - Set CPU as the TCP communication client.
State Enums
- TCPCOMM_ST_CLOSED - TCP state after a connection has been successfully closed.
- TCPCOMM_ST_OPENING - TCP state while connection is being established.
- TCPCOMM_ST_IOCTL - TCP state that changes or retrieves settings.
- TCPCOMM_ST_LISTEN - TCP state while connection is established and idle.
- TCPCOMM_ST_CLOSING - TCP state while connection is being closed.
- TCPCOMM_ST_ERROR - TCP state when an error has occurred.
Send State Enums
- TCPCOMM_SEND_ST_IDLE - TCP send state while connection is not actively sending.
- TCPCOMM_SEND_ST_SEND - TCP send state while connection is actively sending.
Receive State Enums
- TCPCOMM_RECV_ST_READ - TCP read state.
TCPConnection_typ
This structure is used to establish and monitor a TCP connection.
Commands
- Enable - Boolean that enables the TCP connection.
- AcknowledgeConnection - Boolean that commands the function to initiate a connection.
- AcknowledgeError - Boolean that clears a non-persistent error.
Configuration
- Mode - Enum that determines whether the CPU will be a TCP client or server.
- LocalIPAddress - IP Address of the CPU.
- LocalPort - Port of the CPU.
- RemoteIPAddress - IP Address of device intended to connect to CPU.
- RemotePort - Port of device intended to connect to CPU.
- SendBufferSize - Maximum length of data in bytes (length of the data buffer specified).
- UseSSL - Boolean used to signify an SSL connection is required.
- SSLCertificate - Storage for required SSL certificate information.
Outputs
- NewConnectionAvailable - Boolean signifying a new connection is available and ready to be acknowledged.
- Connection - Data associated with current connection.
- IPAddress - IP Address of connected device.
- Port - Port of connected device.
- Ident - Socket ident.
- UID - Unique identifier for the connection. 0 implies the connection is not valid.
- Error - Boolean used to signify an error has occurred.
- ErrorID - Error ID associated with the error that has occurred.
- ErrorString - Error string to inform the user about the error that has occurred.
Initialization
//Set the IP Address and port of server
TCPServer.IN.CFG.RemoteIPAddress:= '192.168.10.1';
TCPServer.IN.CFG.RemotePort:= 8000;
//Set CPU's IP Address and port
TCPServer.IN.CFG.LocalIPAddress:= '192.168.10.2';
TCPServer.IN.CFG.LocalPort:= 8001;
//Set the mode to be either Client or Server
TCPServer.IN.CFG.Mode:= TCPCOMM_MODE_CLIENT;
TCPServer.IN.CMD.Enable:= 1;
Cyclic Operation
//cyclically called function
TCPManageConnection(TCPServer);
//Acknowledge a new connection
IF( TCPServer.OUT.NewConnectionAvailable)THEN
TCPServer.IN.CMD.AcknowledgeConnection:= 1;
END_IF
TCPStream_typ
This structure is used to send and receive data via TCP communication.
Commands
- Receive - Command for receiving a new TCP packet.
- Send - Command for sending a new TCP packet.
- Close - Command to close the current connection.
- AcknowledgeData - Command to acknowledge new data has been received.
- AcknowledgeError - Command to acknowledge an error has occurred.
Parameters
- Connection - Data associated with current connection.
- IPAddress - IP Address of connected device.
- Port - Port of connected device.
- Ident - Socket ident.
- pReceiveData - Pointer to string containing received TCP packet.
- MaxReceiveLength - Maximum length of data in bytes (length of the data buffer specified).
- ReceiveFlags - Flag parameter associated with receiving a TCP packet. See Automation Studio TCPRecv() for list of flags.
- AllowContinuosReceive - Boolean that allows for continuously receiving TCP packets.
- pSendData - Pointer to string containing TCP packet being sent.
- SendLength - Length of the SendData string.
- SendFlags - Flag parameter associated with sending a TCP packet. See Automation Studio TCPSend() for list of flags.
- AllowContinuosSend - Boolean that allows for continuously sending TCP packets.
Outputs
- Connection - Data associated with current connection.
- IPAddress - IP Address of connected device.
- Port - Port of connected device.
- Ident - Socket ident.
- Active - Boolean signifying an active connection.
- Receiving - Boolean signifying a TCP received packet is being processed.
- DataReceived - Boolean signifying a TCP received packet has been processed.
- ReceivedDataLength - Length of the TCP packet last received.
- Sending - Boolean signifying a TCP packet is currently being sent.
- DataSent - Boolean signifying a TCP packet was successfully sent.
- SentDataLength - Length of the most recently sent TCP packet.
- Error - Boolean used to signify an error has occurred while sending or receiving TCP packets.
- ErrorID - Error ID associated with the error that has occurred.
- ErrorString - Error string to inform the user about the error that has occurred.
Initialization
//set the send data string to avoid sending garbage
sendData:= 'Hello Server';
Cyclic Operation
//When a new connection is available set the pointers for the send and receive strings and set the commands high
IF( TCPServer.OUT.NewConnectionAvailable )THEN
TCPServer.IN.CMD.AcknowledgeConnection:= 1;
TCPDataManager.IN.PAR.Connection:= TCPServer.OUT.Connection;
TCPDataManager.IN.PAR.pSendData:= ADR(sendData);
TCPDataManager.IN.PAR.pReceiveData:= ADR(recvData);
TCPDataManager.IN.PAR.MaxReceiveLength:= SIZEOF(recvData);
TCPDataManager.IN.CMD.Receive:= TRUE;
TCPDataManager.IN.CMD.Send:= TRUE;
END_IF
//Acknowledge new connection and cyclically check for received data
TCPDataManager.IN.CMD.AcknowledgeData:= 1;
TCPStreamReceive(TCPDataManager);
//When not sending, recalculate the length of the data to be sent.
IF( NOT TCPDataManager.OUT.Sending ) THEN
TCPDataManager.IN.PAR.SendLength:= brsstrlen(ADR(sendData));
END_IF
//Function that performs the TCP sending functionality when the send command is high
TCPStreamSend(TCPDataManager);
//Clean up commands for next cycle
TCPDataManager.IN.CMD.Send:= 0;
TCPDataManager.IN.CMD.Close:= 0;
Snippets
Disconnect handling
// Handle a disconnect error
if (TCPDataManager.OUT.ErrorID == tcpERR_NOT_CONNECTED) {
TCPDataManager.IN.CMD.Send = 0;
TCPDataManager.IN.CMD.Receive = 0;
TCPDataManager.IN.CMD.Close = 1;
TCPDataManager.IN.CMD.AcknowledgeError = 1;
}
Note: In TCP, an empty packet also indicates a disconnect.
if (TCPDataManager.OUT.DataReceived) {
if (TCPDataManager.OUT.ReceivedDataLength == 0) {
TCPDataManager.IN.CMD.Send = 0;
TCPDataManager.IN.CMD.Receive = 0;
TCPDataManager.IN.CMD.Close = 1;
}
}
Note: If running in Client mode, after a disconnect the Client FUB needs to see a rising edge of enable to attempt to reconnect. Setting Enable false on Client while connected will close the connection.
if(TCPDataManager.IN.CMD.Close) {
TCPClient.IN.CMD.Enable := 0; // TCP Client requires a toggle on enable to reconnect
}
Note: The Close command is reset in the Send and Receive functions, so this code must run before those calls.
or
// On falling edge of Active
if(prevActive && !TCPDataManager.OUT.Active) {
TCPClient.IN.CMD.Enable := 0; // TCP Client requires a toggle on enable to reconnect
}
Note: After closing a connection, the underlying send buffer will finish sending before the socket fully closes. Immediately reopening the connection can cause memory issues. If immediate reconnection is desirable, consider disabling lingering. See TcpIoctl() with the tcpSO_LINGER_SET ioctl.
Handling config changes
// Handle user changes in Configuration.Connection
IF brsmemcmp(ADR(TCPClient.IN.CFG),ADR(Configuration.Connection),SIZEOF(Configuration.Connection)) <> 0 THEN
TCPClient.IN.CFG:= Configuration.Connection;
TCPClient.IN.CMD.Enable:= FALSE; // Client (or server) needs to restart to accept configuration
END_IF
Keep alive
// Temp variable
sending := TCPDataManager.OUT.Sending OR TCPDataManager.IN.CMD.Send; // Is data being sent or about to be sent
Ping.IN:= connected AND NOT sending AND NOT Ping.Q;
Ping.PT:= Configuration.Ping;
Ping(); // Ping TON
IF Ping.Q AND Configuration.Ping > 0 THEN
FormattedData := 'ok?'; // Ping message
TCPDataManager.IN.PAR.SendLength := 3; // Ping message len (strlen('ok?') == 3)
TCPDataManager.IN.CMD.Send := TRUE;
TCPDataManager.IN.PAR.pSendData := ADR(FormattedData);
END_IF
Errors
- 50000 - TCPCOMM_ERR_INVALIDINPUT
- 50001 - TCPCOMM_ERR_SENDDATATOOLARGE
- 50002 - TCPCOMM_ERR_