Author Topic: Function to Compute checksum of a hex string  (Read 2511 times)

bachphi

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Function to Compute checksum of a hex string
« on: July 17, 2016, 04:21:04 PM »
It is a 2's complement 8-bit checksum. The string only contains [0-9,A-F], and is an even number of characters. Checksum is calculated by adding the byte values in the hex string,  negating, then masking the lowest 8-bits, finally add 1 to it. Checksum can then be added to the cmdString later. Let me know if you have a better way. Thanks.

Code: [Select]
' CmdString = "B6FE8019AA"
Private Function ComputeCheckSum(ByVal HexString As String) As String

        Dim ReturnValue As String 'Return value is 2-dig hex value representing the checksum
        Dim intchecksum As Integer = 0

        For i As Integer = 0 To HexString.Length - 1 Step 2
            Dim value As Integer = Integer.Parse(HexString.Substring(i, 2), NumberStyles.AllowHexSpecifier)
            intchecksum = (intchecksum + value) And &HFF
        Next
        ReturnValue = Hex(256 - intchecksum)                                      'instead of negating and add 1
        If Len(ReturnValue) = 1 Then ReturnValue = "0" & ReturnValue    'Make sure each converted byte value is 2 digits

        ComputeCheckSum = ReturnValue        '09

    End Function

To check if the message  was transmitted correctly: B6+FE+80+19+AA+09 = 300 
 (300 AND &0xFF) should equal 0
 
    A 'simple' 8-bit checksum per Markem-Imaje is actually a one complement checksum. The above routine can still be used, just change 256 to 255   

« Last Edit: February 11, 2020, 09:52:53 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Function to Compute checksum of a hex string
« Reply #1 on: February 10, 2020, 08:56:43 PM »
Archie, I am looking for 16-bit CRC checksum codes. It looks like it was in MfgControl.AdvancedHMI.Drivers.Checksum.CalculateCRC16 at one point in time. Thanks in  advance.
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Godra

  • Hero Member
  • *****
  • Posts: 1438
    • View Profile
Re: Function to Compute checksum of a hex string
« Reply #2 on: February 10, 2020, 09:56:10 PM »
The function MfgControl.AdvancedHMI.Drivers.Checksum.CalculateCRC16 is still there.

bachphi

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Function to Compute checksum of a hex string
« Reply #3 on: February 11, 2020, 11:49:18 AM »
You're right.
I was hoping to take a peek in the codes.
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Godra

  • Hero Member
  • *****
  • Posts: 1438
    • View Profile
Re: Function to Compute checksum of a hex string
« Reply #4 on: February 11, 2020, 02:08:18 PM »
Google is probably a good choice but you can take a peak at C# code of the nModbus library:

https://code.google.com/p/nmodbus/

Download the source code and look for the file ModbusUtility.cs, which should be inside either "src/Modbus/Utility" or "nmodbus/branches/VSNET_Updates/NModbus/src/Modbus/Utility" folder.
Maybe try converting it to VB.
« Last Edit: February 11, 2020, 02:19:36 PM by Godra »

bachphi

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Function to Compute checksum of a hex string
« Reply #5 on: February 11, 2020, 09:37:59 PM »
Modbus it is: 
Initialize the checksum as FFFFhex.
For each byte
   Checksum = Checksum XOR (current byte)
   For I = 0 to 7
       If ( ( Checksum AND 1 ) = 0 )
            Checksum = Right_Bit_Shift Checksum 1 bit
       Else
            Checksum = (Right_Bit_Shift Checksum 1 bit) XOR A001 hex
   Next I
Next Byte

Code: [Select]
'CRC-16/MODBUS
   Function CRC16(Dataa As String)
        Dim chksum As Integer = 65535
        For i = 1 To Dataa.Length
            chksum = ByteCRC(chksum, Asc(Mid(Dataa, i, 1)))
        Next i
        CRC16 = Hex(chksum)
    End Function

    Function ByteCRC(sum As Integer, dat As Integer) As Integer
        sum = sum Xor dat
        For i = 0 To 7
            If ((sum And 1) = 0) Then
                sum = Int(sum / 2)              ' /2 = Right Shift
            Else
                sum = Int(sum / 2) Xor 40961    ' A001 Hex
            End If
        Next i
        ByteCRC = sum
    End Function
« Last Edit: February 11, 2020, 10:10:33 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Function to Compute checksum of a hex string
« Reply #6 on: February 14, 2020, 04:34:55 PM »
Attached is AOI for 8-bit checksum for either 1s or 2s complement.
« Last Edit: February 15, 2020, 08:10:53 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Function to Compute checksum of a hex string
« Reply #7 on: February 15, 2020, 08:09:29 PM »
Attached is an AOI for 16-bit CRC checksum Modbus
Code: [Select]
CRC16 := 16#FFFF ; // 65535
FOR Indx1 := 0 TO Str_HexData.LEN-1 do // For each byte
CRC16 := (CRC16 XOR Str_HexData.DATA[Indx1]); //Checksum = Checksum XOR (current byte)
FOR Indx2 := 0 TO 7 do
If (CRC16 AND 1) = 0 then
CRC16 := (CRC16/2);
ELSE
CRC16 := ((CRC16/2) XOR 16#A001); //Checksum = (Right_Bit_Shift Checksum 1 bit) XOR A001hex 40961
END_if;
end_for;
end_for;

===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================