KROHNE ALTOSONIC V Modbus FR Manuel d'utilisation

Page 52

Advertising
background image

ALTOSONIC V

Manuel Modbus

Page 52 sur 55

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.

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):

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

Advertising