none
8-bit CRC code in Visual Basic RRS feed

  • Question

  • Hi

    I want to check the CRC generated by the sample data with 8-bit CRC VB code. We have 24bits hex data along with 8 bit CRC is generating. Is there any code snippet for CRC verification.

    Ex: 29CAC351

    29CAC3 is 24bit data.

    51 is the 8-bit CRC

    Regards

    B Teja


    B TEJA

    Saturday, May 5, 2018 5:46 AM

All replies

  • I want to check the CRC generated by the sample data with 8-bit CRC VB code.

    There are many different ways of implementing a CRC.  You should indicate the algorithm to be used in this example, and also data types.  For instance, is 29CAC3 provided as a string, a number or a byte array, and is the result returned as a string, number or byte array?  See: https://en.wikipedia.org/wiki/Cyclic_redundancy_check

    Saturday, May 5, 2018 6:25 AM
  • Well I certainly couldn't do it but I found code here Checksum calculation in vb.net from dbasnett that can do it. But the enumerator has CRC8 and CRC8ccitt and I had to use CRC8ccitt to retrieve 81 which is the value of 51 hex in "29CAC351" converted to Decimal or 81 decimal.

    I could've had the MessageBox display 51 hex by using the Hex Function but I forgot about it.

    The link has the rest of the code for all the CRC checks listed in the enumerator.

    Option Strict On
    
    Public Class Form1
    
        Dim s As String = "29CAC351"
        '29 hex = 41
        'CA hex = 202
        'C3 hex = 195
        '51 hex = 81 - CRC
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        End Sub
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim CRCCheck As New List(Of Byte)
            For i = 0 To s.Count - 3 Step 2
                CRCCheck.Add(CByte(Convert.ToInt32(s.Substring(i, 2), 16)))
            Next
            Dim b() As Byte = CreateCRC(CRCCheck.ToArray, 0, 0)
            MessageBox.Show(b(0).ToString)
        End Sub
    
        Public base As New CRCbaseTable(Of Byte)(PolyDef.CRC8ccitt)
    
        Public Function CreateCRC(data As Byte(), Optional InitialCRCVal As Byte = 0, Optional FinalXOR As Byte = 0) As Byte()
            Dim crc As Byte = InitialCRCVal
            For Each b As Byte In data
                'position in table
                Dim pos As Byte = b Xor crc
                crc = Me.base.crcTable(pos)
            Next
            Me.base.LastComputedCRC = crc Xor FinalXOR
            Return BitConverter.GetBytes(Me.base.LastComputedCRC)
        End Function
    
        Public Enum PolyDef As ULong
            CRC8 = &HD5UL
            CRC8ccitt = &H7UL
            CRC16ccitt = &H1021UL
            CRC32 = &H4C11DB7UL
            CRC32C = &H1EDC6F41UL
            CRC64 = &H1BUL
            CRC64ecma = &H42F0E1EBA9EA3693UL
            CRC64iso = &H1BUL
        End Enum
    
    
        Public Class CRCbaseTable(Of T)
            Public ReadOnly polynomial As T
            Private allBytes() As Byte 'temp for bytes 0 - 255
            Public ReadOnly shift As Integer = 0
            Public ReadOnly msbMask As T 'most significant bit
            Private typMask As T 'type mask 
            Public LastComputedCRC As T 'CRC
            Private typeOfT As Type 'T's type
            Public ReadOnly crcTable() As T = New T(255) {} 'CRC lookup table
    
            Public Sub New(PolynomialToUse As PolyDef)
                Me.allBytes = Enumerable.Range(0, 256).Select(Function(i) As Byte
                                                                  Return Convert.ToByte(i)
                                                              End Function).ToArray
    
                Me.typeOfT = Type.GetType(GetType(T).ToString)
                Me.polynomial = CType(Convert.ChangeType(PolynomialToUse, Me.typeOfT), T)
                Select Case GetType(T)
                    Case GetType(Byte)
                        Me.typMask = CType(Convert.ChangeType(Byte.MaxValue, Me.typeOfT), T)
                        Me.shift = 8
                    Case GetType(UInt16)
                        Me.typMask = CType(Convert.ChangeType(UInt16.MaxValue, Me.typeOfT), T)
                        Me.shift = 16
                    Case GetType(UInt32)
                        Me.typMask = CType(Convert.ChangeType(UInt32.MaxValue, Me.typeOfT), T)
                        Me.shift = 32
                    Case GetType(UInt64)
                        Me.typMask = CType(Convert.ChangeType(UInt64.MaxValue, Me.typeOfT), T)
                        Me.shift = 64
                    Case Else
                        Stop 'error
                End Select
                'create most significant bit mask
                Me.msbMask = CType(Convert.ChangeType(1UL << Me.shift - 1, Me.typeOfT), T)
                Me.shift -= 8 'shift is - 8 of width
                'build look up table .crcTable
                Dim curByte As UInt64
                Dim tmsbMask As UInt64 = Convert.ToUInt64(Me.msbMask)
                Dim tpoly As UInt64 = Convert.ToUInt64(Me.polynomial)
                Dim ttypMask As UInt64 = Convert.ToUInt64(Me.typMask)
                For x As Integer = 0 To Me.allBytes.Length - 1
                    curByte = Convert.ToUInt64(Me.allBytes(x)) << Me.shift
                    For bit As Byte = 0 To 7
                        If (curByte And tmsbMask) <> 0UL Then
                            curByte <<= 1 'left shift 1
                            curByte = curByte Xor tpoly
                        Else
                            curByte <<= 1
                        End If
                    Next
                    curByte = curByte And ttypMask
                    Me.crcTable(x) = CType(Convert.ChangeType(curByte, Me.typeOfT), T)
                Next
                Array.Resize(Me.allBytes, 0) '.allBytes not needed after constructor
    
            End Sub
    
            Public Function ViewTable() As String
                Dim pad As Integer
                Select Case Me.typeOfT
                    Case GetType(Byte)
                        pad = 2
                    Case GetType(UInt16)
                        pad = 4
                    Case GetType(UInt32)
                        pad = 8
                    Case GetType(UInt64)
                        pad = 16
                End Select
    
                Dim sb As New System.Text.StringBuilder
                sb.AppendFormat("Polynomial  0x{0} ", Convert.ToUInt64(Me.polynomial).ToString("x").PadLeft(pad, "0"c))
                sb.AppendLine()
                For x As Integer = 0 To 31
                    For y As Integer = x * 8 To x * 8 + 7
                        Dim curByte As UInt64 = Convert.ToUInt64(Me.crcTable(y))
                        sb.AppendFormat("0x{0} ", curByte.ToString("x").PadLeft(pad, "0"c))
                    Next
                    sb.AppendLine()
                Next
                Return sb.ToString.Trim
            End Function
        End Class
    
    End Class


    La vida loca

    Saturday, May 5, 2018 8:45 PM