Introduction
TCP
TCP is a connection-oriented analog protocol that can be compared to a phone connection, where participants have to establish the connection first. The TCP protocol is used in applications where confirmation is required for the data sent by the client or server. Data integrity is managed by the protocol, which requires more resources. The TCP protocol is well suited for sending larger data quantities.
TCP is a stream-oriented transport protocol, i.e. it transports a data stream without defined start and end. No information about length, start and end of a message is transferred. For the transmitter this is not a problem since he knows how many data bytes are transmitted. However, the receiver is unable to detect where a message ends within the data stream and where the next data stream starts. A read call on the receiver side only supplies the data currently in the receive buffer (this may be less or more than the data block sent by the other device).
The transmitter has to specify a message structure that is known to the receiver and can be interpreted. In simple cases the message structure may consist of the data and a final control character (e.g. carriage return). The final control character indicates the end of a message.
The message structure for transferring binary data with variable length is often specified as follows: The first data bytes contain a special control character (start delimiter) and the data length of the subsequent data. This enables the receiver to detect the start and end of the message.
A minimum TCP/IP client implementation within the PLC requires the following function blocks:
- An instance of the FB_SocketConnect and FB_SocketClose function blocks for establishing and closing the connection to the remote server (Hint: FB_ClientServerConnection encapsulates the functionality of both function blocks in one function block);
- An instance of the FB_SocketSend and/or FB_SocketReceive function block for the data exchange with the remote server;
A minimum TCP/IP server implementation within the PLC requires the following function blocks:
- An instance of the FB_SocketListen function block for opening the listener socket. An instance of the FB_SocketAccept and FB_SocketClose function blocks for establishing and closing the connection(s) to the remote clients (Hint: FB_ServerClientConnection encapsulates the functionality of all three function block in one function block);
- An instance of the FB_SocketSend and/or FB_SocketReceive function block for the data exchange with the remote clients;
An instance of the FB_SocketCloseAll function block is required in each PLC runtime system in which a socket is opened.
The instances of the FB_SocketAccept and FB_SocketReceive function blocks are called cyclically (polling), all others are called as required.
UDP
UDP is a connection-less protocol. Data are sent between devices without an explicit connection. The UDP protocol is well suited for sending small data quantities. An UDP application can be both a client or a server. The UDP protocol does not guarantee that data that were sent actually reach the target (no confirmation for the packets received is sent). The individual data packets may arrive in a different order or may be lost.
UDP is a packet-oriented/message-oriented transport protocol, i.e. the sent data block is received on the receiver side as a complete data block.
The following function blocks are required for a minimum UDP server/client implementation:
- An instance of the FB_SocketUdpCreate and FB_SocketClose function blocks for opening and closing an UDP socket;
- An instance of the FB_SocketUdpSendTo and/or FB_SocketUdpReceiveFrom function blocks for the data exchange with other devices;
- In each PLC runtime system in which a UDP socket is opened an instance of the FB_SocketCloseAll function block is required;
The instances of the FB_SocketUdpReceiveFrom function block are called cyclically (polling), all others are called as required.
Further information can be found on the following documentation pages.
Glossary
Term | Description |
---|---|
TwinCAT TCP/IP Connection Server | A TwinCAT Server that enables opening, closing, sending and receiving of data via the Windows sockets. |
Remote-Client | A client on a remote computer (from a server point of view) with which the server wants to communicate. |
Remote-Server | A server on a remote computer (from a client point of view) with which the client wants to communicate. |
Local-Client | A client on the local computer. |
Local-Server | A server on the local computer. |
Connection handle (socket handle) | A PLC variable of type T_HSOCKET |