none
CRC16 struggle vb.net RRS feed

  • Question

  • Hello everyone. I want to figure out how to  CRC16 XMODEM works and write a code for it. it will calculate from 3 to 18bytes and calls with the button,  it will take HEX values  then show a result in hex value aswell. For example: 0x05 0x02 0xAA 0xAA - will be 0x3430 accrording to http://crccalc.com/ - and this is correct. But how to implement this with code , does anyone have any info please?



    • Edited by Raffy_Raff Tuesday, February 6, 2018 6:30 AM
    Tuesday, February 6, 2018 6:30 AM

Answers

  • I found this little bit edited it, and seems it works

     
    Option Strict On
    Imports System.Security.Cryptography ' only used to generate some test data
    Public Class Form1
        Dim TXTBytes As New TextBox
        Dim TXTCRC As New TextBox
        Dim BTNGo As New Button
        Function calcrc(ByVal data() As Byte, ByVal count As Integer) As Integer
            Dim crc As Integer = 0 ' usually initialized as &HFFFF ???
            For Each b As Byte In data
                Dim d As Integer = CInt(b)
                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
            Return crc And &HFFFF
        End Function
        Private Sub BTNClick(sender As System.Object, e As System.EventArgs)
            TXTBytes.Clear()
            TXTCRC.Clear()
            Dim b(255) As Byte
            b = {5, 2, &HAA, &HAA}
            TXTCRC.Text = calcrc(b, b.Length).ToString("X4")
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Width = 750
            Me.Height = 250
            With TXTBytes
                .Top = 0
                .Left = 0
                .Height = 150
                .Width = Me.ClientRectangle.Width
                .Multiline = True
                .Font = New System.Drawing.Font("Courier New", 10)
            End With
            Me.Controls.Add(TXTBytes)
            With BTNGo
                .Left = 0
                .Top = 160
                .Width = 50
                .Height = 25
                .Text = "Get CRC"
            End With
            Me.Controls.Add(BTNGo)
            AddHandler BTNGo.Click, AddressOf BTNClick
            With TXTCRC
                .Top = 195
                .Left = 0
                .Height = 25
                .Width = 100
                .Multiline = False
                .Font = New System.Drawing.Font("Courier New", 10)
            End With
            Me.Controls.Add(TXTCRC)
        End Sub
    End Class

    Tuesday, February 6, 2018 6:47 AM

All replies

  • I found this little bit edited it, and seems it works

     
    Option Strict On
    Imports System.Security.Cryptography ' only used to generate some test data
    Public Class Form1
        Dim TXTBytes As New TextBox
        Dim TXTCRC As New TextBox
        Dim BTNGo As New Button
        Function calcrc(ByVal data() As Byte, ByVal count As Integer) As Integer
            Dim crc As Integer = 0 ' usually initialized as &HFFFF ???
            For Each b As Byte In data
                Dim d As Integer = CInt(b)
                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
            Return crc And &HFFFF
        End Function
        Private Sub BTNClick(sender As System.Object, e As System.EventArgs)
            TXTBytes.Clear()
            TXTCRC.Clear()
            Dim b(255) As Byte
            b = {5, 2, &HAA, &HAA}
            TXTCRC.Text = calcrc(b, b.Length).ToString("X4")
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Width = 750
            Me.Height = 250
            With TXTBytes
                .Top = 0
                .Left = 0
                .Height = 150
                .Width = Me.ClientRectangle.Width
                .Multiline = True
                .Font = New System.Drawing.Font("Courier New", 10)
            End With
            Me.Controls.Add(TXTBytes)
            With BTNGo
                .Left = 0
                .Top = 160
                .Width = 50
                .Height = 25
                .Text = "Get CRC"
            End With
            Me.Controls.Add(BTNGo)
            AddHandler BTNGo.Click, AddressOf BTNClick
            With TXTCRC
                .Top = 195
                .Left = 0
                .Height = 25
                .Width = 100
                .Multiline = False
                .Font = New System.Drawing.Font("Courier New", 10)
            End With
            Me.Controls.Add(TXTCRC)
        End Sub
    End Class

    Tuesday, February 6, 2018 6:47 AM
  • Hi Raffy_Raff,

    Glad to hear this issue has been solved by yourself. Thanks for your sharing, I will introduce this experience to other forum users who face the same condition. Please mark your reply to close this case.

    Best regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 12, 2018 9:12 AM
    Moderator