Serial server function
The serial port server is a communication protocol converter, which has two types of communication ports: one is a standard serial port in RS232 / 422/485 format, and the remote RTU (remote terminal equipment) monitoring module is connected through a serial control bus This type of port enters the serial port server; the other type is the Ethernet port, which connects the serial port server to the LAN switch and other devices through the network cable.
The serial port server can automatically convert serial data in RS232 / 422/485 format and Ethernet data packets based on TCP / IP protocol during work. On the one hand, the serial server receives the live data from a serial port, converts it into TCP / IP format data, and uploads the network through the Ethernet port; on the other hand, the serial server captures the legal data protocol in the LAN Packet, through unpacking to parse out the effective control information, through the serial port designated by the monitoring machine to transmit control commands to the remote RTU in the form of serial data.
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 a 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 collection and signal output functions. The system supports Modbus-RTU protocol. At the same time, the system also supports a variety of bus communications, 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: The scalable and complete operating system provides a reliable multi-tasking environment, based on the open source model (GPL), using a variety of UNIX and open source applications and multiple designs based on ARM technology (including networking And wireless fields).
Modbus protocol is a communication language used in electronic controllers. With this protocol, the controllers communicate with each other and other devices through the network (such as Ethernet).
The mainstream Modbus protocol is the standard Modbus protocol (Modbus RTU protocol) and Modbus TCP protocol. The standard Modbus communication protocol defines each byte of the message frame, and defines how to pack the data into a message frame and how to decode it. The message frame includes one byte address code, one byte function code, data and two bytes check code. Among them, the check code uses CRC check.
Modbus TCP protocol is further developed on the basis of standard Modbus protocol. It is formed by embedding Modbus protocol into the underlying TCP / IP protocol, so that Modbus message communication of client-server architecture is realized on TCP / IP Ethernet. The data frame structure of the two is shown in Figure 2.
Figure 2: Standard Modbus and Modbus_TCP data frame structure comparison.
Analysis of Modbus TCP protocol and RTU protocol can clearly see the main difference between the two. Compared with Modbus RTU protocol, Modbus TCP data frame no longer has CRC check, and this part of check task is completed by TCP / IP protocol and Ethernet link layer. In addition, Modbus TCP's more standard Modbus protocol also adds an MBAP header, which explains and explains Modbus parameters and functions. 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. On the link. 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 for obtaining configuration information
Before designing a serial server, you must first configure the corresponding device number and configure the corresponding serial server IP address, TCP communication port number, and serial port parameters for the device. These configuration information are placed in a txt file.
We designed a Configinfo.txt file. When we need to transfer information to a device, we need to first read the configuration information from the file, configure the corresponding serial port and the network of the industrial control board, and then perform the corresponding operation. At this time, we used two custom functions: GetConfigValue and GetCFGValue. The function of GetConfigValue is to configure the information in Configinfo.txt to the serial server. It calls the GetCFGValue function, finds the corresponding configuration information item in Configinfo.txt, and intercepts the corresponding configuration information. Their block diagrams are shown in Figures 3 (a) and 3 (b).
Figure 3: GetConfigValue function program flowchart (a) and GetCFGValue function program flowchart (b).
Design and Implementation of Serial Port Operation Function Packaging
When performing corresponding operations on the serial port, first open the serial port and configure the serial port's baud rate, data bit, stop bit, and working mode. 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 operations performed include the following operations.
Open and close 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 on the serial port setting. When closing the serial port, you need to restore the previously saved terminal information to return the serial port to the state before opening.
Read and write data to the serial port: The serial port read and write operation is the most basic function of the serial port 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 file pointed to, and the serial port read operation will write the data sent from the serial port to the memory. Use writecomport and readcomport respectively.
Set the serial port: you need to set the baud rate, data bit, stop bit and parity bit. 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.
Implementation 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 the conversion of the MBPA header. 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 the Modbus RTU format through the serial server, sent to the slave station through the 485 port, and the corresponding data of the slave station is sent to the serial server through the 485 interface, converted into Modbus TCP format information and returned to the master Station (Figure 4).
Figure 4: Modbus TCP / RTU conversion example.
Network Cabinet,Standing Network Cabinet,Floor Standing Data Cabinet,Free Standing Server Cabinet
Ningbo JingCheng Communication Equipment Co., Ltd. , https://www.jingchengtel.com