Computer Interfacing
Discussions about interfacing and electronics

CRC16 Modbus in HEX Basics


       Computer Interfacing Forum Index -> Error detection and correction
Author Message
New User

Joined: 16 Jan 2008
Posts: 2

Jan 17, 2008 8:47 pm


I've got a module Visual Basic which is calculating for me the CRC16-Modbus in ASCII.
I need the result in HEX format but it doesn't work! If I change my program to HEX, I only
can calculate the first digit, later on my program doesn't calculate any serious result.!?
Example: 0xC at me and Lammert = 0x45BF
0xCE at me = 0x7485, at Lammert = 0xD43E

I've tried already a lot but I cannot find the right change of the code.

Can anybody tell me why?
Attached please find my VB-code, perhaps somebody is used to this and may help me.
Sub CRC16()
Dim x As Long
Dim mask, i, j, nC, Crc As Integer
Dim c As String

txt = "CA00F7"

Crc = &HFFFF ' crc mit $ffff initalisieren

For nC = 1 To Len(txt)
    j = Val("&H" + Mid(txt, nC, 1)) 'im HEX-Format
    Crc = Crc Xor j
    For j = 1 To 8
        mask = 0
        If Crc / 2 <> Int(Crc / 2) Then mask = &HA001
        Crc = Int(Crc / 2) And &H7FFF: Crc = Crc Xor mas
    Next j
Next nC

txt = Hex$(Crc) 'Checksumme

End Sub

Thx Lobo
Preferred Member

Joined: 30 Oct 2007
Posts: 184
Location: London, UK

Jan 17, 2008 9:52 pm

Hello Lobo, and welcome.

Everything's fine with your code, with only two changes:
For nC = 1 To Len(txt) Step 2
    j = Val("&H" + Mid(txt, nC, 2)) 'im HEX-Format

New User

Joined: 16 Jan 2008
Posts: 2

Jan 19, 2008 10:03 am


that's was the problem Smile

thank you very very much Greg

Jul 10, 2009 10:46 am


I have a problem with English Sad
I use this program as written above. However, I have a problem, the variable is entered for the message and that indicates the final CRC.

Lp PeX

Aug 25, 2012 10:04 pm

thanks. it helped me a lot !

Jan 01, 2015 1:32 pm

Great Thanks . It works for me

       Computer Interfacing Forum Index -> Error detection and correction
Page 1 of 1

Running on php BB © 2001, 2009 php BB Group
   Lammert Bies     Interfacing     Sitemap     Forum