Analysis of similarities and differences in Modbus RTU/ASCII communication protocols

Modbus has two modes based on serial communication: Modbus RTU and Modbus ASCII mode. Regardless of RTU mode or ASCII mode, Modbus information is transmitted in frames. Each information frame has a determined starting point and ending point, so that the receiving device starts reading the address at the starting point of the information and determines the device to be addressed (host broadcast to all devices), and the end time of information transmission. And some information can be detected, and errors can be set as a result.

The difference between RTU mode and ASCII mode is:

1. The beginning and end signs are different.

The start and end of RTU mode are marked by the idle time required to transmit 3.5 bytes. Assuming the serial port communication rate is 9600bps, the time required to transmit one byte is about one millisecond (8/9600 or /1200 seconds), 3.5 The idle time of bytes is about 3-4 milliseconds, that is, when the baud rate is 9600bps, the RTU transmission idle time is 6-8 milliseconds (the idle time at the end of the previous information frame and the idle time at the beginning of the information frame) to start a New infoframe. The ASCII mode uses fixed ASCII characters to represent the start (:, colon, 3AH in hexadecimal) and the end (CRLF, carriage return-line feed key, 0D and 0AH in hexadecimal).

2. The verification modes are different.

RTU mode uses CRC check code, while ASCII mode uses LRC check code. Relatively speaking, LRC check code is simpler and easier to understand. The LRC check code is to add 1 to all the data in the information frame except the start and end characters (: and carriage return and line feed) by byte superposition. The LRC check code code is as follows:

BYTE GetCheckCode(const char*pSendBuf,int nEnd)//Get LRC check code

{

BYTE byLrc=0;char pBuf[4];int nData=0;

for(i=1;i

{

pBuf[0]=pSendBuf[i];

pBuf[1]=pSendBuf[i 1];

pBuf[2]=”

ssanf(pBuf”%X”,&nData;byLrc =nData);

}

bLrc=~bLrc;

bLrc;

}

The CRC check code is that each eight-bit character is individually ORed with the contents of the register. The result moves to the least significant bit, the most significant bit is filled with 0, and the LSB is 1. The register is ORed with the preset value, LSB If it is 0, it will not proceed. The whole process is repeated 8 times. After the last bit is completed, the next 8-bit byte is ORed with the current value of the register, and the final value of the register is the CRC value. The CRC check code code is as follows:

WORD GetCheckCode(const char *pSendBuf,int nEnd)//Get CRC check code

{

WORD wCrc=WORD(0xFFFF);

for(inti=0;i

{

wCrc^=WORD(BYTE(pSendBuf[i]));

for(int j=0;j<8;j)

{

if(wCrc&1)

{

wCrc>>=1;

wCrc^=0xA001;

}

}

else

{

wCrc>>=1;

}

}

return wCrc;

}

3. The transmission efficiency of RTU mode is higher than that of ASCII mode.

ASCII mode not only needs to add start and end flags, but also needs to convert hexadecimal data into ASCII codes. For example, when hexadecimal 0x25 is converted into ASCII characters, it is 0x32, 0x35. The expression efficiency of ASCII is only half of that of RTU expression. Convert RTU mode instructions to ASCII mode instructions: 1. Remove the CRC check code; 2. Convert all corresponding bytes into corresponding two-byte ASCII characters; 3. Add a start identifier and an end identifier, and Calculate LRC check code plus.

X

Contatto

Contatto