Modbus Protocol Handout

Source: Internet
Author: User

Modbus an industry-commonly used communication protocol, a communication convention. Modbus protocol includes RTU, ASCII, TCP. One of the most commonly used modbus-rtu, relatively simple, on a single-chip computer is easy to achieve. Although the RTU is relatively simple, but look at the protocol materials, manuals said too professional, at first a lot of content is difficult to understand.
What are the so-called agreements? Is the agreement between each other, if not let others know that is the signal. Now it's time to define a new, simplest protocol. For example

Protocol: "A"--"led off"
"B"--"alarm"
"C"--"led bright"
SCM received "a" control an led off, single-chip microcomputer received "B" control Alarm, SCM received "a" control of a led on light. Then when the corresponding information is received to perform the corresponding action, this is the agreement, very simple.

First, a simple analysis of a MODBUS-RTU message, for example: 01 06 00 01 00 17 98 04
01 06 00 01 00 17 98 04
Slave address function Number data address data CRC checksum

This string of data means: write the data 0x0017 (decimal 23) to the 1th slave address 0x0001 the data address.

Figure out what's going on here.

1. Message
A message is a frame of data, a data frame is a message: refers to a string of complete instruction data, like a string of data above.

2. CRC check
Meaning: For example, the above 98 04 is the data in front of it (01 06 00 01 00 17) The results calculated by an algorithm (see Appendix 2, very simple) are actually calculated as cumulative and so on. (Additive sum: is the value of 010600010017 Plus, then its algorithm is addition).
Role: In the data transmission process may occur error, CRC check the data received is correct. For example, the host issued 01 06 00 01 00 17 98 04, then from the machine received after receiving the CRC check value according to 01 06 00 01 00 17, the slave determines whether the CRC check itself is equal to the received CRC (98 04 host computed), if not equal then the data The data cannot be transmitted with errors.

3. Function No.
Meaning: Modbus definition. See Appendix 1.
Function: Indicates the specific operation.

Modbus-rtu
One message analysis
First of all to declare our purpose, we are to two devices communication, with the Modbus protocol. The above briefly describes: "Message" "CRC check" "function number".

In the microcontroller to take out a portion of memory (RAM) for two device communication, such as:
A comment following the array, explaining
OX[20] represents the output coil, accessed with the function code 0x01,0x05,0x0f, the opening address is 0 (this follow-up instructions)
IX[20] represents the input coil, accessed with the function code 0x02, the opening address is 1 (this follow-up instructions)
The other two are the same truth.
Note: The so-called "coil" "register" is the "bit variable" "16-bit variable", do not be confused. "Coil" I think it should be for the application of equipment, Modbus protocol is specifically for the 485 bus equipment (Example PLC) developed.

1, the host to the slave write data operation
If a single-chip computer receives a message then the message is parsed to perform the corresponding processing, such as the above message:
01 06 00 01 00 17 98 04
Slave address function Number data address data CRC checksum

If the local address is 1, then the microcontroller received this string of data based on the data to calculate the CRC check whether the data is correct, if the data is correct, then the result is:
HOLDDATAREG[1] = 0x0017;
The Modbus host completes a write operation to the slave data, and realizes the communication.

2, host to slave read data operation
Host for read Holddatareg[1] operations, the message is:
D5 CA
Read the number of data from the machine address function Number data address CRC checksum
Then the SCM received this string of data based on the data to calculate the CRC check whether the data is correct, if the data is accurate, the result is: return information to the host, the returned information is also formatted:
Return content:
Geneva 0017 F8 4 A
Slave address function Number data byte number two byte data CRC checksum
The Modbus host completes a read operation of the slave data and realizes the communication.


Two, Modbus message model

The above understanding of the Modbus frame of the message is how to communicate, in fact, the format of each message is basically the same.




Here are two acronyms not known before, but now to understand what is meant, "ADU" "PDU"
ADU: Application Data unit
PDU: Protocol Data Unit

Three, Modbus data model




Four, Modbus transaction processing
The following state diagram describes the general processing process for Modbus transactions on the server side.


Five, Modbus request and response
See Modbus Protocol Manual, Chinese 10th page start, English 24th page start. The manual gives a very detailed example of the request and response of each function number of the Modbus protocol.



The realization process of Modbus protocol on single chip microcomputer


MODBUS Task handling function


function, Rcvbuf is the serial receive buffer, if received a message, rcvbuf[0] is the slave address, rcvbuf[0] is the Modbus function number. The response is based on the function number, and the specific operation is performed in the respective function according to the function number, which is equivalent to parsing the received data.

Appendix 1:modbus-rtu function Codes
Most commonly used function code:
The following "coil" "register" is actually the "bit variable" "16-bit variable"
(0x01) Read coil
(0x02) Read discrete input
(0x03) Read hold Register
(0x04) Read input register
(0x05) write a single coil
(0x06) write a single register
Write multiple coils (0x0F)
(0x10) Write multiple registers


Appendix 2: CRC Generation

Modbus Protocol Handout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.