Modbus Protocol Specification



tải về 0.61 Mb.
Chế độ xem pdf
trang32/35
Chuyển đổi dữ liệu14.05.2023
Kích0.61 Mb.
#54683
1   ...   27   28   29   30   31   32   33   34   35
modbus protocol

Step 1 Add all bytes in the message, excluding the starting colon and ending CRLF. Add them into an eight-bit field, so that
carries will be discarded.
Step 2 Subtract the final field value from FF hex (all 1's), to produce the ones-complement.
Step 3 Add 1 to produce the two's-complement.
Placing the LRC into the Message
When the the eight-bit LRC (two ASCII characters) is transmitted in the message, the high order character will be transmitted
first, followed by the low order character-e.g., if the LRC value is 61 hex (0110 0001):
Figure 8 LRC Character Sequence
Example
An example of a C language function performing LRC generation is shown below. The function takes two arguments:
unsigned char *auchMsg ; A pointer to the message buffer 
con-
taining
binary data to be used for
generating
the LRC
unsigned short usDataLen ; The quantity of bytes in the 
message
buffer.
The function returns the LRC as a type unsigned char.
LRC Generation Function
static unsigned char LRC(auchMsg, usDataLen)
http://www.modicon.com/techpubs/crc7.html (1 of 5) [1/11/2000 10:44:55 PM]


unsigned char *auchMsg ; /* message to calculate */
unsigned short usDataLen ; /* LRC upon quantity of */
/*
bytes in message */
{
unsigned char uchLRC = 0 ; /* LRC char initialized */
while (usDataLen--) /* pass through message */ 
uchLRC += *auchMsg++ ; /* buffer add buffer byte*/ 
/*
without carry */
return ((unsigned char)(-((char_uchLRC))) ;
/*
return twos complemen */
}
6.2 CRC Generation
The Cyclical Redundancy Check (CRC) field is two bytes, containing a 16-bit binary value. The CRC value is calculated by
the transmitting device, which appends the CRC to the message. The receiving device recalculates a CRC during receipt of the
message, and compares the calculated value to the actual value it received in the CRC field. If the two values are not equal, an
error results.
The CRC is started by first preloading a 16-bit register to all 1's. Then a process begins of applying successive eight-bit bytes
of the message to the current contents of the register. Only the eight bits of data in each character are used for generating the
CRC. Start and stop bits, and the parity bit, do not apply to the CRC.
During generation of the CRC, each eight-bit character is exclusive ORed with the register contents. The result is shifted in the
direction of the least significant bit (LSB), with a zero filled into the most significant bit (MSB) position. The LSB is extracted
and examined. If the LSB was a 1, the register is then exclusive ORed with a preset, fixed value. If the LSB was a 0, no
exclusive OR takes place.
This process is repeated until eight shifts have been performed. After the last (eighth) shift, the next eight-bit character is
exclusive ORed with the register's current value, and the process repeats for eight more shifts as described above. The final
contents of the register, after all the characters of the message have been applied, is the CRC value.

tải về 0.61 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   27   28   29   30   31   32   33   34   35




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương