MODBUS-RTU communication protocol of water meter
This water meter uses MODBUS-RTU communication protocol, which defines the check code and data sequence in detail, which are necessary for specific data exchange. Modbus protocol uses master-slave answer connection (half duplex) on one communication line, which means that the signal is transmitted in two opposite directions on a separate communication line. First, the signal of the host computer is addressed to a unique terminal device (slave), and then the response signal sent by the terminal device is transmitted to the host in the opposite direction.
Modbus protocol only allows communication between the host (PC, PLC, etc.) and terminal devices, and does not allow data exchange between independent terminal devices, so that each terminal device will not occupy the communication line when they are initialized, but only respond to the query signal arriving at the local machine.
MODBUS-RTU format has no start and end characters, but it needs to add waiting time, which is not less than 3.5 characters of sending time.
(1) Transmission mode
The information transmission is asynchronous and in bytes. The communication information transmitted between the master and the slave is in 10 bit word format, including 1 start bit, 8 data bits (the smallest significant bit is sent first), no parity bit, and 1 stop bit.
(2) Data frame format
Address code
Function code
Data area
CRC check code
1 byte
1 byte
N bytes
2 bytes
Address code: the address code is composed of one byte (8-bit binary code) at the beginning of the frame, with decimal system of 0 ~ 255. Only 1 ~ 247 is used in the water meter, and other addresses are reserved. These bits indicate the address of the user specified terminal device, which will receive data from the host connected to it. The address of each terminal device must be unique, and only the addressed terminal will respond to the query containing the address. When the terminal sends back a response, the slave address data in the response tells the host which terminal is communicating with it.
Function code: the function code tells the addressed terminal what function to perform. The following table lists the function codes used by this series of instruments, as well as their meanings and functions.
function
definition
operation
03H
Read data register
Operation of obtaining water volume
05H
Valve control operation
Valve control operation
01H
Read valve status
Read valve status
Data area: the data area contains the data required by the terminal to perform specific functions or the data collected when the terminal responds to queries. The contents of these data may be numerical values, reference addresses or setting values. For example, the function code tells the terminal to read a register, and the data area needs to indicate which register to start from and how many data to read. The embedded address and data vary according to the type and different contents between slaves.
CRC check code: the error check (CRC) field occupies two bytes and contains a 16 bit binary value. The CRC value is calculated by the transmission device and attached to the data frame. The receiving device recalculates the CRC value when receiving data, and then compares it with the value in the received CRC field. If the two values are not equal, an error occurs.
The process of generating a CRC is:
1. Preset a 16 bit register as 0ffffh (all 1), which is called CRC register.
2. XOR the 8 bits of the first byte in the data frame with the low byte in the CRC register, and save the result back to the CRC register.
3. Move the CRC register to the right by one bit, fill the highest bit with 0, and move the lowest bit out and detect.
4. If the lowest bit is 0, repeat the third step (next shift); If the lowest bit is 1, XOR the CRC register with a preset fixed value (0a001h).
5. Repeat steps 3 and 4 until 8 shifts. This completes a complete eight.
6. Repeat steps 2 to 5 to process the next octet until all byte processing is completed.
7. Finally, the value of CRC register is the value of CRC.
In addition, there is another method to calculate CRC by using preset tables. Its main feature is that the calculation speed is fast, but the tables need large storage space. This method will not be repeated here. Please refer to relevant materials.
Detailed explanation of communication application format
(1) Function code 03h: read register
This function allows the user to obtain the data and system parameters collected and recorded by the device. The number of data requested by the host at one time is unlimited, but it cannot exceed the defined address range.
The following example is the basic data collected from the machine reading 1 from 01 (each address in the data frame occupies 2 bytes). The collected data is the total water consumption (occupying 4 bytes), and its address is 00h
Host send
Send message
Slave return
Return information
Address code
01H
Address code
01H
Function code
03H
Function code
03H
Starting address
High byte
00H
Number of bytes
04H
Low byte
00H
Register data
High byte
00H
Number of registers
High byte
00H
Low byte
12H
Low byte
02H
Register data
High byte
D6H
CRC check code
High byte
C4H
Low byte
87H
Low byte
0BH
CRC check code
High byte
44H
Low byte
34H
Attachment: 1. Read write attribute: "R" is read-only, and 03h command is used to read parameters; "R/w" is readable and writable, and 10h command is used to write parameters. It is forbidden to write to addresses that are not listed or have no writable attributes.
2. The communication address and communication rate of the table are given at the factory, and can also be read out through the software provided by our company. It can also be set by protocol.
3. The readings measured in the table account for 4 bytes, and the unit is m3. If the reading value is 0012d687h (1234567), the measurement value is 1234567 × 0.01=12345.67m3。
The address code of the water meter is the lowest two digits of the 8 gray digits on the water meter. For example, the water meter is 42316790, and the Modbus address is 90
2. Valve opening control
Host send
Send message
Slave return
Return information
Address code
01H
Address code
01H
Function code
05H
Function code
05H
Starting address
High byte
00H
High byte
00H
Low byte
00H
Starting address
Low byte
00H
Number of registers
High byte
FFH
Low byte
00H
Register data
High byte
FFH
CRC check code
High byte
89H
Low byte
00H
Low byte
B2H
CRC check code
High byte
44H
Low byte
34H
3. Valve closing control
Host send
Send message
Slave return
Return information
Address code
01H
Address code
01H
Function code
05H
Function code
05H
Starting address
High byte
00H
High byte
00H
Low byte
00H
Starting address
Low byte
00H
Number of registers
High byte
00H
Low byte
00H
Register data
High byte
00H
CRC check code
High byte
48H
Low byte
00H
Low byte
72H
CRC check code
High byte
44H
Low byte
34H
4. Read the valve status
Host send
Send message
Slave return
Return information
Address code
01H
Address code
01H
Function code
01H
Function code
01H
Starting address
High byte
00H
High byte
00H
Low byte
00H
Starting address
Low byte
00H
Number of registers
High byte
00H
Low byte
00H
Register data
High byte
00H/FFH
CRC check code
High byte
48H
Low byte
00H
Low byte
72H
CRC check code
High byte
44H
Low byte
34H
Note: 00h close valve FFH open valve
5. Read the abnormal state of the valve
Host send
Send message
Slave return
Return information
Address code
01H
Address code
01H
Function code
01H
Function code
85H
Starting address
High byte
00H
High byte
00H
Low byte
00H
Starting address
Low byte
00H
Number of registers
High byte
00H
Low byte
00H
Register data
High byte
01H
CRC check code
High byte
48H
Low byte
00H
Low byte
72H
CRC check code
High byte
44H
Low byte
34H