CN111124986B - Asynchronous serial port communication protocol compatible with Modbus and high in efficiency - Google Patents
Asynchronous serial port communication protocol compatible with Modbus and high in efficiency Download PDFInfo
- Publication number
- CN111124986B CN111124986B CN201911390305.7A CN201911390305A CN111124986B CN 111124986 B CN111124986 B CN 111124986B CN 201911390305 A CN201911390305 A CN 201911390305A CN 111124986 B CN111124986 B CN 111124986B
- Authority
- CN
- China
- Prior art keywords
- serial port
- asynchronous serial
- frame
- communication protocol
- setting
- 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.)
- Active
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F13/00—Interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
- G06F13/38—Information transfer, e.g. on bus
- G06F13/42—Bus transfer protocol, e.g. handshake; Synchronisation
- G06F13/4282—Bus transfer protocol, e.g. handshake; Synchronisation on a serial bus, e.g. I2C bus, SPI bus
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Communication Control (AREA)
Abstract
The invention provides a more efficient asynchronous serial port communication protocol compatible with Modbus, which does not depend on 3.5 idle timing of 11-bit transmission time through a communication protocol frame and a delimitation method thereof so as to delimitate a frame (namely, a t3.5 frame delimitation method) and listen to receiving interruption generated by each transmitted byte so as to identify the frame sent to the slave, thereby greatly reducing the generation of a large amount of additional receiving interruption processing and a large amount of timer interruption processing when the slave is not selected by a host for communication, simultaneously being compatible and coexisting with Modbus-RTU equipment in the same bus through reasonable slave address distribution, simultaneously serving the host and solving the defects in the prior art.
Description
Technical Field
The invention relates to the technical field of serial communication, in particular to communication between controllers and input and output devices of industrial equipment and Internet of things equipment, and particularly relates to a more efficient asynchronous serial port communication protocol compatible with Modbus.
Background
Modbus is a serial communication protocol, adopts a single-master multi-slave communication method, and is published by Modicon corporation (current schneider electrical) in 1979 for communication using a Programmable Logic Controller (PLC). Now becomes an industry standard of communication protocols in the industry field, and is a common connection mode between industrial electronic devices.
On a serial link, the Modbus has two transmission modes of ASCII and RTU, and the specific format is as follows:
1. the ASCII serial port format is: the baud rate is variable and is transmitted in 10 bits (i.e., 1 start bit, 7 data bits, 1 parity bit, 1 stop bit). Even parity is required and odd parity, no parity is supported. The check bits are filled with stop bits when there is no check. Each binary data byte is transmitted as a hexadecimal number represented by 2-time 7-bit ASCII characters;
2. the serial port format of the RTU mode is as follows: the baud rate is variable and is transmitted in 11 bits (i.e., 1 start bit, 8 data bits, 1 parity bit, 1 stop bit). Even parity is required and odd parity, no parity is supported. The check bits are filled with stop bits when there is no check. Each binary data byte is transmitted with a 1-time 8-bit binary number;
3. the ASCII mode frame-delimited format is: in the column number': ' (hexadecimal 3A) is a frame start character, and a carriage return-line feed ' CR-LF ' (hexadecimal 0D-0A) is a frame end character;
4. the frame delimitation format for the RTU mode is: with 3.5 idle with 11 bit transmission time as frame delimiters, namely: after each frame is sent, the idle time of 3.5 11-bit transmission time is immediately followed, so that a receiving end program judges that the current frame is received completely.
As shown in fig. 1, an example of a primary communication transmission of Modbus in ASCII and RTU modes on a serial link, where the slave address 86H, the function code 06H, the register address 0001H to be written, and the register data 0003H to be written constitute a communication frame from these parameters, the master sends a request for a normal response from the slave, 34 10-bit transmissions on the bus in ASCII mode cause all the slaves to generate 34 reception interrupts, and 16 11-bit transmissions on the bus in RTU mode cause all the slaves to generate 16 reception interrupts and an idle state occupying at least 2 transmission times of 3.5 and 11 bits;
it can be seen that the Modbus communication protocol has the following technical defects in programming implementation and practical use:
1. the frame delimitation method adopted by the Modbus inevitably causes that all slave computers on the bus must generate receiving interruption to each transmitted byte for interception so as to identify the communication frame sent to the slave computers;
2. in the widely used RTU mode, it is also necessary to occupy a special idle timer with a timer of 3.5 transmission times of 11 bits in order to determine the end position of the frame;
3. the communication frame structure adopted by the Modbus ensures that the length of the communication frame is short enough, and excessive useless communication load is occupied;
in summary, the communication efficiency of the Modbus communication protocol is not high, and all the slaves on the bus with a large number of slaves cannot avoid a large amount of additional receiving interrupt processing in the intensive communication, which greatly increases the burden of the slave processor.
Disclosure of Invention
In order to overcome the defects in the prior art. The invention provides a more efficient asynchronous serial port communication protocol compatible with Modbus, which can greatly reduce a large amount of extra receiving interrupt processing generated when a slave computer is not selected by a host computer for communication, and further reduce a large amount of timer interrupt processing because 3.5 idle time keeping of 11-bit transmission time is not needed, and simultaneously can be compatible and coexisted with Modbus-RTU equipment in the same bus and serve the host computer.
The technical scheme adopted by the invention is as follows:
an asynchronous serial port communication protocol compatible with Modbus and more efficient is provided in programs of various central processing units or embedded systems, and communication is implemented between a master device and a slave device by manipulating a universal asynchronous transceiver/transmitter (i.e., an asynchronous serial port) using a communication protocol frame, a frame delimiting method, a communication timing sequence;
the communication protocol frame is used for appointing a group of communication data structures;
the frame delimitation method is used for appointing a setting and judging method for the start and the end of the communication protocol frame;
the communication time sequence is used for appointing the transmission sequence of the communication protocol frame between the host equipment and the slave equipment;
the master device is an active communication device with an asynchronous serial port, does not have an address code in a bus, can actively initiate communication to the slave device in the bus, actively sends a request communication protocol frame, receives a response communication protocol frame, sends an operable function corresponding to a request execution operation instruction code to the slave device through an operation instruction code and an operation result code published by the slave device, and distinguishes the execution result of the operation instruction code according to the operation result code received by the slave device;
the slave device is a passive communication device with an asynchronous serial port, a unique address code and a public broadcast address code are arranged in a bus, communication cannot be actively initiated to any device in the bus, a request communication protocol frame is received, a response communication protocol frame is passively sent, an operational function is set to be an operation instruction code, and an execution result of the operational function is set to be an operation result code and is published to the host device for use.
Further, the communication protocol frame, comprising: a link protocol unit and an application protocol unit;
as shown in fig. 2, part 3 is the overall structure of the communication protocol frame;
the link protocol unit is used for appointing a structure for transmitting the application protocol unit and is a transmission load of an asynchronous serial port;
the application protocol unit is used for appointing a structure for transmitting the operation instruction code, the operation result code and the additional data thereof, and is the transmission load of the link protocol unit.
Further, the link protocol unit sequentially includes, in order of asynchronous serial port transmission: an address domain, a load domain and a check domain;
as shown in fig. 2, part 2 is the structure of the link protocol unit;
the address domain is used for storing the address of the communication equipment, and the minimum length is 1 byte, and the maximum length is 64 bytes;
the load domain is used for storing data load of communication transmission, and the minimum length is 0 byte, and the maximum length is infinite byte;
the check domain is used for storing communication check data, and has the minimum length of 1 byte and the maximum length of 64 bytes.
Further, the application protocol unit sequentially includes, in order of the asynchronous serial port transmission: an operation domain and a data domain;
as shown in fig. 2, part 1 is the structure of the application protocol unit;
the operation domain is used for storing the operation instruction code or the operation result code, and has the minimum length of 1 byte and the maximum length of 64 bytes;
the data field is used for storing additional data of the operation instruction code or the operation result code, and the minimum length is 0 byte, and the maximum length is infinite byte.
Further, a method of delimitation of the frame, comprising: setting method of asynchronous serial port, setting and judging method of frame delimiter;
the setting method of the asynchronous serial port is used for setting the serial port to be suitable for the delimiting method of the frame and transmitting the communication protocol frame by taking bytes as units;
the frame delimiter setting and judging method is used for adding and distinguishing the frame delimiter in the byte stream transmitted by the asynchronous serial port.
Further, the setting method of the asynchronous serial port has different setting modes in different central processing units or embedded systems, but the common characteristics are as follows: the baud rate is fixed or variable, the 11-bit transmission mode and the receiving end of the asynchronous serial port alternately receive in the common receiving mode and the multi-machine receiving mode;
the 11-bit transmission mode includes: 1 start bit, 8 data bits, 1 check bit and 1 stop bit;
the 1 check bit has the functions of: marking whether the currently transmitted 8 data bits are address information or data information;
the common receiving mode has different setting modes, but the common characteristics are as follows: the receiving end of the asynchronous serial port generates a receiving interrupt request when the check is '0' or '1' for each received 8 data bits and 1 check bit;
the multi-machine receiving mode has different setting modes, but the common characteristics are as follows: the receiving end of the asynchronous serial port does not generate a receiving interrupt request when the check bit is '0' for each received 8 data bits and 1 check bit; when the check bit is '1', generating a receiving interrupt request for an asynchronous serial port without a hardware address matching function; when the check bit is '1', for the asynchronous serial port with the hardware address matching function, and when the content of 8 data bits is matched with the preset hardware address, a receiving interrupt request is generated.
Further, the method for setting and judging the frame delimiter comprises the following steps: setting a frame delimiter at an asynchronous serial port sending end, and judging the frame delimiter at an asynchronous serial port receiving end;
the setting step of the frame delimiter at the asynchronous serial port sending end, as shown in the 4 th part of fig. 2, includes:
the method comprises the following steps that 1, the host equipment constructs a request communication protocol frame by using an operation instruction code and additional data thereof, and the slave equipment constructs a response communication protocol frame by using an operation result code and additional data thereof;
setting step 2, setting the check bit of the asynchronous serial port to '1', and marking that the currently transmitted 8 data bits are address information, namely: the frame start character sets 8 data bits of the asynchronous serial port as the first byte content of the communication protocol frame and sends the first byte content in an 11-bit transmission mode of the asynchronous serial port;
setting step 3, setting the check bit of the asynchronous serial port to '0', and marking that the currently transmitted 8 data bits are data information, namely: frame data symbols, 8 data bits of the asynchronous serial port are set as the next byte content of the communication protocol frame and are sent in an 11-bit transmission mode of the asynchronous serial port;
setting step 4, if the last byte of the communication protocol frame is sent in the setting step 3', continuing the step, otherwise, turning to the setting step 3', and continuing to send the next byte of the communication protocol frame;
setting step 5, setting the check bit of the asynchronous serial port to '1', and marking that the currently transmitted 8 data bits are address information, namely: a frame terminator, which sets 8 data bits of the asynchronous serial port as any content and sends the data in an 11-bit transmission mode of the asynchronous serial port;
the frame delimiter judgment step at the asynchronous serial port receiving end comprises the following steps:
judging the host equipment and the slave equipment in the step 1, and presetting 'no receiving' for an asynchronous serial port receiving end after starting;
judging step 2, setting the asynchronous serial port as a 'multi-machine receiving mode' for enabling a frame start symbol to trigger receiving interruption;
judging step 3, setting a receiving counter to '0' for receiving and counting a new communication protocol frame;
judging step 4, setting the asynchronous serial port as 'allowing receiving';
judging step 5, staying in the step until the asynchronous serial port generates a receiving interrupt request;
and 6, entering asynchronous serial port receiving interrupt processing, wherein the processing steps are as follows:
processing step 6.1, setting the asynchronous serial port as a 'common receiving mode' for triggering receiving interruption by a frame data symbol and a frame terminator;
processing step 6.2, accumulating the value of the receiving counter by '1';
processing step 6.3, if the value of the receiving counter is greater than '1' and the check bit is '1', indicating that the frame terminator is received, switching to 'judging step 7';
processing step 6.4, optional step, namely, the step can be skipped for the asynchronous serial port with the hardware address matching function, the software matching is carried out on the address field for the asynchronous serial port without the hardware address matching function, if the address field matching fails, the step is shifted to 'judging step 2', and the new communication protocol frame is received again;
processing step 6.5, storing 8 data bits received by the asynchronous serial port into a communication protocol frame buffer area, and then switching to 'judgment step 5' to wait for receiving next transmitted byte data;
a judgment step 7, setting the asynchronous serial port as 'no receiving';
judging step 8, handing the value of the receiving counter and the content of the stored communication protocol frame to a communication protocol frame processing program;
and 9, after the communication protocol frame processing program is finished, if appropriate, the step 2 is switched to, and the next communication protocol frame is received.
Further, the communication timing sequence, comprising: a communication sequence that the slave device has a response and a communication sequence that the slave device has no response;
the slave device having a communication timing of the reply, comprising:
a1, the host device creates and sends a request communication protocol frame, starts a response timeout timer and waits for a response after program stagnation;
a2, the slave device receives the request communication protocol frame, and if the address field and the check field of the request communication protocol frame have no error, a normal or abnormal response communication protocol frame is created and sent according to the request execution condition;
a3, the host device receives a response communication protocol frame, closes a response overtime timer, relieves program stagnation waiting, analyzes the response communication protocol frame and processes the response information of the slave device;
a communication timing of no reply by the slave device, comprising:
b1, the host equipment creates and sends a request communication protocol frame, starts a response timeout timer and waits for a response after program stagnation;
b2, if the slave device does not receive the request communication protocol frame, or the address field and the check field of the request communication protocol frame have errors, or unexpected power failure, or communication abnormality, or other unexpected abnormality occurs, so that the slave device cannot enable the host device to receive the response communication protocol frame before the response timeout timer expires, the slave device is equivalent to no response of the slave device;
and B3, the host equipment times out the response overtime timer, forcibly relieves program stagnation waiting and processes no-response exception.
The invention has the beneficial effects that:
in the prior art, a frame delimiting method and a communication frame structure adopted by a Modbus cause that the length of a frame is not short enough and all slave machines on the bus must intercept each transmitted byte to generate receiving interruption so as to identify a communication frame sent to the slave machines, and in a widely used RTU mode, an idle timer with a timer of 3.5 and 11 bit transmission time is also required to be specially occupied so as to judge the end position of the frame.
The invention relates to a communication protocol frame and a delimitation method thereof, which do not depend on 3.5 idle time of 11-bit transmission time to delimit the frame and generate receiving interruption of each transmitted byte to intercept the frame sent to the slave, thereby greatly reducing a large amount of additional receiving interruption processing generated by the slave when the slave is not selected by a host for communication and a large amount of timer interruption processing, simultaneously being compatible and coexisting with Modbus-RTU equipment in the same bus and serving the host, and solving the technical problem.
Drawings
FIG. 1 is a diagram of an example of an ASCII and RTU mode communication transmission of Modbus over a serial link.
Fig. 2 shows the structure of a communication protocol frame and its frame delimiter setting in an asynchronous serial byte stream, according to the present invention.
Fig. 3 shows the structure of a communication protocol frame and its frame delimiter setting in an asynchronous serial byte stream according to an embodiment of the present invention.
Detailed Description
Preferred embodiments of the present invention will be described below with reference to the accompanying drawings.
A preferred embodiment of an asynchronous serial port communication protocol compatible with Modbus and more efficient is arranged in programs of various central processing units or embedded systems, and by manipulating a universal asynchronous transceiver (namely, an asynchronous serial port), particularly, a communication protocol frame, a frame delimiting method and a communication timing sequence are used, so that the communication between a master device and a plurality of slave devices in a bus is realized, wherein the master device sends 'request execution' to the slave devices and the slave devices send 'response results' to the master device.
Specifically, the master device is an active communication device with an asynchronous serial port, and can actively initiate communication to the slave devices in the bus without an address code in the bus, and actively sends a request communication protocol frame, receives a response communication protocol frame, and sends an operational function corresponding to the request execution operation instruction code to the slave devices through the operation instruction code and the operation result code published by the slave devices, and distinguishes the execution result of the operation instruction code according to the operation result code responded by the slave devices.
Specifically, the slave device is a passive communication device with an asynchronous serial port, a unique address code and a public broadcast address code are arranged in a bus, communication cannot be actively initiated to any device in the bus, a request communication protocol frame is received, a response communication protocol frame is passively sent, an operational function is set to be an operation instruction code, and an execution result of the operational function is set to be an operation result code and is published to the master device for use.
Specifically, the communication protocol frame is used to agree on a structure of a group of communication data, as shown in part 3 of fig. 3, and includes: a link protocol unit and an application protocol unit.
Specifically, the link protocol unit of the communication protocol frame is used to agree with the structure of the transmission application protocol unit, and is the transmission load of the asynchronous serial port, as shown in part 2 of fig. 3, and sequentially includes, according to the sequence of asynchronous serial port transmission: address domain, load domain, check domain.
Specifically, the application protocol unit of the communication protocol frame is used for agreeing on the structure of transmitting the operation instruction code, the operation result code and the additional data thereof, and is the content of the load domain of the link protocol unit, as shown in part 1 of fig. 3, and sequentially includes, in order of the sequence of asynchronous serial port transmission: an operation domain and a data domain.
Specifically, the address field of the link protocol unit is used for storing the address code of the target slave device when the master device sends a request communication protocol frame, and storing the address code of the slave device when the slave device sends a response communication protocol frame; the length is 1 byte, and the code range is 00H to FFH, wherein the 00H to FEH is the only address code range of a certain slave device in the bus, and the FFH is a common broadcast address code of all the slave devices in the bus.
Specifically, the operation domain of the application protocol unit is used for storing an operation instruction code when the master device sends a request communication protocol frame and storing an operation result code when the slave device sends a response communication protocol frame; the length is 1 byte, and the coding value range is 00H to FFH in hexadecimal, wherein the value range except the following fixed and mandatory operation instruction codes and operation result codes is customized by a user to define specific functions and meanings;
fixed mandatory operation instruction codes, as follows (hexadecimal representation):
00H, the function of the instruction code is 'no operation', the data field is not occupied, and only the broadcast address code is not responded;
01H, the instruction code function is 'reading equipment identification code', the data field occupies 1 byte, and only does not respond to the broadcast address code;
the data field occupies 1 byte, specifically 1 byte equipment identification field code, and the coding value range is 00H to FFH, wherein hexadecimal 00H of the field code is the name of a manufacturer, and the field content is binary content less than or equal to 128 bytes;
wherein, the hexadecimal 01H of the field code is the device code, the field content is the binary content of 6 bytes;
the hexadecimal 02H field code is the equipment version, and the field content is 6 bytes of binary content;
the hexadecimal 03H field code is the product name, and the content of the field is the binary content of less than or equal to 128 bytes;
wherein, the hexadecimal 04H of the field code is an application remark, and the content of the field is the binary content less than or equal to 128 bytes;
wherein, the hexadecimal 05H of the field code is the website of the supplier, and the content of the field is the binary content less than or equal to 128 bytes;
02H, the instruction code function is 'write device communication parameter', the data field occupies 2 bytes, and only does not respond to the broadcast address code;
the data field occupies 2 bytes, specifically, the first byte is slave equipment address code and is stored before (sent first), and the second byte is slave equipment asynchronous serial port baud rate code and is stored after (sent later);
the mandatory operation result code is fixed as follows (hexadecimal representation):
00H, the meaning of the result code is 'null operation success', the result code belongs to normal response, and a data field is not occupied;
01H, the meaning of the result code is 'the execution success of the operation instruction code', the result code belongs to normal response, the data field occupies 0 to 251 bytes, and the content of the result code is data which requires response according to the format of the operation instruction code;
02H, the meaning of the result code is 'invalid operation instruction code', the result code belongs to abnormal response, a data field is not occupied, and when the operation instruction code requested by the host equipment is not set in the slave equipment, the slave equipment responds by using the operation result code;
03H, the meaning of the result code is 'the additional data of the operation instruction code is invalid', the result code belongs to abnormal response, a data field is not occupied, and when the additional data of the operation instruction code requested by the host equipment does not accord with the format published by the slave equipment, the slave equipment uses the operation result code to respond;
04H, the result code meaning is 'operation instruction code execution failure', belonging to abnormal response, the data field is not occupied, when the operation instruction code requested by the host equipment can not successfully execute all sub items in the execution process (for example, the writing of a FLASH register fails, the connection with lower-level equipment can not be realized, and the like), the slave equipment uses the operation result code to respond;
05H, the meaning of the result code is 'operation instruction code execution refusal', the result code belongs to abnormal response, a data field is not occupied, and when the operation instruction code requested by the host equipment conflicts with the operation being executed at the current moment and can not be continuously executed, the slave equipment uses the operation result code to respond.
Specifically, the data field of the application protocol unit is used for storing additional data of an operation instruction code or an operation result code, the minimum length is 0 byte, the maximum length is 251 bytes, and the encoding format is set by the specific operation instruction code.
Specifically, the check field of the link protocol unit is used for storing CRC16 check data, and has a length of 2 bytes, and the specific CRC16 check parameter is: CRC width 16 bits, polynomial hexadecimal 8005H, initial value hexadecimal FFFFH, result xor value hexadecimal 0000H, no inversion of input, no inversion of output, the specific CRC16 check data source is in order: the specific CRC16 check data coding mode comprises an address field, an operation field and a data field: the lower 8 bits of the CRC16 are stored before (transmitted first), and the upper 8 bits of the CRC16 are stored after (transmitted later).
Specifically, the frame delimiting method is a method for setting and judging the start and the end of the appointed communication protocol frame, and comprises the following steps: setting method of asynchronous serial port, setting and judging method of frame delimiter.
Specifically, in the frame delimiting method, the setting method of the asynchronous serial port has different setting modes in different central processing units or embedded systems, but the specific common characteristics are as follows: the baud rate is fixed or variable, the 11-bit transmission mode and the receiving end of the asynchronous serial port alternately receive in the common receiving mode and the multi-machine receiving mode.
Specifically, the 11-bit transmission mode of the asynchronous serial port is as follows: 1 start bit, 8 data bits, 1 check bit, 1 stop bit.
Specifically, the function of 1 check bit of the asynchronous serial port is as follows: whether the currently transmitted 8 data bits are address information or data information is marked.
Specifically, the common receiving mode of the asynchronous serial port has different setting modes, but the common characteristics are as follows: the receiving end of the asynchronous serial port generates a receiving interrupt request when each of 8 received data bits and 1 received check bit is checked to be '0' or '1'.
Specifically, the asynchronous serial port multi-machine receiving mode has different setting modes, but the common characteristics are as follows: the receiving end of the asynchronous serial port does not generate a receiving interrupt request when the check bit is '0' for each received 8 data bits and 1 check bit; when the check bit is '1', generating a receiving interrupt request for an asynchronous serial port without a hardware address matching function, and matching whether an address code of 1 byte is the same as preset or not by software; when the check bit is '1', for the asynchronous serial port with the hardware address matching function, and when the content of 8 data bits is matched with the preset hardware 1 byte address code, a receiving interrupt request is generated.
Specifically, in the frame delimitation method, a method for setting and judging a frame delimiter includes: setting frame delimiter at the sending end of asynchronous serial port, and judging frame delimiter at the receiving end of asynchronous serial port.
Specifically, the step of setting the frame delimiter at the asynchronous serial port transmitting end, as shown in section 4 of fig. 3, includes:
the method comprises the following steps that 1, a request communication protocol frame is constructed by using an operation instruction code and additional data thereof by host equipment, and a response communication protocol frame is constructed by using an operation result code and additional data thereof by slave equipment;
setting step 2, setting the check bit of the asynchronous serial port to '1', and marking that the currently transmitted 8 data bits are address information, namely: the frame start character, setting 8 data bits of the asynchronous serial port as the first byte (namely 1 byte address code) of the communication protocol frame, and sending the data bits in the 11-bit transmission mode of the asynchronous serial port;
setting step 3, setting the check bit of the asynchronous serial port to '0', and marking that the currently transmitted 8 data bits are data information, namely: frame data symbols, wherein 8 data bits of the asynchronous serial port are set as the next byte content of a communication protocol frame and are sent in an 11-bit transmission mode of the asynchronous serial port;
setting step 4, if the last byte of the communication protocol frame is sent in the setting step 3', continuing the step, otherwise, turning to the setting step 3', and continuing to send the next byte of the communication protocol frame;
setting step 5, setting the check bit of the asynchronous serial port to '1', and marking that the currently transmitted 8 data bits are address information, namely: the frame terminator sets 8 data bits of the asynchronous serial port as the first byte (namely: 1 byte address code) of the current communication protocol frame or the first byte (namely: 1 byte address code) of the next communication protocol frame, and transmits the data in the 11-bit transmission mode of the asynchronous serial port.
Specifically, the step of determining the frame delimiter at the asynchronous serial port receiving end includes:
judging the host equipment and the slave equipment in the step 1, and presetting 'no reception' for an asynchronous serial port receiving end after starting;
judging step 2, setting the asynchronous serial port as a 'multi-machine receiving mode' for triggering the receiving interruption by a frame start character (namely, 1 byte address code);
a judging step 3 of setting a receiving counter to '0' for the receiving count of a new communication protocol frame;
judging step 4, setting the asynchronous serial port as 'allowing receiving';
judging step 5, staying in the step until the asynchronous serial port generates a receiving interrupt request;
and 6, entering asynchronous serial port receiving interrupt processing, wherein the processing steps are as follows:
processing step 6.1, setting the asynchronous serial port as a 'common receiving mode' for triggering the receiving interruption by the frame data symbol and the frame terminator;
processing step 6.2, accumulating the value of the receiving counter by '1';
processing step 6.3, if the value of the receiving counter is greater than '1' and the check bit is '1', indicating that the frame terminator is received, then switching to 'judgment step 7';
processing step 6.4, optional step, namely, the step can be skipped for the asynchronous serial port with the hardware address matching function, the software matching is carried out on the 1-byte address code for the asynchronous serial port without the hardware address matching function, if the address field matching fails, the step is shifted to 'judging step 2', and the new communication protocol frame is restarted to be received;
processing step 6.5, storing 8 data bits received by the asynchronous serial port into a communication protocol frame buffer area, and then switching to 'judgment step 5' to wait for receiving next transmitted byte data;
a judgment step 7, setting the asynchronous serial port as 'no receiving';
judging step 8, delivering the value of the receiving counter and the content of the stored communication protocol frame to a communication protocol frame processing program;
and 9, after the communication protocol frame processing program is finished, if appropriate, the step 2 is switched to, and the next communication protocol frame is received.
Specifically, the communication sequence is used to agree on the transmission sequence of the communication protocol frame between the host device and the slave device, and includes: the slave device has a communication sequence of response and a communication sequence of no response.
Specifically, the slave device has a communication timing of the reply, which includes:
a1, a host device creates and sends a request communication protocol frame, starts a response timeout timer and waits for a response after program stagnation;
a2, slave equipment receives a request communication protocol frame, and if the address field and the check field of the request communication protocol frame have no errors, creates and sends a normal or abnormal response communication protocol frame according to the request execution condition;
and A3, the host device receives the response communication protocol frame, closes the response overtime timer, relieves the program stagnation waiting, analyzes the response communication protocol frame and processes the response information of the slave device.
Specifically, the slave device unanswered communication sequence includes:
b1, the host equipment creates and sends a request communication protocol frame, starts a response timeout timer and waits for a response after program stagnation;
b2, if the slave device does not receive the request communication protocol frame, or the address field and the check field of the request communication protocol frame have errors, or unexpected power failure, or communication abnormality, or other unexpected abnormality occurs, so that the slave device cannot enable the host device to receive the response communication protocol frame before the response timeout timer expires, the slave device is equivalent to no response of the slave device;
b3, the host equipment times out the response overtime timer, forcibly relieves program stagnation waiting and processes no-response exception.
The above description is only for illustrating and explaining the preferred embodiment of the present invention, and it should be noted that the preferred embodiment is only one specific implementation manner of the present invention, and any other changes, substitutions or modifications, and implementing part or all of the present invention in software code or by using hardware entity modules are included in the inventive concept and protection scope of the present invention.
Claims (1)
1. The utility model provides a compatible and more efficient asynchronous serial port communication protocol with Modbus which characterized in that:
the communication protocol frame is arranged in programs of various central processing units or embedded systems, and communication is implemented between the host equipment and the slave equipment by manipulating asynchronous serial ports and using a communication protocol frame and a frame delimitation method;
the communication protocol frame is used for appointing a group of communication data structures and comprises a link protocol unit and an application protocol unit;
the link protocol unit is used for appointing a structure for transmitting the application protocol unit, is a transmission load of an asynchronous serial port, and sequentially comprises the following components according to the sequence of asynchronous serial port transmission: an address domain, a load domain and a check domain;
the address domain is used for storing the address of the communication equipment, and the minimum length is 1 byte, and the maximum length is 64 bytes;
the load domain is used for storing data loads of communication transmission, and has the minimum length of 0 byte and the maximum length of infinite byte;
the check domain is used for storing communication check data, and the minimum length of the check domain is 1 byte, and the maximum length of the check domain is 64 bytes;
the application protocol unit is the transmission load of the link protocol unit, and sequentially comprises the following components according to the sequence of asynchronous serial port transmission: an operation domain and a data domain;
the operation domain is used for storing an operation instruction code or an operation result code, and has the minimum length of 1 byte and the maximum length of 64 bytes;
the data field is used for storing additional data of an operation instruction code or an operation result code, and has a minimum length of 0 byte and a maximum length of infinite byte;
the frame delimitation method is used for appointing a setting and judging method of the start and the end of the communication protocol frame, and comprises a setting method of an asynchronous serial port and a setting and judging method of a frame delimiter;
the setting method of the asynchronous serial port is used for setting the serial port to be suitable for the delimiting method of the frame, the communication protocol frame is transmitted by taking bytes as units, different setting modes exist in different central processing units or embedded systems, but the common characteristics are as follows: the baud rate is fixed or variable, the 11-bit transmission mode and the receiving end of the asynchronous serial port alternately receive in the common receiving mode and the multi-machine receiving mode;
the 11-bit transmission mode includes: 1 start bit, 8 data bits, 1 check bit and 1 stop bit;
the 1 check bit has the functions of: marking whether the currently transmitted 8 data bits are address information or data information;
the common receiving mode has different setting modes, but the common characteristics are as follows: the receiving end of the asynchronous serial port generates a receiving interrupt request when 8 data bits and 1 check bit received by each receiving end are checked to be '0' or '1';
the multi-machine receiving mode has different setting modes, but the common characteristics are as follows: the receiving end of the asynchronous serial port does not generate a receiving interrupt request when the check bit is '0' for each received 8 data bits and 1 check bit; when the check bit is '1', generating a receiving interrupt request for an asynchronous serial port without a hardware address matching function; when the check bit is '1', generating a receiving interrupt request for an asynchronous serial port with a hardware address matching function when the content of 8 data bits is matched with a preset hardware address;
the method for setting and judging the frame delimiter is used for adding and distinguishing the frame delimiter in a byte stream transmitted by an asynchronous serial port and comprises a frame delimiter setting step at an asynchronous serial port sending end and a frame delimiter judging step at an asynchronous serial port receiving end;
the frame delimiter is set at the asynchronous serial port sending end, which comprises the following steps:
the method comprises the following steps that 1, a request communication protocol frame is constructed by using an operation instruction code and additional data thereof by a host device, and a response communication protocol frame is constructed by using an operation result code and additional data thereof by a slave device;
setting step 2, setting the check bit of the asynchronous serial port to be '1', and marking that the currently transmitted 8 data bits are address information: the frame start character sets 8 data bits of the asynchronous serial port as the first byte content of the communication protocol frame and sends the first byte content in an 11-bit transmission mode of the asynchronous serial port;
setting step 3, setting the check bit of the asynchronous serial port to '0', and marking that the currently transmitted 8 data bits are data information: frame data symbols, wherein 8 data bits of the asynchronous serial port are set as the next byte content of a communication protocol frame and are sent in an 11-bit transmission mode of the asynchronous serial port;
setting step 4, if the last byte of the communication protocol frame is sent in the setting step 3', continuing the next step, otherwise, turning to the setting step 3', and continuing to send the next byte of the communication protocol frame;
setting step 5, setting the check bit of the asynchronous serial port to '1', and marking that the currently transmitted 8 data bits are address information, namely: a frame terminator, which sets 8 data bits of the asynchronous serial port as any content and sends the data in an 11-bit transmission mode of the asynchronous serial port;
the frame delimiter judgment step at the asynchronous serial port receiving end comprises the following steps:
judging the host equipment and the slave equipment in the step 1, and presetting 'no receiving' for an asynchronous serial port receiving end after starting;
judging step 2, setting the asynchronous serial port as a 'multi-machine receiving mode' for enabling a frame start symbol to trigger receiving interruption;
judging step 3, setting a receiving counter to '0' for receiving and counting a new communication protocol frame;
judging step 4, setting the asynchronous serial port as 'permission to receive';
judging step 5, staying in the step until the asynchronous serial port generates a receiving interrupt request;
and 6, entering asynchronous serial port receiving interrupt processing, wherein the processing steps are as follows:
processing step 6.1, setting the asynchronous serial port as a 'common receiving mode' for enabling the data symbol and the frame terminator to trigger receiving interruption;
processing step 6.2, accumulating the value of the receiving counter by '1';
processing step 6.3, if the value of the receiving counter is greater than '1' and the check bit is '1', indicating that the frame terminator is received, then switching to 'judgment step 7';
processing step 6.4, optional step, namely, the step can be skipped for the asynchronous serial port with the hardware address matching function, the software matching is carried out on the address field for the asynchronous serial port without the hardware address matching function, if the address field matching fails, the step is shifted to 'judging step 2', and the new communication protocol frame is received again;
processing step 6.5, storing 8 data bits received by the asynchronous serial port into a communication protocol frame buffer area, and then switching to 'judgment step 5' to wait for receiving next transmitted byte data;
a judgment step 7, setting the asynchronous serial port as 'no receiving';
judging step 8, delivering the value of the receiving counter and the content of the stored communication protocol frame to a communication protocol frame processing program;
and 9, after the communication protocol frame processing program is finished, if appropriate, the step 2 is switched to, and the next communication protocol frame is received.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911390305.7A CN111124986B (en) | 2019-12-30 | 2019-12-30 | Asynchronous serial port communication protocol compatible with Modbus and high in efficiency |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911390305.7A CN111124986B (en) | 2019-12-30 | 2019-12-30 | Asynchronous serial port communication protocol compatible with Modbus and high in efficiency |
Publications (2)
Publication Number | Publication Date |
---|---|
CN111124986A CN111124986A (en) | 2020-05-08 |
CN111124986B true CN111124986B (en) | 2023-04-18 |
Family
ID=70504589
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201911390305.7A Active CN111124986B (en) | 2019-12-30 | 2019-12-30 | Asynchronous serial port communication protocol compatible with Modbus and high in efficiency |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN111124986B (en) |
Families Citing this family (8)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN114095145B (en) * | 2020-07-31 | 2023-07-14 | 青岛海尔电冰箱有限公司 | Half-duplex communication method for refrigerator and refrigerator |
CN114584630B (en) * | 2020-11-18 | 2023-10-27 | 中移物联网有限公司 | Communication method and device based on field bus protocol |
CN112468382B (en) * | 2020-12-20 | 2021-12-28 | 佛山市微风科技有限公司 | Economical RTU communication method and system based on MODBUS protocol |
CN112637027B (en) * | 2020-12-22 | 2022-11-08 | 北京神经元网络技术有限公司 | Frame boundary defining device based on UART (universal asynchronous receiver/transmitter), transmitting method and receiving method |
CN113132468B (en) * | 2021-03-19 | 2022-02-25 | 珠海科创电力电子有限公司 | Communication method, communication device, communication system and storage medium based on ModbusTCP protocol |
CN113094305B (en) * | 2021-04-02 | 2024-03-26 | 北京黑蚁兄弟科技有限公司 | Asynchronous communication processing method, device and storage medium |
CN114035912B (en) * | 2021-09-28 | 2024-05-31 | 西安空间无线电技术研究所 | Autonomous running method of agile satellite data transmission element task sequence |
CN117155782B (en) * | 2023-09-15 | 2024-06-07 | 江苏舒茨测控设备股份有限公司 | Method for automatically analyzing communication parameters |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US6816935B1 (en) * | 2001-03-02 | 2004-11-09 | Advanced Micro Devices, Inc. | Interrupt and status reporting structure and method for a timeslot bus |
US7443886B1 (en) * | 2002-04-30 | 2008-10-28 | Schneider Automation Inc. | Communication protocol system and method |
CN108197042A (en) * | 2017-12-20 | 2018-06-22 | 北京控制工程研究所 | A kind of universal asynchronous serial and its answer method based on FPGA |
Family Cites Families (6)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101221550A (en) * | 2008-01-30 | 2008-07-16 | 许新朋 | Method and chip for serial communication |
CN201805556U (en) * | 2010-08-30 | 2011-04-20 | 深圳市宏电技术股份有限公司 | Information inquiry device, wireless transmission apparatus and MODBUS slave station |
CN102053935A (en) * | 2010-11-25 | 2011-05-11 | 广东雅达电子股份有限公司 | MODBUS serial communication protocol-based communication method |
CN103645714B (en) * | 2013-12-11 | 2015-11-11 | 东方电气集团东方汽轮机有限公司 | The method of MODBUS asynchronous serial communication protocol is realized with DSP |
CN104580461B (en) * | 2015-01-07 | 2019-03-01 | 四川航天拓鑫玄武岩实业有限公司 | A kind of communication means of more hosts and more slaves |
CN108768844A (en) * | 2018-07-06 | 2018-11-06 | 邯郸职业技术学院 | A kind of wireless device information integrating device for people's air defense command post |
-
2019
- 2019-12-30 CN CN201911390305.7A patent/CN111124986B/en active Active
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US6816935B1 (en) * | 2001-03-02 | 2004-11-09 | Advanced Micro Devices, Inc. | Interrupt and status reporting structure and method for a timeslot bus |
US7443886B1 (en) * | 2002-04-30 | 2008-10-28 | Schneider Automation Inc. | Communication protocol system and method |
CN108197042A (en) * | 2017-12-20 | 2018-06-22 | 北京控制工程研究所 | A kind of universal asynchronous serial and its answer method based on FPGA |
Also Published As
Publication number | Publication date |
---|---|
CN111124986A (en) | 2020-05-08 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN111124986B (en) | Asynchronous serial port communication protocol compatible with Modbus and high in efficiency | |
US7051143B2 (en) | Method, system and program for the transmission of modbus messages between networks | |
CN110851392B (en) | Communication method and communication system based on Modbus RTU protocol | |
US4809217A (en) | Remote I/O port for transfer of I/O data in a programmable controller | |
CN103399546B (en) | Triple redundance control method and system | |
US5958024A (en) | System having a receive data register for storing at least nine data bits of frame and status bits indicating the status of asynchronous serial receiver | |
RU2678715C2 (en) | Data transmission protocol with protocol exception state | |
US8386908B2 (en) | Data transmission methods and universal serial bus host controllers utilizing the same | |
CN103106113A (en) | Interrupt event processing method and processing equipment | |
CN104852825A (en) | Bus communication test method and system | |
TWI336040B (en) | A device for controlling endpoints of usb device and method of controlling endpoints of usb device | |
CN109062850B (en) | Data sending and receiving method of single chip microcomputer | |
CN105630424A (en) | Data processing method, device and system | |
CN113225406A (en) | Communication method of host and multiple slaves | |
CN103577362B (en) | Method for improving data transmission and related computer system | |
CN111736866A (en) | One-to-one and one-to-many compatible online upgrading method and terminal equipment | |
CN113890781B (en) | Communication method, system and storage medium based on timer extension | |
CN105353671B (en) | The pressure of nuclear power plant instrument control system variable and release device and method | |
CN112422485A (en) | Communication method and device of transmission control protocol | |
CN104253667A (en) | System and method for check and feedback of serial synchronous bus for mobile phone platform | |
CN103248547A (en) | Fast data capture method for slave station in Modbus RTU (remote terminal unit) and slave station | |
CN104660372A (en) | Multichannel data transmission method and system based on asynchronous serial port of single chip microcomputer | |
EP4407892A1 (en) | Infrared communication method and apparatus, and storage medium and electronic apparatus | |
CN108038061B (en) | Address allocation method and PLC system | |
CN112559422A (en) | Encoding and decoding implementation method, device and equipment based on USB differential signal line |
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 | ||
GR01 | Patent grant | ||
GR01 | Patent grant |