In recent years, due to the needs of the development of information technology and the Internet of things, a large number of serial port servers have emerged. They do not occupy host resources and have the function of terminal server. However, problems such as large volume, high price, difficult cutting or expansion of serial port, packet loss when transmitting a large number of data frames, complex parameter configuration and so on also appear.
Raspberry pie, the core equipment of the serial server used in this design, is a small, cheap but powerful platform. It can access the Internet through a variety of communication modes and support a variety of complete network protocols. Combined with USB hub and USB / serial converter, it can ensure the convenient use of the serial server and realize real-time, accurate and long-term stable data transmission.
1 overall design
According to today's industrial field data transmission requirements, the technical indicators and setting parameters should be as follows.
1.1 technical indicators
Number of sessions: multiple connections are supported to meet the requirements that less than 5 users manage one module device at the same time;
Working mode: TCP client, TCP server and UDP broadcast mode;
Indicator light: power indicator light and communication indicator light;
1.2 setting parameters
The device selects the web browser setting mode and has a built-in web server. Users can manage and configure IP address, serial communication parameters, working mode, etc. The following mainly introduces the setting of serial communication and working mode parameters.
(1) Serial communication parameters
The baud rate is 2400 115 200 BPS;
The data bits are 6 / 7 / 8 / 9;
The check bit is none / even / odd;
The stop bit is 1 / 1.5/2;
The framing length is 1 15000 bytes;
The framing interval is 30 500 bits.
Wherein, the frame length refers to the length of the received data of each frame, and once the receiving end buffer receives the data of this length, it will be forwarded; Framing interval is the so-called serial port timeout. The relationship between timeout time t (s), framing interval n (bit) and baud rate B (B / s) is as follows. Once the timeout occurs, whether the length of the existing data reaches the frame length or not, the receiving end buffer forwards the existing data group frame.
T= N/B (1)
(2) Operating mode parameters
A single serial port supports 1 5 sessions, and each session mode is independent of each other;
The TCP client mode is to configure the IP, port and automatic reconnection time of the target server. The automatic reconnection time refers to the time interval of re initiating the active connection when the network connection is interrupted or the server is abnormal, and the automatic reconnection cycle until the normal connection is made;
UDP broadcast mode is to configure local port, target server IP and target port.
2 hardware design
According to the above indicators, the serial server is composed of the latest raspberry pie, USB hub and USB / four serial port converter produced by Beijing Century Lianxin company.
2.1 hardware structure composition
The hardware structure of serial server is shown in Figure 1. Raspberry pie has two USB interfaces, a TTL serial port and a 10 / 100 MHz adaptive Ethernet interface. One USB interface expands 4N RS232 / RS422 / rs455 serial ports through USB hub and n (optional, n = 4 at most) USB / serial port converters, and the other can be connected with wireless USB network card to realize wired or wireless data transmission in LAN or WAN.
Figure 1 hardware structure block diagram
2.2 introduction to raspberry pie
Raspberry Pi is a single board computer with the size of only one credit card based on Linux system. It is equipped with a 700 MHz arm architecture bcm2835 processor produced by Broadcom, 512 MB memory, SD card as storage medium, open source Linux system as operating system, and python as the main programming language.
2.3 introduction to USB hub
USB hub (USB hub) is a USB interface that connects multiple USB devices to a computer or another USB hub. At some time, they all come from one or two main USB interfaces inside the motherboard, rather than independent hardware. A USB interface, a USB hub and several peripherals can build a USB network. The USB hub selected by the system is expanded from one USB interface to four USB interfaces.
2.4 introduction to USB / serial port converter
The USB / serial port converter adopts Beijing Century Lianxin lensys-usb2com-4 module. It is a rail mounted serial port expansion module, which realizes the function of expanding 4 serial ports with USB port. It can quickly expand 4 computer serial ports through USB, set the RS 232 / 422 / 485 interface mode through the dial switch, and realize the function of 4 RS 232 to 4 RS422 / 485. The module can reliably, real-time and conveniently complete the transmission and conversion of RS232 / 485 / 422 signals in industrial field. It is widely used in oil and gas, water conservancy, power dispatching, municipal dispatching and other industries.
3 software design
The basic structure of the serial server software design is shown in Figure 2. The client is only a web browser, and the software design is mainly reflected in the web page display, business logic and data storage of the server.
The web server is responsible for the manual configuration and storage of the communication parameters of the serial server, the manual start and stop of the communication program, etc; The application server is responsible for realizing the interworking between TCP / IP network and serial interface equipment, and completing the mutual conversion between TCP / IP protocol format data and serial data; The database mainly includes user management data table, serial port parameter configuration data table and working mode configuration data table.
Fig. 2 basic structure of software design
The following introduces three main tasks in the software design of serial port server: the design of web server, the design of web configuration page and the design of serial port networking communication program.
3.1 design of web server
Due to the large number of serial ports, a web server is embedded in the raspberry pie. The web server converts the user's request into the query or update of background data, and displays the friendly graphical interface to the user on the browser, so as to realize simple and unified remote communication parameter configuration, equipment management, user management and monitoring the operation status of the serial port server.
The serial port networking server selects the python provided by raspberry pie as the development language and adopts the small and flexible web.py development framework to provide HTTP services. Its lightweight meets the requirements of the acquisition system for data transmission module and can improve the use efficiency of the system. The design flow of web server is shown in Figure 3.
Figure 3 design flow of web server
After the equipment is powered on or the system is restarted, the web service starts automatically and initializes the information to be displayed and configured by the web server. The user can access the parameter configuration page by accessing the specified port of the system through the browser for login authentication. Among them, the device information and network information are viewed and configured through the system file manager, the user information, serial port parameters and working mode information are read and written through the database, and the help information is downloaded from the serial port server instruction manual, company service information and other documents through the FTP server.
3.2 web configuration page design
The web configuration page is the display layer of the web server, as shown in Figure 4. The number of serial ports scanned indicates that the serial server has 16 serial ports, and each serial port configuration is independent of each other.
Figure 4 web configuration interface
Since the configuration program is designed to associate the working mode configuration with the serial port parameter configuration, if the first three serial ports are selected, only the working mode configuration interface of these three serial ports is displayed in the working mode configuration, as shown in Figure 5.
Figure 5 working mode configuration interface
The above configuration methods can facilitate batch configuration. After configuration, a configuration information viewing interface can be generated, as shown in Figure 6. This interface can also edit and delete the communication parameters and working mode of the selected serial port.
Figure 6 configuration information viewing interface
3.3 serial port networking communication program design
After the web server completes the configuration, it can start the communication program for data communication. The specific implementation process is shown in Figure 7. This program starts automatically after the equipment is powered on or the system is restarted. After reading the serial port communication and working mode parameters configured by the user, it enters the infinite cycle of the communication program. The number of processes in the figure is the total number of serial ports configured by the field application. Where n is the number of serial ports of the serial port server, and m (m w) is the number of serial ports used in n serial ports; N is the respective number, and I is the process number; S is the number of sessions configured, and S is the thread number.
Figure 7 work flow of serial server communication program
In the three working modes, the TCP server and the TCP client belong to the same TCP protocol transmission program, and their data frame sending and receiving process adopts the same idea. The following describes the design of TCP, UDP protocol transmission program and data frame forwarding program:
3.3.1 top protocol transmission program design
TCP protocol transmission program involves the design of server and client.
Server side programming:
Establish a socket, select INET type and TCP connection mode;
Read the configuration port for binding and listening, and wait for the client to actively connect;
Set the listening queue size;
Enter an infinite loop, use accept() to wait for the client connection, and the returned new connection corresponds to the client IP to establish the communication channel;
Enter the infinite sub loop and read and write through sendall() and recv().
Client programming:
Establish a socket, and the selection type and connection mode are the same as that of the server;
Read the configured remote server IP and port;
Enter the infinite loop and use connect() to connect to the remote server. If not, reconnect again after reaching the configured timeout interval;
After the connection is successful, it enters the infinite sub loop and reads and writes through sendall() and recv().
3.3.2 UDP protocol transmission program design
UDP transmission is called connectionless transmission. There is no triple handshake and error retransmission mechanism in TCP. Its transmission program needs to read the configured local IP and port, remote IP and port at the same time. After establishing a socket in the form of datagram, it can be used as the sender and receiver at the same time. As the sending end, sendto() sends IP and port of the remote receiving end. As the receiving end, it exposes the local IP and port. Recvfrom() waits for the data from the remote sending end and can receive data packets sent from any address.
3.3.3 data frame forwarding program design
The serial server function module realizes the conversion between serial link data and Ethernet data: on the one hand, it receives the data frame from the serial link, converts it into Ethernet link data frame and sends it out; On the other hand, it receives the data frame from the Ethernet link, converts it into a serial link data frame and sends it. The above two communication processes are executed in parallel and implemented by multithreading in program design.
(1) Receiving of network data
In each communication mode, the receiving of network data is realized in the specified callback function. The receiving function of TCP communication is recv (), and that of UDP communication is recvfrom (). When the data frame length accumulates to the specified reception