“The system uses STM32F103 as the main control unit, uses the RS485 communication bus, and develops a set of communication protocols to ensure the integrity of the bus arbitration mechanism from the software perspective and the normal operation of the measurement and control network. In the process of system design discussion, according to the characteristics of RS485 communication bus, the principle and process of multi-computer communication design in the system are discussed, combined with the problems encountered in the system debugging process, the precautions in the process of using RS485 are put forward.
Introduction: The system uses STM32F103 as the main control unit, uses the RS485 communication bus, and develops a set of communication protocols to ensure the integrity of the bus arbitration mechanism from the software perspective and the normal operation of the measurement and control network. In the process of system design discussion, according to the characteristics of RS485 communication bus, the principle and process of multi-computer communication design in the system are discussed, combined with the problems encountered in the system debugging process, the precautions in the process of using RS485 are put forward.
With the development of embedded technology, measurement and control equipment is gradually becoming intelligent. A necessary condition for enterprise informatization requirements in equipment selection is that the equipment has a networked communication interface. The RS232 interface can realize point-to-point communication, but cannot realize the networking function, and the long-distance transmission performance is poor in anti-interference. The RS485 interface solves this problem. The maximum data transmission rate can reach 10 Mbps, and the interface adopts a combination of a balanced driver and a differential receiver. It has strong anti-common-mode interference capability and good noise immunity, and is suitable for long-distance transmission. The RS485 bus can support a maximum of 32 nodes, and a special chip can support a maximum of 400 nodes. Therefore, RS485 is very suitable for the communication field of distributed measurement and control systems. But RS485 can only represent the physical medium of communication, and corresponding programs must be developed for data transmission and access.
CORTEX-M3 is a processor core based on the ARM7 architecture and adopts the Harvard architecture, which has low power consumption and high cost performance. In the design of multi-machine communication network, after demand analysis, a multi-machine communication network based on CORTEX-M3 is proposed and designed. In the design, CORTEX-M3 is used as the system terminal, as the master and slave. The slave is responsible for various data measurements of the front-end system, and the master is the control terminal, responsible for the collection of measurement information from each slave, and realizes the control of the slave through the network. In the design of the system, the network transmission interface adopts RS485. According to the characteristics of the communication network, the RS485 multi-machine communication protocol is self-made in the design, and the communication interface program based on the CORTEX-M3 processor is developed. The program has good portability and can be transplanted to CORTEX― In the communication network of the M3 core. After testing, the system communication performance is good, no data loss and network deadlock phenomenon.
1 Introduction to RS485
The RS-485 standard was created to make up for the shortcomings of RS-232 communication such as short communication distance and poor anti-interference performance of long-distance communication. The RS-485 standard only specifies the electrical characteristics of a balanced transmitter and receiver, but does not specify connectors, transmission cables, and application layer communication protocols.
The RS-485 standard is different from RS-232. The data signal adopts a differential transmission method, also called balanced transmission. It uses a pair of twisted pairs, one of which is defined as A and the other is defined as B, as shown in Figure 1. .
In Figure 2, the high level between A and B in the RS485 transmitter is between +2～+6 V, indicating a positive logic state; the low level is between -2 and -6 V, indicating a negative logic state. For the RS485 receiving end, when there is a voltage greater than +200 mV between A and B, the input is a positive logic level; if it is less than -200 mV, the input is a negative logic level, as shown in Figure 3. There is a signal ground in the RS-485 device in Figure 1. Generally, the signal ground of each device can be connected together or not connected; in the device, there is another “enable” control signal terminal, which is used to control RS485 as the transmitting terminal Still as the receiving end. When the enable terminal is at a high level, it is used as a transmitting terminal, and when it is at a low level, it is used as a receiving terminal. RS485 is a half-duplex communication mode. At the same time, the device can only be used as a transmitter or receiver. In the software design of the bus arbitration, it is controlled by the “enable” signal.
2 Application of RS485 in the system
In RS485 multi-machine communication, point A in each device is connected together, point B is connected together, connected by twisted pair, all RS485 nodes are connected to a pair of RS485 buses. Because RS485 is a half-duplex working mode, at the same time, only one RS485 interface device is allowed to be in the sending state in the network, otherwise, the data on the bus will be confused; in addition, because RS485 lacks a hardware arbitration mechanism, communication cannot be arbitrated by hardware. To release and occupy the bus, in a multi-machine communication network, bus arbitration can be considered from a software perspective. The RS485 standard does not specify the application layer communication protocol. In the software design system bus arbitration, the corresponding application communication layer protocol can be formulated according to the system characteristics and functions.
1) Brief description of the system
The network structure in the system adopts the star network structure, and the network topology is shown in Figure 4.
The network structure in the system consists of the master (M0) and slaves (S1, S2…), which are all embedded system equipment, and the core is CORTEX-M3. All RS485 terminal devices in this system are embedded devices, and the terminal device interfaces are the same, and they are directly connected through twisted pair wires.
The host is responsible for the information collection of the measurement and control system. Control information transmission; each slave is responsible for and completes the measurement and data conversion of various information, and sends the measurement and control information to the host. The system adopts a star topology structure, which makes it easy to find faults in branch nodes. Because of the simple interface and good scalability, the central node communication software protocol is more complicated, and the communication protocol of each branch node is relatively simple.
2) RS485 communication protocol bus design
The design of the communication protocol mainly includes the physical layer and the data link layer:
The physical layer implements the following functions:
When sending data, the data sent from the data link layer is converted into bytes by byte, and the start bit, parity bit and stop bit are added, and then the converted signal is sent to the physical channel; when receiving data, Receive the level signal serially from the physical channel, and then perform level conversion, parity check, and serial-to-parallel conversion to obtain the received data.
The data link layer implements the following functions:
The data link layer is located between the physical layer and the application layer, and provides a unified interface function for the application layer to achieve error-free two-way data transmission. The data link layer protocol mainly includes frame format design, transmission rate setting, frame receiving and dispatching procedures, and character string receiving and dispatching procedures.
①Serial port driver design
The design of the STM32F103 serial port driver calls system library functions, eliminating the need for writing many underlying functions. The frame format design and the transmission rate setting are assigned and initialized by the structure InitType DefInit StruCture; the frame sending and receiving has the library functions USART_ReceiveData (USART_TypeDef*USARTx) and USART_SendData (USART_Type Def*USARTx, uint16_tData), through these two The application of the function can realize the receiving and sending procedure of the string, simplifying the development of the driver.
In the serial port driver design, it is necessary to pay attention to: when receiving and sending frame data, the level of the enable terminal is set, so the transceiver program cannot directly use the library functions in stm32f. Accordingly, in the design of the serial port driver, the function of receiving and sending bytes is rewritten. Example of sending byte function:
②Communication protocol design
In order to achieve the correct reception of slave information, in the design process, according to the characteristics of the system, the corresponding communication protocol was formulated: artificially think that each slave has an address code, for example, the address of the 1st machine is 0x1, and the address of the 2nd machine is 0x2 , And so on, this data also represents the slave number. All slaves act as receivers, waiting to receive commands.
The first step: all slaves are in the receiving waiting state; the host sends a frame of data to indicate the address, and the slave receives and judges whether it is the address of the local machine: yes, then respond; otherwise, discard this frame of data and continue to wait for the receiving of the local address frame ;
Step 2: After the slave receives the local address frame, it sends a handshake response data to the master. At this time, the slave is the sender and the master is the receiver. After the host sends the address frame, it switches states and waits to receive the handshake response data. After receiving the bus handshake data, the host judges whether it is the handshake communication data of the machine number that needs to be read: yes, the communication is successful, read the information sent by the slave; otherwise, restart the address transmission;
The third step: After the slave sends the bus handshake signal, it immediately sends the data to the master. After the data is sent, the waiting state is restored.
During the entire communication process, all the slave communication protocols are consistent. After the slave receives the address information and judges, if it matches the local address, it will send the data to the master.
According to the communication protocol established in the system, the software design flowcharts of the master and slave are shown in Figure 5-7.
The slave machine is used as the measurement and control front end to realize system measurement and control and data transmission. Its communication is controlled by the master. Therefore, the slave receives data in an interrupt mode, that is, once the slave receives the call signal of the master, it immediately enters the reception and interrupts the communication with the master, and its work The flowchart is shown in Figures 6 and 7.
In the whole system, master and slave can be used as sender and receiver. At the same time, there can only be one sender in the network. Therefore, when designing the communication protocol, pay attention to the state switching; in addition, according to the timing characteristics of the RS485 state, it is necessary to design the delay between sending and receiving, otherwise there will be data loss. . When sending, the enable terminal is set to “1”, the delay is about 1 ms, and then valid data is sent. After one frame of data is sent, the delay is 1 ms again, and the enable terminal is set to “0”. In this way, the bus has a stable working state when the state is switched.
3 concluding remarks
The system uses RS485 half-duplex transceivers to realize the formation of a multi-machine communication network based on CORTEX-M3. The host uses a sequential method to collect data and realizes a master-multiple-slave communication mode. Through state switching, both the master and slave can play the roles of the sender and receiver. In essence, they still use the characteristics of half-duplex communication and use software for task scheduling, which finally realizes multi-machine communication in a star network. The RS485 communication protocol designed in this article has strong portability, high stability, and easy expansion, and can be widely used in industrial control, fieldbus, remote monitoring and other fields.