“With the rapid development of the Internet, while computers are used for network interconnection, various household appliances, instrumentation, and data acquisition and control equipment in industrial production are gradually becoming networked in order to share network resources. Therefore, as Electronic equipment is becoming more and more networked today, it is of great significance to use serial server to realize network communication. Using the realization of TCP/IP-based serial data stream transmission to control and manage the equipment, the traditional equipment can be managed, replaced or upgraded without investing a lot of manpower and material resources.
With the rapid development of the Internet, while computers are used for network interconnection, various household appliances, instrumentation, and data acquisition and control equipment in industrial production are gradually becoming networked in order to share network resources. Therefore, as electronic equipment is becoming more and more networked today, it is of great significance to use serial server to realize network communication. Using the realization of TCP/IP-based serial data stream transmission to control and manage the equipment, the traditional equipment can be managed, replaced or upgraded without investing a lot of manpower and material resources.
Function of serial server
The serial port server is a communication protocol converter. It has two types of communication ports: one is a standard RS232/422/485 format serial port, and the remote RTU (remote terminal equipment) monitoring module is connected through the serial control bus. Enter this type of port of the serial server; the other is an Ethernet port, which connects the serial server to LAN switches and other devices through a network cable.
The serial port server can automatically transparently convert the serial data in RS232/422/485 format and the Ethernet data packet based on TCP/IP protocol during work. On the one hand, the serial server receives field data from a certain serial port, converts it into TCP/IP format data, and uploads it to the network through the Ethernet port; on the other hand, the serial server captures the legal data protocol in the local area network Package, analyze the effective control information by unpacking, and transmit the control command to the remote RTU in the form of serial data through the serial port designated by the monitor.
The hardware platform of this system is shown in Figure 1. It is a Multibus-CPU development board built with AT91RM9200 chip. The MultiBus-CPU system is an intelligent multi-bus measurement and control system based on the AT91RM9200 microcontroller. The system can establish bus communication based on the Modbus-RTU bus protocol, so that the system equipment can be seamlessly connected to the bus system based on the Modbus-RTU mode , And reliable, real-time and accurate completion of industrial field data acquisition and signal output functions. The system supports Modbus-RTU protocol. At the same time, the system also supports multiple bus communication, including RS485 bus, industrial Ethernet (UDP protocol), serial RS232 communication interface and USB communication interface.
Figure 1: Schematic diagram of the CPU module.
This design uses embedded Linux as the operating system. The main advantages of Linux on ARM: a scalable and complete operating system provides a reliable multitasking environment, based on the open source model (GPL), the use of a variety of UNIX and open source applications, and for a variety of designs based on ARM technology (including network And wireless fields).
Modbus protocol is a communication language applied to electronic controllers. Using this protocol, the controllers communicate with each other and between the controllers and other devices through a network (such as Ethernet).
The mainstream Modbus protocols are the standard Modbus protocol (Modbus RTU protocol) and Modbus TCP protocol. The standard Modbus communication protocol defines each byte of a message frame, defines how to pack data into a message frame and how to decode it. The message frame includes one byte of address code, one byte of function code, data and two bytes of check code. Among them, the check code adopts CRC check.
The Modbus TCP protocol is further developed on the basis of the standard Modbus protocol. It is constructed by embedding the Modbus protocol into the underlying TCP/IP protocol, so that the Modbus message communication of the client-server architecture is realized on the TCP/IP Ethernet. The data frame structure of the two is shown in Figure 2.
Figure 2: Comparison of standard Modbus and Modbus_TCP data frame structure.
Analyzing Modbus TCP protocol and RTU protocol, we can clearly see the main difference between the two. Compared with the Modbus RTU protocol, there is no CRC check in the Modbus TCP data frame anymore, and this part of the check task is completed by the TCP/IP protocol and the link layer of the Ethernet. In addition, the more standard Modbus protocol of Modbus TCP also adds a MBAP header to explain the parameters and functions of Modbus. The other parts can be used interchangeably. If the TCP protocol is converted to the RTU protocol, then only the “unit identification field” in the MBAP header of the TCP protocol and the subsequent bytes form a frame, and the CRC check of this frame can form the RTU protocol. Send on the uplink. If the RTU protocol is converted to the TCP protocol, then an MBAP header should be formed according to the actual situation.
Design and implementation of GetConfigValue function to obtain configuration information
Before designing the serial server, you must first configure the corresponding device number and configure the device with the corresponding serial server IP address, TCP communication port number, and serial parameters. These configuration information are placed in a txt file.
We have designed a Configinfo.txt file. When we need to transmit information to a device, we need to read the configuration information from the file first, configure the corresponding serial port and the network of the industrial control board, and then perform the corresponding operations. At this time, we used two custom functions in it: GetConfigValue and GetCFGValue. The function of GetConfigValue is to configure the information in Configinfo.txt to the serial server. It calls the GetCFGValue function and looks for the corresponding configuration information in Configinfo.txt Item, and intercept the corresponding configuration information. Their block diagrams are shown in Figure 3 (a) and 3 (b).
Figure 3: GetConfigValue function program flow chart (a) and GetCFGValue function program flow chart (b).
The design and realization of serial port operation function encapsulation
When performing corresponding operations on the serial port, first open the serial port and configure the baud rate, data bit, stop bit, and working mode of the serial port. These operations on the serial port are encapsulated in a file to make the structure clear, easy to check and modify or add more operation functions. The following operations are carried out.
Opening and closing of the serial port: when opening the serial port, you need to first determine the type of the serial port, then open the serial port in a certain way and save the original serial port configuration information, and finally perform the baud rate, data bit, stop bit and parity bit of the serial port setting. When closing the serial port, you need to restore the previously saved terminal information to make the serial port back to the state before it was opened.
Read and write data to the serial port: The read and write operation of the serial port is the most basic function of the serial server. When data is transmitted, it needs to be sent through the serial port. The serial port write operation will write the data in the memory to the pointed file, and the serial read operation will write the data sent from the serial port into the memory. Respectively use writecomport and readcomport to achieve.
Set the serial port: the baud rate, data bit, stop bit and parity bit need to be set. Call the defined encapsulation function to complete these operations. The working mode of the serial port is controlled by the configuration of the termios function.
Realization of Modbus TCP/RTU Mutual Conversion Function
This step is the key to the design of the serial server, and its conversion focuses on the CRC check and MBPA header conversion. Here, define the mod2tcp function to complete the conversion from Modbus RTU to TCP format, and define the tcp2mod function to complete the data conversion from Modbus TCP to RTU format. For example, the TCP protocol request from the Modbus master station is converted into Modbus RTU format by the serial server, and sent to the slave station via the 485 port, and the corresponding data from the slave station is sent to the serial server via the 485 interface, converted into Modbus TCP format information and returned to the master Station (Figure 4).
Figure 4: Modbus TCP/RTU mutual conversion example.
The program flow is shown in Figure 5(a) and 5(b).
Figure 5: Modbus RTU to TCP format data conversion program flow (a) and Modbus TCP to RTU format data conversion program flow (b).
It can be seen that the number of bytes in the TCP format = the number of bytes in the RTU format-2+6, and the available data obtained are all the same, so the program implementation is no longer a problem.
CRC check function
CRC stands for Cyclic Redundancy Check Code, which is the most commonly used error check code in the field of data communication. In RTU mode, CRC checks all the data transmitted, it ignores the parity check method of single character data in the message. The key of CRC check lies in data matching, and how to correctly apply the obtained data to CRC check is the key.
There are established rules for CRC verification, and its program implementation is very simple. It only requires the following steps: initialization of CRC bytes; import data for CRC calculation; return the calculated CRC value.
Design and Implementation of Serial Port Server
This part introduces the main() function of the entire program. It will call the functions introduced in the previous section to realize the serial port server function, so the header file should include the function files described in the previous section. The program flow is shown in Figure 6.
The main() function should be programmed with Linux C function. Special attention should be paid to the application of the socket. The socket must be established first and then bind(); the parameters in the semaphore must be configured correctly; in the management of the process, pay attention to the exit child when the process exits. Process, close the socket after all processes are completed.
Figure 6: The main () function program flow chart of the serial server.
Problem and solution
During the serial port server test, sometimes there will be a data packet error when sending from TCP to RTU, and TCP data cannot be converted to RTU data correctly.
The reason is that during the test, the PC terminal under Windows sends data to the Linux industrial control board, but the real-time performance of Windows is not very good, and the data cannot be sent in time. Sometimes there will be a phenomenon of sending two packets of data to the serial server at a time; , The TCP/IP protocol tailoring and encapsulation under Linux in the industrial control board is not perfect, resulting in its poor ability to receive and process data.
When multiple frames of data arrive at the same time, the serial server fails to send the next frame of data correctly, and divides one frame of data into two frames, resulting in a data frame error. This is why the above data is completely wrong. At this time, you need to add data framing processing function to the serial server. When receiving data, first determine whether the data frame is a valid data frame to ensure that the data sent each time can be a complete frame, and no data frame will appear. Misalignment leads to complete data errors.
Summary of this article
The goal of this article is to implement the serial server function on the Multibus-CPU development board built with the AT91RM9200 chip. The serial server uses Modbus related protocols to connect traditional RS485/232 serial communication equipment to the industrial Ethernet to realize the information exchange between the host computer and the equipment. The serial server program has been able to run correctly on the development board.