Modbus communication protocol and Programming

Source: Internet
Author: User
Modbus communication protocol and Programming
 
Modbus communication protocols are divided into RTU and ASCII protocols. Many of our instruments use Modbus RTU communication protocols, such: ch2000 intelligent power monitor, ch2000m power parameter acquisition module, inspection table, digital display table, and optical column digital display table. The Modbus RTU Protocol is briefly described as follows:

I. Communication Protocols

(1) communication transmission mode:
Communication is divided into independent information headers and sent encoded data. The following communication transmission mode definitions are also compatible with Modbus RTU communication protocols:

Code encoding 8-bit binary
Start position 1 digit
Data bit 8-digit
Parity bit 1 (even check bit)
Stop bit 1 digit
Error Checking CRC (redundant cyclic code)

Initial Structure = Time ≥ 4 bytes
Address Code = 1 byte
Function Code = 1 byte
Data zone = n Bytes
Error Checking = 16-bit CRC code
End structure = Time ≥ 4 bytes

Address Code: The Address Code is the first byte sent by communication. This byte indicates that the slave machine with the address code set by the user will receive the information sent by the host. In addition, each slave machine has a unique address code, and the response is sent with its own address code. The address code sent by the host indicates the slave address to be sent, and the address code sent from the slave server indicates the slave address to be sent back.

Function Code: The second byte transmitted by communication. The Modbus communication protocol defines a function ranging from 1 to 127. This meter only uses part of the function code. Sends a host request and uses a function code to tell the slave about the action. As a slave response, the slave sends the same function code as the slave sends, indicating that the slave has responded to the host for operations. If the highest bit of the Function Code sent from the slave machine is 1 (for example, the function code is larger than 127 at the same time), it indicates that the slave machine does not respond to the operation or the sending fails.

Data Zone: The data zone varies according to different function codes. The data zone can be the actual value, setting point, and address that the host sends to the slave or slave to the host.

   CRC code: Two-byte error detection code.

(2) Communication Protocol:

When the communication command is sent to the instrument, the device that complies with the corresponding address code receives the communication command, removes the address code, and reads the information. If no error occurs, the corresponding task is executed; then, return the execution result to the sender. The returned information includes the address code, the function code for executing the action, the data of the result after the action is executed, and the error verification code. If an error occurs, no information is sent.

1. Information Frame Structure

Address Code Function Code Data Zone Error Verification Code
8-digit 8-digit N × 8 digits 16-bit

Address Code: The Address Code is the first byte (8 bits) of the Information Frame, from 0 to 255. This byte indicates that the slave machine with the address set by the user will receive the information sent by the host. Each slave machine must have a unique address code, and only the slave machine that complies with the address code can respond to the delivery. When sending the information back from the slave, a considerable address code indicates where the information comes from.

   Function Code: The function code sent by the host tells the slave about the task to be executed. The function codes listed in Table 1-1 have specific meanings and operations.

Code Description Operation
03 Read data Reads one or more binary values in the current Register.
06 Reset a single register Write the set binary value to a single register

  Data Zone: The data zone contains the actions to be taken from the machine or the return information collected from the slave machine. The information can be numerical values, reference addresses, and so on. For example, if the function code tells the slave to read the register value, the data zone must contain the starting address and length of the register to be read. For different slave machines, the address and data information are different.

  Error Verification Code: Check code available for the host or slave to identify whether the received information is incorrect. Sometimes, due to electronic noise or other interference, the information will be slightly changed during transmission. The error Verification Code ensures that the host or slave machine does not take effect on the information that is wrong during transmission. This increases system security and efficiency. CRC-16 verification is used for error verification.

Note: the format of information frames is basically the same: Address Code, function code, data zone, and error check code.

2. Error Verification

The redundant cyclic code (CRC) contains two bytes, namely, 16-bit binary. The CRC code is calculated by the sending device and placed at the end of the message. The device that receives the information recalculates the CRC code of the received information to compare whether the Calculated CRC code is consistent with the received one. If the two do not match, an error occurs.

The CRC code is calculated by first configuring 16-bit registers to 1. Then, each 8-bit data is processed gradually. Only 8-bit data bits are used for CRC code calculation. The start bits and stop bits are not involved in CRC code calculation if any parity bits exist.

