Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to calculate the CRC of some telegrams in VB2008 but not sure if it is X or Zmodem. And honestly I cant understand the algorithm because i've seen some with lookup tables, other with polinomes, shifts...
These examples of telegrams i'm using and the data used in the CRC calculation is between 0x02 and 0x04 (02 and 04 excluded).

STX ADDR DATA EOT CRCH CRCL ETX
02 38 51 4D 4E 30 30 23 04 F1 D7 03

02 38 51 4D 4E 31 30 04 EC CC 03

02 38 51 4D 4E 32 30 52 04 27 D7 03

I wonder if there aren't any flowcharts explaining how to calculate the CRC, or a DLL i can use, or even the code in VB.
If anyone can help me with this...

Regards.
Posted

1 solution

Here you are the VB function (very C-like code: it is a direct porting of the 'ufficial' C routine, see [^]).
If efficiency matters, consider porting a table-based one.
VB
Function calcrc(ByVal data() As Byte, ByVal count As Integer) As Integer
    Dim crc As Integer = 0
    Dim i, j As Integer
    Dim d As Integer

    For i = 0 To count - 1
        d = data(i)
        crc = crc Xor (d << 8)
        For j = 0 To 7
            If ((crc And &H8000) <> 0) Then
                crc = (crc << 1) Xor &H1021
            Else
                crc = (crc << 1)
            End If
        Next
    Next
    calcrc = crc And &HFFFF
End Function


The following code is a test with your first chunk of data:

VB
Sub Main()
    'Please note data(6) declare 7 items (OK, that's VB madness)
    Dim data(6) As Byte
    Dim crc As Integer
    data(0) = &H38
    data(1) = &H51
    data(2) = &H4D
    data(3) = &H4E
    data(4) = &H30
    data(5) = &H30
    data(6) = &H23
    crc = calcrc(data, 7)
End Sub


:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900