Question CRC-CCITT Calculation

lukemeyers

New member
Joined
May 20, 2009
Messages
3
Programming Experience
1-3
Hello everybody

I'm new to this forum so please be kind ;)

I'm currently writing a small application for a serial device and need a CRC-CCITT (inverted) for the outgoing data.

The following code works fine:

VB.NET:
Expand Collapse Copy
    Public Function getCRC16(ByVal strInput As String)

        Dim lngCheck As Long
        Dim Power(7) As Integer
        Dim I As Integer
        Dim J As Integer
        Dim Poly As Long
        Dim CRC As Long
        Dim TestBit As Boolean
        Dim TestBit1 As Boolean
        Dim TestBit2 As Boolean

        Poly = &H1021
        CRC = &HFFFF

        For J = 0 To 7
            Power(J) = 2 ^ J
        Next J

        For I = 1 To Len(strInput)
            lngCheck = Asc(Mid$(strInput, I, 1))
            For J = 7 To 0 Step -1
                If (CRC And 32768) = 32768 Then
                    TestBit1 = True
                Else
                    TestBit1 = False
                End If

                If (lngCheck And Power(J)) = Power(J) Then
                    TestBit2 = True
                Else
                    TestBit2 = False
                End If

                TestBit = TestBit1 Xor TestBit2
                CRC = (CRC And 32767) * 2
                If TestBit = True Then
                    CRC = CRC Xor Poly
                End If

            Next J
        Next I

        Dim tmp As String
        tmp = Hex(CRC)
        getCRC16 = tmp

    End Function

Is it somehow possible to change the input from String to an Bytearray? I tried it several times, but the line

lngCheck = Asc(Mid$(strInput, I, 1))

is making problems.

And how would you invert all bytes after the checksum is built?

Thanks for your help.

Luke
 
thx picoflop for your fast reply. unfortunately, your solution with lngcheck doesn't work for me.

when I debug the application, everything works fine, but the calculated CRC is now wrong. What could be the reason for this?

Thanks for your help.
 
does anybone have an idea? i got really stuck with this little thing...

My code looks now like this:

VB.NET:
Expand Collapse Copy
Public Class Form1
    Public z As Integer = -1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Do
            Dim input As String
            Dim array As Object = Nothing
            Dim x As Integer = 0

            input = InputBox("Insert String")

            input = Replace(input, " ", "")
            input = Replace(input, "%", "")

            For i = 0 To input.Length - 1 Step 2
                ReDim array(x)
                array(x) = "&H" & input.Substring(i, 2)
                MsgBox(array(x))
                x = x + 1
                z = z + 1
            Next

            getCRC16(array)

        Loop

    End Sub

    Public Function getCRC16(ByVal input As Object)

        Dim lngCheck As Long
        Dim Power(7) As Integer
        Dim I As Integer
        Dim J As Integer
        Dim Poly As Long
        Dim CRC As Long
        Dim TestBit As Boolean
        Dim TestBit1 As Boolean
        Dim TestBit2 As Boolean

        Poly = &H1021
        CRC = &HFFFF

        For J = 0 To 7
            Power(J) = 2 ^ J
        Next J

        For I = 1 To z
            lngCheck = input(I - 1)
            For J = 7 To 0 Step -1
                If (CRC And 32768) = 32768 Then
                    TestBit1 = True
                Else
                    TestBit1 = False
                End If

                If (lngCheck And Power(J)) = Power(J) Then
                    TestBit2 = True
                Else
                    TestBit2 = False
                End If

                TestBit = TestBit1 Xor TestBit2
                CRC = (CRC And 32767) * 2
                If TestBit = True Then
                    CRC = CRC Xor Poly
                End If

            Next J
        Next I
        Dim tmp As String
        tmp = Hex(CRC)
        MsgBox("CRC: " & tmp)
        getCRC16 = tmp

    End Function

End Class

Unfortutanetly, the calculation is now wrong (the failure starts at the second place of the CRC) and I think the reason for this must be in one of my "for ... to" loops.

Luke
 
Last edited:
Back
Top