Modbus Protocol Specification



tải về 0.61 Mb.
Chế độ xem pdf
trang33/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

Generating a CRC
Step 1 Load a 16-bit register with FFFF hex (all 1's). Call this the CRC register.
Step 2 Exclusive OR the first eight-bit byte of the message with the low order byte of the 16-bit CRC register, putting the
result in the CRC register.
Step 3 Shift the CRC register one bit to the right (toward the LSB), zerofilling the MSB. Extract and examine the LSB.
Step 4 If the LSB is 0, repeat Step 3 (another shift). If the LSB is 1, Exclusive OR the CRC register with the polynomial value
A001 hex (1010 0000 0000 0001).
Step 5 Repeat Steps 3 and 4 until eight shifts have been performed. When this is done, a complete eight-bit byte will have been
processed.
Step 6 Repeat Steps 2 ... 5 for the next eight-bit byte of the message. Continue doing this until all bytes have been processed.
Result The final contents of the CRC register is the CRC value.
Step 7 When the CRC is placed into the message, its upper and lower bytes must be swapped as described below.
Placing the CRC into the Message
When the 16-bit CRC (two eight-bit bytes) is transmitted in the message, the low order byte will be transmitted first, followed
by the high order byte-e.g., if the CRC value is 1241 hex (0001 0010 0100 0001):
http://www.modicon.com/techpubs/crc7.html (2 of 5) [1/11/2000 10:44:55 PM]


Figure 9 CRC Byte Sequence
Example
An example of a C language function performing CRC generation is shown on the following pages. All of the possible CRC
values are preloaded into two arrays, which are simply indexed as the function increments through the message buffer. One
array contains all of the 256 possible CRC values for the high byte of the 16-bit CRC field, and the other array contains all of
the values for the low byte.
Indexing the CRC in this way provides faster execution than would be achieved by calculating a new CRC value with each
new character from the message buffer.
Note: This function performs the swapping of the high/low CRC bytes internally. The bytes are already swapped in the CRC
value that is returned from the function. Therefore the CRC value returned from the function can be directly placed into the
message for transmission.
The function takes two arguments:
unsigned char *puchMsg ; A pointer to the message buffer
containing
binary data to be used
for
generating the CRC
unsigned short usDataLen ; The quantity of bytes in the 
message
buffer.
The function returns the CRC as a type unsigned short.

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