RS485 transceiving direction switching method and device
By using sleep interrupt processing and hardware timer interrupts in RS485 state transition, the problems of data packet loss and high CPU usage caused by unreasonable delay during RS485 state transition are solved, and more efficient data transmission and system performance improvement are achieved.
Patent Information
- Application Number
- CN202310807256.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-30
- Publication Date
- 2024-12-31
AI Technical Summary
In the prior art, the unreasonable delay setting during state transition of RS485 leads to packet loss or serial port lag, and frequent query of hardware sending completion flags takes up a large amount of CPU processing time, affecting system performance.
By increasing the priority of the data send caller, the RS485 transceiver pin is switched to the sending state, and the hardware timer interrupt is used to intermittently query the hardware sending completion status, reducing CPU usage, and improving state switching accuracy and efficiency.
It effectively avoids data packet loss and serial port lag, reduces the system CPU share, improves the overall operating performance of the operating system, and improves the accuracy and accuracy of sending and receiving direction switching without modifying the hardware solution.
Smart Images

Figure CN119226204A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a method and a device for switching RS485 receiving and sending directions, belonging to the technical field of communication. Background Art
[0002] RS485 is a half-duplex universal asynchronous receiver and transceiver. It can only be in one state, sending or receiving, at any time. When sending data, RS485 needs to be converted to the transmitter state before sending data. When receiving data, RS485 needs to be converted to the receiver state before receiving data. State conversion can be achieved by hardware and software. The hardware method will take into account the driving ability and cost control of the hardware. Some applications are not suitable for the hardware method. Most of the products with built-in RS485 on the market basically use the software method. This method controls the direction pin level of RS485 through software to achieve the conversion of sending and receiving states.
[0003] The conversion of RS485 status in software is realized by controlling the direction pin level. When the control direction pin is high, RS485 is converted to transmitter status, and then a certain time is delayed to ensure that the hardware has enough time to complete the switch; after sending the data, a delay is also required to ensure that the data can be sent out completely, and then the control direction pin level is pulled down to convert RS485 to receiver status. When implementing RS485 status conversion, if the delay is unreasonable, it will cause data packet loss or serial port jamming, affecting the normal communication of the serial port; it will also frequently query the hardware sending completion flag, which will take up a lot of CPU processing time, resulting in a high system CPU occupancy rate, affecting the overall performance of the operating system.
[0004] The invention patent document with publication number CN112713910A discloses a method, system, device and medium for converting the RS485 transceiver state. The method is based on controlling the RS485 to the sending state and starting the timer to start timing. The timing period of the timer is calculated according to the current baud rate of the RS485. When the timer ends, the RS485 is controlled to the receiver state. However, the baud rate of RS485 is affected by environmental factors, transmission distance, cable quality and other factors, which will cause the baud rate of RS485 to be not particularly accurate, which will cause the deviation of the timing period in the patent scheme, thereby also causing data packet loss or serial port jamming problems, affecting the normal communication of the serial port. Summary of the invention
[0005] The purpose of the present invention is to provide a method and device for switching the RS485 receiving and sending directions, so as to solve the problems of data packet loss or serial port jamming caused by unreasonable delay setting during RS485 state conversion and high system CPU occupancy rate caused by frequent cyclic query of hardware sending completion flag in the prior art.
[0006] To achieve the above object, the solution of the present invention includes:
[0007] A method for switching the RS485 transmission and reception direction of the present invention comprises the following steps:
[0008] 1) When sending data, increase the priority of the data sender; switch the RS485 transceiver pin to the sending state;
[0009] Copy data to the buffer;
[0010] 2) Enable the THR transmit holding register empty interrupt to complete the data transmission of the buffer;
[0011] 3) Check whether the hardware has completed sending. If the hardware has completed sending, switch the RS485 transceiver pin to the receiving state and restore the priority of the data sender; if the hardware has not completed sending, enter the dormant interrupt processing;
[0012] 4) After the set conditions are met, the sleep interrupt processing is exited, and the above step 3) is repeated until the hardware sending is completed.
[0013] When sending data, the driver first raises the task priority of the caller of the data sending module to ensure timely awakening of the subsequent sleep waiting; after completing the data sending of the buffer, query whether the hardware has completed the sending. If the hardware sending has not been completed, directly perform the sleep suspension service; intermittently query whether the hardware has completed the data sending through the sleep module, which can effectively reduce or avoid data packet loss or serial port jamming, and will not frequently query the hardware sending completion flag, thereby reducing the processing time occupied by the CPU, reducing the system CPU occupancy rate, and improving the overall operating performance of the operating system.
[0014] Furthermore, in step 3), the sleep interrupt processing is to set the hardware timer interrupt trigger time and enable the timer interrupt, suspend the task and wait for the timer interrupt event. If the timer interrupt event arrives, it will be awakened to exit the sleep interrupt, otherwise it will continue to wait for the arrival of the timer interrupt event; the timer interrupt event is that after the interrupt trigger time of the hardware timer arrives, the timer interrupt is disabled and the suspended task is awakened.
[0015] Furthermore, after enabling the THR send holding register empty interrupt in the step 2), when the serial port interrupt processing detects that the send holding register is empty, the data in the serial port data buffer area is written to the serial port controller for hardware transmission. If the data transmission of the buffer is completed, the THR send holding register empty interrupt is disabled, and a data buffer empty event is sent. The sending data buffer empty event indicates that the data transmission of the buffer is completed.
[0016] Use the send holding register empty interrupt event to complete the data transmission of the buffer. There is no need to calculate the time required for sending the data in the buffer. You only need to wait for the interrupt event to be triggered. This method is simple and effective.
[0017] Furthermore, the query hardware includes querying a transmit holding register and a transmit shift register.
[0018] Check whether the transmit holding register and the transmit shift register are empty to determine whether the data has been actually sent, thus avoiding packet loss of the transmitted data.
[0019] As a further improvement, the interrupt triggering time of the hardware timer is set to microsecond level.
[0020] By setting the timer interrupt time interval to microseconds, you can more accurately control the completion time of querying the hardware to send data and reduce the delay time of waiting for pin status switching.
[0021] An RS485 transceiver direction switching device includes a processor, wherein the processor executes the following method instructions:
[0022] 1) When sending data, increase the priority of the data sender; switch the RS485 transceiver pin to the sending state;
[0023] Copy data to the buffer;
[0024] 2) Enable the THR transmit holding register empty interrupt to complete the data transmission of the buffer;
[0025] 3) Check whether the hardware has completed sending. If the hardware has completed sending, switch the RS485 transceiver pin to the receiving state and restore the priority of the data sender; if the hardware has not completed sending, enter the dormant interrupt processing;
[0026] 4) After the set conditions are met, the sleep interrupt processing is exited, and the above step 3) is repeated until the hardware sending is completed.
[0027] Furthermore, in step 3), the sleep interrupt processing is to set the hardware timer interrupt trigger time and enable the timer interrupt, suspend the task and wait for the timer interrupt event. If the timer interrupt event arrives, it will be awakened to exit the sleep interrupt, otherwise it will continue to wait for the arrival of the timer interrupt event; the timer interrupt event is that after the interrupt trigger time of the hardware timer arrives, the timer interrupt is disabled and the suspended task is awakened.
[0028] Furthermore, after enabling the THR send holding register empty interrupt in the step 2), when the serial port interrupt processing detects that the send holding register is empty, the data in the serial port data buffer area is written to the serial port controller for hardware transmission. If the data transmission of the buffer is completed, the THR send holding register empty interrupt is disabled, and a data buffer empty event is sent. The sending data buffer empty event indicates that the data transmission of the buffer is completed.
[0029] Furthermore, the query hardware includes querying a transmit holding register and a transmit shift register.
[0030] As a further improvement, the interrupt triggering time of the hardware timer is set to microsecond level. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 It is a data transmission module flow chart in the RS485 serial port driver of the present invention;
[0032] Figure 2 It is the interrupt processing module flow chart in the RS485 serial port driver of the present invention;
[0033] Figure 3 It is a high-precision sleep module flow chart in the RS485 serial port driver of the present invention;
[0034] Figure 4 It is a schematic diagram of the RS485 receiving and sending direction pin level switching of the present invention. DETAILED DESCRIPTION
[0035] RS485 uses two-wire differential to transmit serial data. Its sending and receiving cannot be performed at the same time. It is in half-duplex working mode. In actual use, in order to improve its driving ability, it is usually necessary to control the sending and receiving direction of the driver chip. Currently, the commonly used directions are: automatic switching by hardware, FPGA implementation and software switching.
[0036] Among these methods, the hardware switching method cannot meet the basic communication requirements in long-distance communication due to insufficient driving capability. On the other hand, the hardware is expensive, which increases the product cost. The FPGA implementation method not only increases the hardware cost, but also requires modifying the original software and hardware design and communication methods. The existing software switching method has the problem of inaccurate delay time for switching the control pin state after sending data. When facing a terminal device with a faster response, it is easy to cause packet loss caused by untimely direction switching. In addition, software switching will also take up a lot of CPU processing time due to cyclic query of the sending completion flag, resulting in a high system occupancy rate and affecting the overall performance of the operating system.
[0037] To solve the above problems, the present invention proposes a strategy for switching the RS485 receiving and sending directions by software, and deploys and implements it on embedded devices using different operating systems (such as Linux or RTOS). Thus, the precision and accuracy of the receiving and sending direction switching are improved without modifying the existing product hardware solution, and the CPU occupancy rate is not too high, thereby saving product costs and improving the economic benefits of the product.
[0038] In order to make the purpose, technical solutions and advantages of the present invention more clear, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments.
[0039] An embodiment of a method for switching the RS485 transmission and reception direction:
[0040] like Figure 1 The RS485 transmission and reception direction switching method shown in the figure implements the method by providing three modules through the serial port driver, namely, a data transmission module, an interrupt processing module and a high-precision sleep module, wherein THR is a transmission holding register, XSR is a transmission shift register, and the final data transmission in RS485 is sent through hardware registers THR and XSR.
[0041] like Figure 1 The data transmission module shown is an interface for users to send data. The hardware device and data to be sent are specified through the serial port device index, the pointer to the transmission data buffer area and the data length. In this module, the driver first increases the task priority of the caller of the data transmission module to ensure timely wake-up of the subsequent sleep waiting, and then controls the RS485 transceiver direction switching pin to the transmission state, and sends the data to the transmission buffer area of the serial port driver module, and enables the transmission holding register THR empty interrupt of the serial port register to trigger Figure 2 The interrupt processing module shown in the figure sends data, and then passes through the first sleep waiting point to wait for Figure 2 The data buffer empty event of the interrupt processing module is shown. Figure 2 After the data buffer empty event is sent by the interrupt module, the send holding register THR, the send shift register XSR and the send data buffer are simultaneously queried to determine whether the data has been truly sent. If not, enter Figure 3 The microsecond-level high-precision sleep module shown in the figure, if it is empty, switches the RS485 receiving and sending direction pin to the receiving state to ensure the effective reception of data. Finally, adjust the task priority of the caller of the data sending module to the original level.
[0042] like Figure 2The interrupt processing module shown is the actual data sending module. When it is detected that the sending holding register is empty, the following operations are performed according to the data situation of the serial port data buffer: If there is data in the serial port data buffer, check whether there is space in the FIFO of the serial port controller. If there is space in the FIFO, write the data into the serial port controller according to the size of the FIFO space of the serial port controller. Otherwise, exit this interrupt and wait for the next THR interrupt to be triggered. If the serial port data buffer is empty, it means that all the data has been sent to the serial port controller. At this time, the sending holding register empty interrupt can be disabled, that is, the sending empty interrupt is no longer enabled, and the data buffer empty event is sent to Figure 1 The data sending module shown, that is, the serial port data sending interface, notifies the serial port data sending interface to enter the query and waiting process of the subsequent sending completion flag and exit this interrupt processing; other situations that do not conform to the above logic all exit this interrupt processing.
[0043] like Figure 3 The high-precision sleep module shown is mainly used to implement a microsecond delay processing to avoid the problem of excessive CPU occupancy caused by repeated queries on whether the hardware has completed the transmission. The specific implementation method is: through a hardware timer, set the interrupt trigger time according to the delay parameter, and enable the timer interrupt, and then set a sleep event waiting point to wait for the timer interrupt to be triggered. When the timer's timing time interval is reached, the timer interrupt event is triggered, and then the timer is prohibited from being interrupted, and the sleep module is awakened, so that the module exits the sleep state to return Figure 1 The data sending module shown continues to query the hardware sending status.
[0044] Through the cooperation of the above three modules, the RS485 receiving direction can be quickly switched without taking up too much CPU processing time. The actual measurement shows that if Figure 4As shown, RST is the sending request pin in RS485, and the input signal of the RST pin is a level pulse signal that triggers the transmission and receiving pins in RS485 to send and receive data. RXD is the receiving data pin, and TXD is the transmitting data pin. When the level signal in RST is high, the TXD pin is triggered to send data without triggering the data reception of the RXD pin. When the level signal in RST is low, the RXD pin is triggered to receive data without triggering the data transmission of the TXD pin. When the level signal in RST is high, after the TXD pin is ready to send data, the start bit of the data is first sent, then the data bit is sent, and finally the data stop bit is sent. After sending the data stop bit, the TXD pin will have a waiting time, waiting for the hardware in RST to send all the data to be sent before switching the sending and receiving direction. The scheme of the present invention controls the time T1 from sending the data stop bit to completing the switching of the sending and receiving direction within tens of microseconds, which can meet the use in most industrial application environments, save hardware investment, and achieve good economic benefits.
[0045] An RS485 transceiver direction switching device embodiment:
[0046] The present invention provides an RS485 transceiver direction switching device, which includes a processor. The processor executes the RS485 transceiver direction switching method in the method embodiment. The RS485 transceiver direction switching method of the present invention has been clearly introduced in the method embodiment and will not be repeated here.
Claims
1. A method for switching the RS485 transmission and reception direction, characterized in that: The steps include: 1) When sending data, increase the priority of the data sender; switch the RS485 transceiver pin to the sending state; Copy data to the buffer; 2) Enable the THR transmit holding register empty interrupt to complete the data transmission of the buffer; 3) Check whether the hardware has completed sending. If the hardware has completed sending, switch the RS485 transceiver pin to the receiving state and restore the priority of the data sender; if the hardware has not completed sending, enter the dormant interrupt processing; 4) After the set conditions are met, the sleep interrupt processing is exited, and the above step 3) is repeated until the hardware sending is completed.
2. The RS485 transceiver direction switching method according to claim 1, wherein: In step 3), the sleep interrupt processing is to set the hardware timer interrupt trigger time, enable the timer interrupt, suspend the task and wait for the timer interrupt event, if the timer interrupt event arrives, it will be awakened to exit the sleep interrupt, otherwise it will continue to wait for the arrival of the timer interrupt event; The timer interrupt event is when the interrupt trigger time of the hardware timer arrives, the timer interrupt is disabled, and the suspended task is awakened.
3. RS485 transceiver direction switching method according to claim 1, characterized in that, After enabling the THR send holding register empty interrupt in the step 2), when the serial port interrupt processing detects that the send holding register is empty, the data in the serial port data buffer area is written to the serial port controller for hardware transmission. If the data transmission of the buffer is completed, the THR send holding register empty interrupt is disabled, and a data buffer empty event is sent. The sending data buffer empty event indicates that the data transmission of the buffer is completed.
4. RS485 transceiver direction switching method according to claim 1, characterized in that, The query hardware includes querying a transmit holding register and a transmit shift register.
5. The RS485 transceiver direction switching method according to claim 2, wherein: The interrupt triggering time of the hardware timer is set to microsecond level.
6. An RS485 transceiver direction switching device, comprising a processor, characterized in that: The processor executes the following method instructions: 1) When sending data, increase the priority of the data sender; switch the RS485 transceiver pin to the sending state; Copy data to the buffer; 2) Enable the THR transmit holding register empty interrupt to complete the data transmission of the buffer; 3) Check whether the hardware has completed sending. If the hardware has completed sending, switch the RS485 transceiver pin to the receiving state and restore the priority of the data sender; if the hardware has not completed sending, enter the dormant interrupt processing; 4) After the set conditions are met, the sleep interrupt processing is exited, and the above step 3) is repeated until the hardware sending is completed.
7. The RS485 transceiver direction switching device according to claim 6, characterized in that: In step 3), the sleep interrupt processing is to set the hardware timer interrupt trigger time, enable the timer interrupt, suspend the task and wait for the timer interrupt event, if the timer interrupt event arrives, it will be awakened to exit the sleep interrupt, otherwise it will continue to wait for the arrival of the timer interrupt event; The timer interrupt event is when the interrupt trigger time of the hardware timer arrives, the timer interrupt is disabled, and the suspended task is awakened.
8. The RS485 transceiver direction switching device according to claim 6, characterized in that: After enabling the THR send holding register empty interrupt in the step 2), when the serial port interrupt processing detects that the send holding register is empty, the data in the serial port data buffer area is written to the serial port controller for hardware transmission. If the data transmission of the buffer is completed, the THR send holding register empty interrupt is disabled, and a data buffer empty event is sent. The sending data buffer empty event indicates that the data transmission of the buffer is completed.
9. The RS485 transceiver direction switching device according to claim 6, characterized in that: The query hardware includes querying a transmit holding register and a transmit shift register.
10. The RS485 transceiver direction switching device according to claim 7, characterized in that: The interrupt triggering time of the hardware timer is set to microsecond level.
Citation Information
Patent Citations
RS485 transceiving state conversion method, system and device, and medium
CN112713910A
Cited By
High-speed data transmission communication method of double-data-rate storage interface
CN122450868A
A high-speed data transmission communication method for a double data rate memory interface
CN122450868B
