Urgent help :CRC-CCITT help needed

i am developing a VB code to generate CRC-CCITT for my data frame
which is
(0x01 0x10 0x01 0x20 0x02 0xF0 0x04 0x02 0x23 0x10 0x63 0x02 0x03 0xFA 0x33 0x04)
so if i send 0x01 0x10 0x01 0x20 0x02 0xF0 0x04 0x02 0x23 0x10 0x63 0x02 0x03 to a crc-ccitt function i should get crc 0xFA 0x33 which i will add to my data and put an end of header then send via comm port

this what have tried but i am not getting 0xFA 0x33 as required by the protocol


'************************************************************************************
'test with this hex string 0110012002F004022310630203 i am suppossed to get crc FA33
' Or testing with 0110012016F004022310590203 should give crc 5F06
' CRC-CCITT Polynomial: 1021h = x16 + x12 + x5 + 1 (LSB first mode)Start value: FFFFh
'******************************************************************************************


Private Sub CommandButton1_Click()
Dim crc As Long
Dim n As String
Dim values(13) As Byte
Dim k As Integer
Dim i As Integer

crc = &HFFFF&
n = text1.Text

k = 0
For i = 1 To Len(n)
values(k) = "&H" & Mid(n, i, 2)
i = i + 1
k = k + 1
Next i

For k = 0 To 12
crc = calc_crc(crc, values(k))
Next k
MsgBox ("Crc " & Hex(crc))

End Sub

Private Function calc_crc(ByVal crc_buff As Long, ByVal inp As Byte) As Long
Dim i As Integer
Dim x16 As Long
Dim values(13) As Byte
For i = 0 To 12

If ((crc_buff And 1) Xor (inp And 1)) = 1 Then
x16 = &H8408&
Else
x16 = 0
End If
crc_buff = crc_buff 2
crc_buff = crc_buff Xor x16
inp = inp 2
Next i
calc_crc = crc_buff
End Function

please urgent
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories