Crc-16 calculations, Crc-16 calculations -11, Crc-16 calculation example in basic – Yaskawa F7 Modbus User Manual

Page 28

Advertising
background image

Message Formats 2-11

CRC-16 Calculations

The last two bytes of a message contain the CRC-16 (Cyclical Redundancy Check). The CRC-16 is one method for verifying the validity of the

message contents and is part of the protocol. The CRC-16 field checks the contents of the entire message, regardless of any parity check method

used for the individual characters of the message.
The CRC-16 field is a 16-bit binary value consisting of two 8 bit bytes. The CRC-16 value is calculated by the transmitting device, which

appends the CRC-16 to the message. The receiving device recalculates a CRC-16 during receipt of the message, and compares this calculated

value to the value received in the transmitted CRC-16 field. If the two values are not equal, the entire message is invalid.
Detailed examples of a CRC-16 generation using Quick Basic and C are shown below.

CRC-16 Calculation Example in Basic

crcsum# = &HFFFF&
crcshift# = &H0&
crcconst# = &HA001&
CLS
PRINT “***************************************************”
PRINT
PRINT “ CRC-16 calculator”
PRINT
PRINT “***************************************************”
PRINT “If entering data in hex, preceed the data with ‘&H’”
PRINT “ Example: 32decimal = 20hex = &H20”
PRINT “***************************************************”
PRINT
INPUT “Enter the number of bytes in the message: “, maxbyte
FOR bytenum = 1 TO maxbyte STEP 1

PRINT “Enter byte “; bytenum; “:”:
INPUT byte&
byte& = byte& AND &HFF&
crcsum# = (crcsum# XOR byte&) AND &HFFFF&
FOR shift = 1 TO 8 STEP 1

crcshift# = (INT(crcsum# / 2)) AND &H7FFF&
IF crcsum# AND &H1& THEN

crcsum# = crcshift# XOR crcconst#

ELSE

crcsum# = crcshift#

END IF

NEXT shift

NEXT bytenum
lower& = crcsum# AND &HFF&
upper& = (INT(crcsum# / 256)) AND &HFF&

PRINT “Lower byte (1

st

) = “, HEX$(lower&)

PRINT “Upper byte (2

nd

) = “, HEX$(upper&)

Figure 2.1 – CRC-16 Calculation in Quick Basic

Advertising
This manual is related to the following products: