- 23 -
6.7 Sample Computation of CRC-16
Below is the sample computation of CRC-16 using Visual Basic 6.0.
Declare the variable as shown below.
Since the unsigned variable cannot be used in VisualBasic6.0, a signed 16-bit integer variable is used for the data.
Likewise, the computation result of CRC will be placed in the signed 32-bit integer variable.
Dim CRC As Long
Dim i, j, arry_count As Integer
Dim c_next, c_carry As LongDim crc_arry(64) As Integer
Next, data to be computed will be placed in crc_arry() while the quantity of data will be placed in arry_count.
After that, the computation result will be placed in CRC by executing the following program:
i = 0
CRC = 65535
For i = 0 To arry_count
c_next = crc_arry(i)
CRC = (CRC Xor c_next) And 65535
For j = 0 To 7
c_carry = CRC And 1
CRC = CRC \ 2
If c_carry Then
CRC = (CRC Xor &HA001) And 65535
End If
Next
Next
If it is to be attached at the end of the message as an error code, attach the lower byte of CRC before the upper byte.