modified CCITT CRC16

suria

New member
Joined
Dec 10, 2011
Messages
1
Programming Experience
5-10
Dear All,

Could someone correct the below code. i suppose to get BF52 as CRC for the input. But im not. TIA
'The polynomial for this algorithm is: X**16 + X**12 + X**5 + 1.

the instruction for the CRC as follows :
//------------------------------------------------------------------------------------------------------------------
at beginning,

CRCLSB = 0FFH (octal 377) (decimal 255)
CRCMSB = 0FFH (octal 377) (decimal 255)

then for each data byte in a block,

X = D XOR CRCMSB
X = X XOR ( X >> 4 )
CRCMSB = CRCLSB XOR ( X >> 3 ) XOR ( X << 4 )
CRCLSB = X XOR ( X << 5 )

and at end,

CRCLSB = CRCLSB XOR 0FFH
CRCMSB = CRCMSB XOR 0FFH
//------------------------------------------------------------------------------------------------------------------


code :


Public Class Form1

Private CRCLSB As UShort = &HFF
Private CRCMSB As UShort = &HFF
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim messages As String
Dim encoding As New System.Text.UTF8Encoding()
'Return (Str())
' messages = "<LF>TDIFF<CR><LF>02<CR><LF>G03<CR><LF>06<CR><LF>LY# 0.5 L<CR><LF>MO# 0.0 L<CR><LF>NE# 3.7 <CR><LF>EO# 0.0 <CR><LF>BA# 0.0 <CR><LF>NRBC# 0.0 <CR><LF>G04<CR><LF>06<CR><LF>LY% 11.1 L<CR><LF>MO% 0.9 L<CR><LF>NE% 87.2 H<CR>" '"<LF>EO% 0.7 L<CR><LF>BA% 0.1 L<CR><LF>NRBC% 0.0 <CR><LF><CR><LF><CR><LF>--------------<CR><LF> "
messages = "<CR><LF><CR><LF><CR><LF><CR><LF><CR><LF><CR><LF>--------------<CR><LF>S02<CR><LF>CBC<CR><LF>DIFF<CR><LF>TCBC<CR><LF>04<CR><LF>G01<CR><LF>0A<CR><LF>DATE <CR><LF>TIME 19:05:39<CR><LF>ID1 11177860<CR><LF>CASSPOS A003703<CR><LF>ID1S"
messages = Replace(messages, "<CR>", vbCr)
messages = Replace(messages, "<LF>", vbLf)
messages = Replace(messages, "<STX>", Chr(2))
messages = Replace(messages, "<ETX>", Chr(3))
CRC16(encoding.GetBytes(messages))

End Sub


Public Function CRC16(ByVal input() As Byte) As UShort
Dim crc_16 As UShort = 0

For i As Integer = 0 To input.Length - 1
crc_16 = update_crc_16(crc_16, input(i))

Next

CRCLSB = CRCLSB Xor &HFF
CRCMSB = CRCMSB Xor &HFF
MsgBox(CRCLSB.ToString("X") & "||" & CRCMSB.ToString("X"))
'BF52
Return crc_16
End Function


Private Function update_crc_16(ByVal crc As UShort, ByVal c As UShort)

Dim tmp As UShort, short_c As UShort

short_c = c Xor CRCMSB
short_c = short_c Xor (short_c >> 4)
CRCMSB = CRCLSB Xor (short_c >> 3) Xor (short_c << 4)
CRCLSB = short_c Xor (short_c << 5)


End Function
End Class
 
Top Bottom