Key technology of serial to Ethernet: work mode of TCP/IP

Key technology of serial to Ethernet: work mode of TCP/IP:

Serial to Ethernet, it is not a simple physical layer and data link layer transformation. Since the serial port protocol itself does not have a network layer and a transmission layer, serial to Ethernet, it is that the serial port data is used as the application layer data of TCP/IP, and the TCP/IP encapsulation transmission method is used. The application layer data of TCP/IP is really valid data that TCP/IP transmits. For example, the user actually receives and sends the application layer data through the socket's recv() and send() functions. In this way, through serial to TCP IP, users can use the recv() and send() functions to send and receive serial data.


However, TCP/IP is not just recv() and send(). According to different work modes, it is related to connection, shutdown, monitoring, etc. this is the part that needs to be added after the serial port is converted to the network port. The work mode of TCP IP can be divided into: TCP server mode (TCP Server), TCP client mode (TCP Client), UDP mode.


UDP mode: UDP mode is a mode based on non-connection, as long as there is data to send, no need to connect in advance. So this mode is closer to the serial port communication method. However, the UDP protocol cannot guarantee that data is not lost and prone to error.


TCP mode: TCP mode uses a reliable data transmission mechanism, so it can ensure that the data  basically has no error code and it is not lost. In TCP communication, it must be composed of two ends of communication, one of which is a TCP client and one of which is a TCP server. The concept of TCP client and TCP server can be analogized by phone. The TCP client is the caller, and the TCP server is the person who answers the call.


How to select TCP/IP work mode?

1. TCP and UDP selection: try to select TCP mode, especially through the large amount of data transmission via the Internet, udp is easy to have error code and loss.

2. Whether to select TCP client or TCP server, please follow the rules below:

First rule: The party that initiates the data transmission should choose to be the client. For example, a data acquisition system, the collection terminal should be the client. This is because when the TCP connection is broken, the client can actively establish a connection when it needs to send data. The TCP server can only passively accept the connection, so that the data cannot be sent out.

Second rule: The party with IP or fixed domain name is the server side. For example, under the condition that there are several multiple data collection terminals and only one central server, the central server should be the server. This is because the IP or domain name of the central server is generally fixed, while the IP of the collection terminal is constantly increasing and changing. It is difficult for the central server to remember the IP of all the collection terminals, so it is difficult to initiate the connection; it is easier for the collection terminal to find the central server.