When the CRC code is calculated, the 8-bit data is different from the register data. The obtained result is shifted to a low byte, and 0 is used to fill the highest bit. Check the priority bit. If the priority bit is 1, the register content is different from the preset number. If the priority bit is 0, no XOR operation is performed.

This process has been repeated for eight times. After the first shift, the next eight bits are the same or as the current Register. This process repeats eight times as above. After all the data information is processed, the content of the last register is the CRC value. The low-byte before sending and receiving data in the CRC code.

To calculate the CRC code, follow these steps:

  • Preset 16-bit registers are hexadecimal FFFF (that is, all are 1 ). This register is called the CRC register;
  • The first 8-bit data is different from the low position of the 16-bit CRC register, and the result is placed in the CRC register;
  • Shifts the register content one to the right (toward the low position), fills the highest bit with 0, and checks the lowest bit;
  • If the percentile is 0: Repeat Step 1 (re-shift); If the percentile is 1: CRC registers are different from the polynomial a001 (3rd 1010 0000) or;
  • Repeat steps 3 and 4 until the right shift is 8 times, so that all the eight-bit data is processed;
  • Repeat steps 2 to 5 to process the next 8-bit data;
  • The final CRC register is the CRC code.

3. function code 03: Read point and return value:

The instrument adopts the Modbus RTU communication protocol, and uses communication commands to perform operations on reading points ("keep registers") or return values ("input registers. Both the holding and input registers are 16-bit (2 bytes) values, and the high is in the front. In this way, both the reading point and the returned value are 2 bytes. The maximum number of readable registers at a time is 60. Some programmable controllers do not need function code 03, so function code 03 is used for reading points and return values. The Command Format of the slave response is the slave address, function code, data zone, and CRC code. The register data in the data area is in front of every two bytes.

4. Function Code 06, single point Storage

The host uses this command to save the single point of data to the memory of the meter. The slave also uses this function code to send information to the host.

Ii. programming examples

The following is an example of Modbus RTU communication written in VC.

(1) communication port settings

DCB;
Hcom = createfile ("COM1 ",
Generic_read | generic_write,
0,
Null,
Open_existing,
0,
Null );
If (hcom = invalid_handle_value)
{
MessageBox ("createfile error, error ");
}
Bool error = setupcomm (hcom );
If (! Error)
MessageBox ("setupcomm error ");
Error = getcommstate (hcom, & DCB );
If (! Error)
MessageBox ("getcommstate, error ");
DCB. baudrate = 2400;
DCB. bytesize = 8;

DCB. Parity = evenparity; // noparity;
DCB. stopbits = onestopbit;

Error = setcommstate (hcom, & DCB );

(2) CRC verification code calculation

Uint CRC
Void calccrc (byte crcbuf)
{
Byte I;

CRC = CRC ^ crcbuf;
For (I = 0; I <8; I ++)
{
Byte tt;
Tt = CRC & 1;
CRC = CRC> 1;
CRC = CRC & 0x7fff;
If (TT = 1)
CRC = CRC ^ 0xa001;
CRC = CRC & 0 xFFFF;
}
}

(3) Data Transmission

Zxaddr = 11; // read inspection table data with the address of 11
Zxnum = 10; // read data from 10 channels

Writebuf2 [0] = zxaddr;
Writebuf2 [1] = 3;
Writebuf2 [2] = 0;
Writebuf2 [3] = 0;
Writebuf2 [4] = 0;
Writebuf2 [5] = zxnum;
CRC = 0 xFFFF;
Calccrc (writebuf2 [0]);
Calccrc (writebuf2 [1]);
Calccrc (writebuf2 [2]);
Calccrc (writebuf2 [3]);
Calccrc (writebuf2 [4]);
Calccrc (writebuf2 [5]);

Writebuf2 [6] = CRC & 0xff;
Writebuf2 [7] = CRC/0x100;
Writefile (hcom, writebuf2, 8, & comnum, null );

(4) Data Reading

Readfile (hcom, writebuf, 5 + zxnum * 2, & comnum, null); // read data from zxnum Channels
You can add error handling programs, such as address code errors, CRC code errors, and communication fault handling.

 

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.