CN110311848A - A kind of method that Modbus RTU communications protocol improves communication efficiency - Google Patents
A kind of method that Modbus RTU communications protocol improves communication efficiency Download PDFInfo
- Publication number
- CN110311848A CN110311848A CN201910712086.3A CN201910712086A CN110311848A CN 110311848 A CN110311848 A CN 110311848A CN 201910712086 A CN201910712086 A CN 201910712086A CN 110311848 A CN110311848 A CN 110311848A
- Authority
- CN
- China
- Prior art keywords
- data frame
- data
- length
- function code
- anticipation
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L1/00—Arrangements for detecting or preventing errors in the information received
- H04L1/004—Arrangements for detecting or preventing errors in the information received by using forward error control
- H04L1/0045—Arrangements at the receiver end
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L1/00—Arrangements for detecting or preventing errors in the information received
- H04L1/004—Arrangements for detecting or preventing errors in the information received by using forward error control
- H04L1/0056—Systems characterized by the type of code used
- H04L1/0061—Error detection codes
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L12/00—Data switching networks
- H04L12/28—Data switching networks characterised by path configuration, e.g. LAN [Local Area Networks] or WAN [Wide Area Networks]
- H04L12/40—Bus networks
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L12/00—Data switching networks
- H04L12/28—Data switching networks characterised by path configuration, e.g. LAN [Local Area Networks] or WAN [Wide Area Networks]
- H04L12/40—Bus networks
- H04L2012/40208—Bus networks characterized by the use of a particular bus standard
- H04L2012/40228—Modbus
Abstract
The invention discloses a kind of methods that Modbus RTU communications protocol improves communication efficiency.For the main website and slave station of ModbusRTU agreement, after receiving data frame and starting, data length is determined using the function code and partial data content that receive in data frame, and carries out following judgement, and the anticipation of the data frame length is carried out with function code and partial data content;After anticipation length reaches, start to carry out cyclic redundancy check, obtains reception address therein using the data frame received, finally start to parse according to function code and data.The characteristics of the method for the present invention is no longer rely on bus-free time to be judged, and included by data frame itself is judged, so that the practical communication speed of ModbusRTU is accelerated, solve above-mentioned technical problem.
Description
Technical field
The present invention relates to a kind of Modbus RTU communications protocol improved methods, more particularly, to a kind of Modbus RTU
The method of communications protocol raising communication efficiency.
Background technique
Modbus RTU is an open serial protocol, using the communication modes of principal and subordinate.Modbus RTU is each
It is widely used among a industrial trade, in such as all kinds of PLC, controller, sensor, among actuator, it is
As a current industry standard.
The communication mode of Modbus RTU is request/response mode, and main website issues inquiry instruction, and slave station is according to receiving
Instruction carries out processing and response.
In request message, main website will inform that specified slave station (address) will execute which kind of function (function code), and attached
It is subject to data corresponding to function code, while provides the CRC check of data frame.
After slave station receives information, will judge whether the CRC of this information is correct, be unicast or broadcast, decision is by this
It is no to need response, response is not needed for broadcast, if it is unicast, then starts to judge whether to be intended for slave station the machine: if
It is non-native to be then not necessarily to response, and start to parse the function code and data of request, it is handled, and generate feedback coefficient
According to frame;If during this, an error has occurred, also generates the reply data frame comprising error code.
Modbus RTU data frame is in bus as shown in Figure 1, sentencing using the time threshold of bus-free time
The end of a disconnected data frame (char indicates character byte).In the agreement of Modbus RTU, the time of bus-free time
This data value of threshold value is set as the length of 3.5 bytes, i.e. time threshold of the bus-free time greater than 3.5 byte lengths
In the case of, it is believed that it is data frame end.In general, if communication speed is lower than and is equal to 19200bps, it is desirable that in strict accordance with 3.5 words
The time of section is judged;It, should be using 1.7500ms as data frame end if communication speed is higher than 19200bps
Judgement.
The protocol implementing method of ModbusRTU is as follows:
First, the logic of data frame receipt and processing is as follows: after serial ports receives data, as shown in Fig. 2, data are deposited
Enter to serial data array, reset timer, and is the judgment value of data frame end with the time of timer.
Second, it is interrupted as shown in figure 3, spilling has occurred in timer, that is, has been more than the time threshold of bus-free time, has said
The data frame receipt of bright ModbusRTU is complete, then starts to analyze the data frame of ModbusRTU, and execute subsequent behaviour
Make.
It is above the working principle and implementation method of existing Modbus RTU.
In fact, Modbus RTU is a wide-open agreement, it can be achieved that in multiple hardwares platform and software platform
Among, it include PC, PLC, various embedded devices etc. on hardware, the operating system of operation contains Windows OS,
Linux OS, various embedded OSs and without operating system etc..Different hardware, software configuration all can be to Modbus
The performance of RTU impacts, for example, all there is comparable difference in the precision that can be obtained timer, it can not be all
In the case of all obtain the precision of Millisecond.
This is resulted in, usually can be by this number when developing ModbusRTU agreement in order to which equipment obtains preferably compatibility
Judge that numerical value is turned up according to the time threshold of frame end, so as to the Modbus equipment being compatible under different platform, this numerical value would generally
It is set to several ms.This is allowed for using Modbus rtu protocol, and practical communication speed is deteriorated.
Summary of the invention
In order to solve the technical problems in background technology, for the end faster to ModbusRTU data frame
Judged, the invention proposes a kind of methods that Modbus RTU communications protocol improves communication efficiency, pass through a kind of new number
Terminate to determine according to frame integrality to improve efficiency.In method, the free time of bus data is not used to be used as judgement, but it is sharp
It is made a decision with the characteristics of ModbusRTU data frame itself.
The technical solution adopted by the present invention is that:
For the main website and slave station of ModbusRTU agreement, after receiving data frame and starting:
1) data length is determined using the function code and partial data content that receive in data frame, and sentence below progress
It is disconnected, the anticipation of the data frame length is carried out with function code and partial data content:
If the data frame length received reaches anticipation value, data frame has been received completely, has been carried out in next step;
2) after anticipation length reaches, start to carry out cyclic redundancy check (CRC):
If cyclic redundancy check passes through, start to stop timer, carries out in next step, terminating this receive process;
If cyclic redundancy check does not pass through, there are problem, the data frames can also receive for the data frame, and it is fixed to continue
When device operation, waiting timer, which reaches, directly abandons the data frame after preset time threshold;
3) reception address therein is obtained using the data frame received: receiving the first character section of address resolution data frame
It obtains.
If the address for receiving address and the machine is different/it does not meet, it resets timer and exits;
If receive address it is identical with the address of the machine/meet, start to parse according to function code and data.
The present invention still maintains the presence of timer, but by timer not as the judgement of bus-free time, with reply
The case where possible data frame error, directly abandons data after timer overflows and interrupts generation.
The data frame is entire Modbus data frame, successively includes device address, function code, data and mistake school
Several parts of code are tested, data are a part in data frame.
The method is not greater than time threshold by bus-free time to judge the end of a data frame.
The present invention determines the length of the data and entire data frame in data frame using the partial content in function code and data
The characteristic of degree, while last two of entire data frame are utilized as cyclic redundancy check code, the school of entire data frame is carried out with this
The characteristic tested devises above-mentioned processing method and carries out data frame end/complete sex determination, so that in Modbus RTU communications protocol
Data frame transfer it is more effective, time end node is more acurrate, thus improve Modbus RTU communication efficiency.
The beneficial effects of the present invention are:
The judgement of data frame integrality was judged by the time of bus free in existing ModbusRTU, one
As equipment for better compatibility, it will usually this time is lengthened, this makes the communication speed of actual ModbusRTU aobvious
It obtains very slow.
The characteristics of the method for the present invention is no longer rely on bus-free time to be judged, and included by data frame itself
Judged, so that the practical communication speed of ModbusRTU is accelerated, solve above-mentioned technical problem.
Detailed description of the invention
Fig. 1 is reception data structure schematic diagram of the Modbus RTU data frame in bus;
Fig. 2 is the processing operation flow chart that serial ports receives after data frame in Modbus RTU;
Fig. 3 is that timer occurs to overflow the processing operation flow chart after interrupting in Modbus RTU;
Fig. 4 is the processing operation flow chart that serial ports receives after data frame in the improved Modbus RTU of the present invention;
Fig. 5 is that timer occurs to overflow the processing operation flow chart after interrupting in the improved Modbus RTU of the present invention;
Fig. 6 is that sine wave is used signal transmission to occur and with the existing ModbusRTU agreement side of sending and receiving in embodiment
Formula and free time is set as the data waveform result figure that 25ms is read;
Fig. 7 is that sine wave is used signal transmission to occur and with the existing ModbusRTU agreement side of sending and receiving in embodiment
Formula and free time is set as the data waveform result figure that 50ms is read;
Fig. 8 be embodiment in using sine wave occur signal transmit and send and receive in the process of the present invention mode and by
Free time is set as the data waveform result figure that 25ms is read.
Specific embodiment
The present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.
The data frame structure that the present invention is embodied is as follows:
By classifying above as it can be seen that there was only the length of data portion in the length of data frame is variation, the length of other parts
Degree is fixed, and data portion and function code have correlation, and it is every to carry out Taxonomic discussion according to the difference of function code here
The length of data portion determines under a function code.
There are also a kind of abnormal function codes, it is existed only among the response of slave station.Its function code is the function that main website issues
Energy code adds 0x80.
By above analysis as it can be seen that main website and slave station for Modbus RTU, all contain data among data frame
The information of the entire length of frame.Have it is some read function code, that is, may know that the entire length of data frame, have and some read number
Partial data in can calculate the entire length of data frame.
In specific implementation, the main website and slave station of ModbusRTU agreement are established.The function code of ModbusRTU has reacted slave
The function that required slave station is completed.
For slave, received is the data frame that host issues.For host, received be from
The data frame that machine returns.
When reading function code, the method for judgement performed by main website and slave station is listed as follows:
The decision logic for the new data frame end that the present invention designs is as follows.
A) the end decision logic for the data frame that main website issues:
The data frame that main website issues is to be received by slave station, and slave station is during received.
1, two variables are set in slave station, and data frame length anticipation value and data frame length prejudge complement mark.When initial,
It is 0 that data frame length, which prejudges complement mark,;
2, slave station real-time reception data frame and judge:
When slave station receives the 2nd byte of data frame, determine function code be 1,2,3,4,5,6 in one when, number
Being set as 8 according to frame length anticipation value, (length of data frame is 8, and the length of data is 4), and by data frame under these function codes
Length anticipation complement mark is set to 1;
When slave station receives the 2nd byte of data frame, determine function code be 15,16 in one when, continue to read
Data frame, when receiving the 7th byte of data frame, data frame length anticipation value is set as the 7th byte of data frame
The numerical value of (i.e. the 5th bytes of data) adds 9, and data frame length anticipation complement mark is set to 1.
3, slave station determines data frame length anticipation complement mark and is set to 1, and the data frame length received reaches data
When frame length anticipation value, starts with data frame last error check code and error checking and correction is carried out to data frame, if mistake school
It tests and passes through, then slave station has just had successfully received data frame, and can continue to connect down this data frame
The analysis and processing gone;
4, the function code in the data frame that slave station is received is not the function code or data length exception within the scope of this,
When error checking and correction does not pass through, the end of data frame will be still judged according to conventional bus-free time;
5, slave station makees discard processing for being not sent to the data frame of this slave station.
B) the end decision logic for the data frame that slave station issues:
The data frame that slave station issues is to be received by main website, and main website is during received.
1, two variables are set in main website, and data frame length anticipation value and data frame length prejudge complement mark.When initial,
It is 0 that data frame length, which prejudges complement mark,;
2, main website real-time reception data frame and judge:
When main website receives the 2nd byte of data frame, when to determine function code be one in 1,2,3,4, after resuming studies
Data frame is taken, when receiving the 3rd byte of data frame, data frame length anticipation value is set as the numerical value of the 3rd byte
In addition 5, and data frame length anticipation complement mark is set to 1;
When main website receives the 2nd byte of data frame, determine function code be 5,6,15,16 in one when, data
Frame length anticipation value is 8, and data frame length anticipation complement mark is set to 1;
4, when main website receives the 2nd byte of data frame, when determining function code greater than 0x80 (abnormal function code), number
It is 5 according to frame length anticipation value, and data frame length anticipation complement mark is set to 1;
5, main station judging is set to 1 to data frame length anticipation complement mark, and the data frame length received reaches data
When frame length anticipation value, starts with data frame last error check code and error checking and correction is carried out to data frame, if mistake school
It tests and passes through, then slave station has just had successfully received data frame, and can continue to carry out subsequently this data frame
Analysis and processing;
6, the function code in the data frame that main website is received is not the function code or data length exception within the scope of this,
When error checking and correction does not pass through, the end of data frame will be still judged according to conventional bus-free time.
Embodiment:
In test, embodiment develops the main website (host) of a ModbusRTU on PC computer, in embedded device
The slave station (slave) of a ModbusRTU is developed, and realizes a sine-wave generator in embedded device, generator
Update cycle is 1ms, amplitude 1000, period 5S.
Serial communication is configured to 9600bps, no parity, 1 stop position.
Circulation reads data and records to it on PC computer, has recorded three kinds of configurations here:
The first, carries out reading data using the method for existing ModbusRTU agreement
Free time is set as 25ms, and the data waveform read is as shown in Figure 6.
Second, as a comparison, the data wave for being set as 50ms, reading will be changed free time on the basis of the first
Shape is as shown in Figure 7.
In Fig. 6 and Fig. 7, waveform sawtooth is more, illustrates that actual communication speed is slower.Fig. 6 and Fig. 7, which compares, can be seen that
Bus-free time has influence to actual communication speed, and bigger bus-free time setting will lead to actual communication speed
Degree reduces.
The third carries out reading data using the method for the present invention, and the data waveform read is as shown in Figure 7.
Thus the case where result of implementation comparison is it can clearly be seen that using new method, reading data is greatly
Improve, only relies on data frame feature itself and judged, practical communication speed significantly increases.
Claims (3)
1. a kind of method that Modbus RTU communications protocol improves communication efficiency, it is characterised in that:
For the main website and slave station of ModbusRTU agreement, after receiving data frame and starting:
1) data length is determined using the function code and partial data content that receive in data frame, and carry out following judgement, with
Function code and partial data content carry out the anticipation of the data frame length, if the data frame length received reaches anticipation value,
Then data frame has received completely, carries out in next step;
2) after anticipation length reaches, start to carry out cyclic redundancy check:
If cyclic redundancy check passes through, start to stop timer, carries out in next step, terminating this receive process;
If cyclic redundancy check does not pass through, there are problems for the data frame, continue the operation of timer, waiting timer reaches
The data frame is directly abandoned after preset time threshold;
3) reception address therein is obtained using the data frame received:
If the address for receiving address and the machine is different/it does not meet, it resets timer and exits;
If receive address it is identical with the address of the machine/meet, start to parse according to function code and data.
2. the method that a kind of Modbus RTU communications protocol according to claim 1 improves communication efficiency, it is characterised in that:
The data frame is entire Modbus data frame, successively includes the several of device address, function code, data and error check code
A part, data are a part in data frame.
3. the method that a kind of Modbus RTU communications protocol according to claim 1 improves communication efficiency, it is characterised in that:
The method is not greater than time threshold by bus-free time to judge the end of a data frame.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201910712086.3A CN110311848A (en) | 2019-08-02 | 2019-08-02 | A kind of method that Modbus RTU communications protocol improves communication efficiency |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201910712086.3A CN110311848A (en) | 2019-08-02 | 2019-08-02 | A kind of method that Modbus RTU communications protocol improves communication efficiency |
Publications (1)
Publication Number | Publication Date |
---|---|
CN110311848A true CN110311848A (en) | 2019-10-08 |
Family
ID=68082912
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201910712086.3A Pending CN110311848A (en) | 2019-08-02 | 2019-08-02 | A kind of method that Modbus RTU communications protocol improves communication efficiency |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN110311848A (en) |
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN112235302A (en) * | 2020-10-15 | 2021-01-15 | 天津津航计算技术研究所 | Protocol analysis method suitable for multiple buses |
CN113660190A (en) * | 2021-05-17 | 2021-11-16 | 捷佳润科技集团股份有限公司 | Novel design method for RTU (remote terminal unit) communication protocol with wake-up mechanism |
CN114584630A (en) * | 2020-11-18 | 2022-06-03 | 中移物联网有限公司 | Communication method and device based on field bus protocol |
Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN103546467A (en) * | 2013-10-23 | 2014-01-29 | 上海爱控自动化设备有限公司 | Method for transmitting Modbus RTU protocol on TCP/IP network |
CN103561032A (en) * | 2013-11-08 | 2014-02-05 | 山东大学 | Modbus device network communication method under TCP/IP |
CN103929439A (en) * | 2014-05-07 | 2014-07-16 | 昆山华恒焊接股份有限公司 | Data transmission method and MODBUS server |
CN107464412A (en) * | 2017-08-09 | 2017-12-12 | 国网浙江省电力公司 | A kind of electric energy meter event information copy reading method |
-
2019
- 2019-08-02 CN CN201910712086.3A patent/CN110311848A/en active Pending
Patent Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN103546467A (en) * | 2013-10-23 | 2014-01-29 | 上海爱控自动化设备有限公司 | Method for transmitting Modbus RTU protocol on TCP/IP network |
CN103561032A (en) * | 2013-11-08 | 2014-02-05 | 山东大学 | Modbus device network communication method under TCP/IP |
CN103929439A (en) * | 2014-05-07 | 2014-07-16 | 昆山华恒焊接股份有限公司 | Data transmission method and MODBUS server |
CN107464412A (en) * | 2017-08-09 | 2017-12-12 | 国网浙江省电力公司 | A kind of electric energy meter event information copy reading method |
Non-Patent Citations (1)
Title |
---|
朱小襄: ""ModBus通信协议及编程"", 《电子工程师》 * |
Cited By (6)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN112235302A (en) * | 2020-10-15 | 2021-01-15 | 天津津航计算技术研究所 | Protocol analysis method suitable for multiple buses |
CN112235302B (en) * | 2020-10-15 | 2023-03-24 | 天津津航计算技术研究所 | Protocol analysis method suitable for multiple buses |
CN114584630A (en) * | 2020-11-18 | 2022-06-03 | 中移物联网有限公司 | Communication method and device based on field bus protocol |
CN114584630B (en) * | 2020-11-18 | 2023-10-27 | 中移物联网有限公司 | Communication method and device based on field bus protocol |
CN113660190A (en) * | 2021-05-17 | 2021-11-16 | 捷佳润科技集团股份有限公司 | Novel design method for RTU (remote terminal unit) communication protocol with wake-up mechanism |
CN113660190B (en) * | 2021-05-17 | 2023-05-30 | 捷佳润科技集团股份有限公司 | Novel design method of RTU communication protocol with wake-up mechanism |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN110311848A (en) | A kind of method that Modbus RTU communications protocol improves communication efficiency | |
US20100211859A1 (en) | Systems and methods for data alignment | |
CN109803024B (en) | Method for cluster node network | |
CN112055096B (en) | Method and device for automatically setting communication address of equipment | |
CN1151231A (en) | Packet data protocol for wireless communication | |
KR101438212B1 (en) | Method for deep packet instection of software defined network and software defined networking system using the same | |
CN115037700B (en) | Complex network data packet transmission method, system, terminal and storage medium | |
CN112698640B (en) | ECU upgrading test system | |
CN103676663A (en) | A wind generating set combined simulation system communication interface realization method | |
CN110730112A (en) | Method, system, equipment and storage medium for testing network running speed | |
EP1500227B1 (en) | Method for recognizing electronic appliance in multiple control system | |
CN107453844B (en) | Method and device for automatically networking intelligent equipment | |
CN111198698B (en) | EtherCAT-based multi-device firmware program parallel downloading method and system | |
CN111371799B (en) | Method, device and equipment for controlling data receiving and transmitting of MCTP (Multi-channel media Port) controller | |
CN114268650A (en) | Configuration method and system for establishing communication connection | |
CN113556210A (en) | Upper computer and method for establishing communication connection | |
CN108683689B (en) | Improved test system and method for realizing NIDS and NIPS intrusion detection function | |
CN112152753A (en) | Method and device for transmitting data with lower computer | |
CN110708379A (en) | Data acquisition method, concentrator, data acquisition system and readable storage medium | |
CN112596893B (en) | Monitoring method and system for multi-node edge computing device | |
CN206283532U (en) | A kind of data acquisition device | |
CN112988202B (en) | System and method for upgrading FC (fiber channel) module | |
CN113630294B (en) | Switch modularization detection method and device | |
CN110868321B (en) | Fault positioning method and system based on edge calculation algorithm verification | |
CN109873742B (en) | Simulation control method and device for FC-AE bus |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
WD01 | Invention patent application deemed withdrawn after publication | ||
WD01 | Invention patent application deemed withdrawn after publication |
Application publication date: 20191008 |