Calculating Checksums
Calculating Checksums
Calculating Checksums
Calculating Checksums
Calculating Checksums
Calculating Checksums Calculating Checksums Calculating Checksums Calculating Checksums Calculating Checksums Calculating Checksums Calculating Checksums Calculating Checksums
Calculating Checksums Calculating Checksums
Calculating Checksums
Go Back  Xtreme Visual Basic Talk > > > Calculating Checksums


Reply
 
Thread Tools Display Modes
  #1  
Old 07-20-2006, 07:57 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default Calculating Checksums


Firstly I will appologise for my newby lack of knowlege and get that out of the road.

Now to the question.... I am programming in VB 2005 and I am trying to calulate a checksum. I have never done this before so I am feeling around in the dark somewhat.

I have read quite a fair bit of material on the different methods of calculating checksums, but none seem to work so hopefully someone can help.

The scenario... I am trying to write a program that sends instructions to my Haltech ECU in order to get data back. Which I then manipulate and send back to the ECU in the form of an update.

Front end to ECU
Function EDG1 EDG2 EDG3 Checksum
1 byte 1 byte 1 byte 1 byte 2 bytes

Function = $07 EDG1 = $01 EDG2 = $00 EDG3 = $00 Checksum = FFFC

No matter how I add up the components I cant seem to get the checksum to add up to FFFC please help...
Reply With Quote
  #2  
Old 07-20-2006, 07:59 PM
OnErr0r's Avatar
OnErr0rCalculating Checksums OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

Which checksum algorithm are you trying to implement?
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #3  
Old 07-20-2006, 08:05 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by OnErr0r
Which checksum algorithm are you trying to implement?
I have very little information regarding what algorithm they are using Haltech sent me a copy of their ECU Data Protocol but it contains limited information on how it all fits together. Below is a response from Haltech

You should be entering in an engine data request that looks like this,

07
byte1
byte2
byte3
CheckSum Hi
CheckSum Lo

Note these guy's are all unsigned chars. Give that a go. Also too remember the baudrate 57600.

I ran a com monitoring program that gave me a read out that contains examples of the checksum but I cant seem to work out what method or algorithm they used.

I think the main problem is I am just not adding them up properly. Also the Checksum Hi / Lo is throwing me.
Reply With Quote
  #4  
Old 07-20-2006, 08:38 PM
OnErr0r's Avatar
OnErr0rCalculating Checksums OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

It's most likely some CRC16 variant. Hi and Lo would mean the high and low bytes. For instance, in the checksum: &H8765& &H87 is the Hi and &H65 is the Lo. They're separated using Shifting and Bit masking.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #5  
Old 07-20-2006, 08:54 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by OnErr0r
It's most likely some CRC16 variant. Hi and Lo would mean the high and low bytes. For instance, in the checksum: &H8765& &H87 is the Hi and &H65 is the Lo. They're separated using Shifting and Bit masking.
I figured it was some thing like that. I am unsure what they are adding up to get the Hi & Lo.

I am guessing that $07 + byte1 = Hi and Byte2 + Byte3 = Lo

I haven't heard of Shifting and Bit Masking before so not sure how that factors in.

below are a few examples from the data I retreived from the ECU as you can see they dont seem to add up.

7 = 7
255 = FF
207 = CF
0 = 0
254 = FE
43 = 2B

7 = 7
13 = D
0 = 0
0 = 0
255 = FF
236 = EC

7 = 7
159 = 9F
79 = 4F
0 = 0
255 = FF
11 = B

7 = 7
1 = 1
0 = 0
0 = 0
255 = FF
248 = F8

7 = 7
77 = 4D
0 = 0
0 = 0
255 = FF
172 = AC
Reply With Quote
  #6  
Old 07-20-2006, 09:01 PM
OnErr0r's Avatar
OnErr0rCalculating Checksums OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

First work on finding the correct CRC16 algo. Search the forum or google. I know both have examples you can try.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #7  
Old 07-20-2006, 09:05 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by OnErr0r
First work on finding the correct CRC16 algo. Search the forum or google. I know both have examples you can try.
Pardon my ignorance but how do I actually determine which is the right CRC16 algorithm, Sorry I am new to the whole checksum / algorithm thing.
Reply With Quote
  #8  
Old 07-20-2006, 09:32 PM
OnErr0r's Avatar
OnErr0rCalculating Checksums OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

Since you apparently don't know what is being used, either attempt to find out, or start trying various algos on the data.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #9  
Old 07-20-2006, 09:50 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by OnErr0r
Since you apparently don't know what is being used, either attempt to find out, or start trying various algos on the data.
I have downloaded a number of tools from the web in an attempt to work out what Checksum it uses but none of the tools I have used have resulted in the checksum listed above.

I have printed out that foolproof document on CRC16 checksum to try get a better understanding of how checksums work.

I also found this bit of code in a search on the forum, I think this might be what I am look for but I am unsure. As I say I am a bit of a newb and appologise for asking such stupid questions but until I understand the concept of what is happening I am finding it diffacult to know exactly what I am looking for.

I think this code below might be much more useful if the author had put some really obvious commenting in it for dummies like me.

Code:
Public Function crc16(ByVal txt As String, ByVal lon As Long) As Long Dim flag As Long Dim crc As Long Dim car As Integer Dim bit As Integer crc = &HFFFF& For car = 1 To lon crc = crc Xor Asc(Mid(txt, car, 1)) For bit = 0 To 7 flag = crc And 1& crc = crc \ 2& If flag = 1 Then crc = crc Xor &HA001& Next Next crc16 = Int(crc And &HFFFF&) End Function

Last edited by OnErr0r; 07-20-2006 at 10:34 PM. Reason: Added vb tags.
Reply With Quote
  #10  
Old 07-20-2006, 10:36 PM
OnErr0r's Avatar
OnErr0rCalculating Checksums OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

Pass it a string and the length of the string. Or, just alter it to take a string only and loop from 1 to len(txt).
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #11  
Old 07-20-2006, 11:10 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by OnErr0r
Pass it a string and the length of the string. Or, just alter it to take a string only and loop from 1 to len(txt).
Public Function crc16(ByVal txt As String, ByVal lon As Long) As Long
Dim flag As Long
Dim crc As Long
Dim car As Integer
Dim bit As Integer

crc = &HFFFF&

For car = 1 To len(txt) 'txt = 07010000
crc = crc Xor Asc(Mid(txt, car, 1))
For bit = 0 To 7
flag = crc And 1&
crc = crc \ 2&
If flag = 1 Then crc = crc Xor &HA001&
Next
Next

crc16 = Int(crc And &HFFFF&)
'Hopefully FFF8 = Int(crc And &HFFFF&)
End Function

Does that look remotely right? I have never used Xor or Asc before so I will need to use help to find out what their functions are. Not sure why they passed crc = &HFFFF& was this to set a value to compare the txt string against?

Maybe it is to format crc to take Hex???
Reply With Quote
  #12  
Old 07-20-2006, 11:43 PM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

Hi,

I assume you've tried the obvious and asked Haltec what algorithm they use to calculate the checksum.

I've found 2 CRC16 algorithms and neither gives FE2B for an input of 07FFCF00 (one - here crc16 CCITT calculation , (Post #5 with the changes suggested by passel) gives AECC, the other , from Planetsourcecode, gives 5A3E)
Edit by dougT: BTW - with the code you are using you are putting the Chr values of the hex rather than "07010000" into the string aren't you. eg
Code:
strTxt = Chr(&H7) & Chr(&H1) & Chr(&H0) & Chr(&H0)

Regards
Doug
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data

Last edited by DougT; 07-20-2006 at 11:55 PM.
Reply With Quote
  #13  
Old 07-21-2006, 12:31 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by DougT
Hi,

I assume you've tried the obvious and asked Haltec what algorithm they use to calculate the checksum.

I've found 2 CRC16 algorithms and neither gives FE2B for an input of 07FFCF00 (one - here crc16 CCITT calculation , (Post #5 with the changes suggested by passel) gives AECC, the other , from Planetsourcecode, gives 5A3E)
Edit by dougT: BTW - with the code you are using you are putting the Chr values of the hex rather than "07010000" into the string aren't you. eg
Code:
strTxt = Chr(&H7) & Chr(&H1) & Chr(&H0) & Chr(&H0)

Regards
Doug
Thanks Doug pays not to assume too much on this one it is my first time working with Hex and I am still trying to come to grips with how Hex is represented in Char, Dec, binary etc that being said I will take your subtle hint and enter it as you have it above.

I know the Chr tells VB it is a character and the &H tell VB that the following number is Hex but what is the difference between Dec 07 and Chr(&H7) when entering it as a string??

Yes I have contacted Haltech but they are fairly reluctant to give me anything beyond what I have. I have emailed several times with no response. I can understand that they don't have time for newbs.

I am going to work with that bit of code above over the weekend and see if I can make some sense of what it is doing.
Reply With Quote
  #14  
Old 07-21-2006, 12:54 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

ok I just ran this and it returned Hex 9050 I was expecting FFF8 any ideas what happened?

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim txt As String
Dim lon As Long

txt = Chr(&H7) & Chr(&H1) & Chr(&H0) & Chr(&H0)
lon = Len(txt)
TextBox1.Text = Hex(crc16(txt, lon))

End Sub

Public Function crc16(ByVal txt As String, ByVal lon As Long) As Long

Dim flag As Long
Dim crc As Long
Dim car As Integer
Dim bit As Integer

crc = &HFFFF&
For car = 1 To lon
crc = crc Xor Asc(Mid(txt, car, 1))
For bit = 0 To 7
flag = crc And 1&
crc = crc \ 2&
If flag = 1 Then crc = crc Xor &HA001&
Next
Next

crc16 = Int(crc And &HFFFF&)
End Function

End Class
Reply With Quote
  #15  
Old 07-21-2006, 01:40 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

Hi,

You may get some success by asking on their forum: http://p210.ezboard.com/bhaltechdiscussionforum
but from the looks of the traffic there, the topics are more about using the devices 'as is'. However you may get lucky if you describe exactly what you're attempting to do.

Regards
Doug
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #16  
Old 07-21-2006, 01:52 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

Hi,

I suspect that Haltec don't use that particular algorithm for calculating the checksum.

Regards
Doug
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #17  
Old 07-21-2006, 02:08 AM
namrekka namrekka is offline
Junior Contributor
 
Join Date: Feb 2005
Location: Netherlands
Posts: 219
Default

There is not a "standard" CRC check. This line:
Code:
If flag = 1 Then crc = crc Xor &HA001&
crc xors a "polynomial". A lot of polynomials can be used with different results.
Then you use a starting value for crc:
Code:
crc = &HFFFF&
Sometimes it starts with "0".
You need more info on this from your supplier, otherwise to many possibilities are left.
Reply With Quote
  #18  
Old 07-21-2006, 02:20 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by DougT
Hi,

I suspect that Haltec don't use that particular algorithm for calculating the checksum.

Regards
Doug
I have posted on their forum but I don't expect a great deal of help based on past experience. I have had a fantastic response here so far so hopefully someone passing by might have the key to getting that elusive FFF8
Reply With Quote
  #19  
Old 07-21-2006, 02:27 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by namrekka
There is not a "standard" CRC check. This line:
Code:
If flag = 1 Then crc = crc Xor &HA001&
crc xors a "polynomial". A lot of polynomials can be used with different results.
Then you use a starting value for crc:
Code:
crc = &HFFFF&
Sometimes it starts with "0".
You need more info on this from your supplier, otherwise to many possibilities are left.
Below is from the document they sent me...

"3 E11 Data Logging Protocol

As per the E6X the E11 communicates engine data to the master through the Serial port.

The front-end sends the request to the ECU and instructs the ECU to send back certain groups of data depending on the “Engine Data Group” byte that is sent. The ECU replies with the “Engine Data Group” byte, followed by the engine data in order of groups from the first group that is requested.

Processing of the request is done by a bit test of the “Engine Data Group” field. The LSB is processed first and data corresponding to this group is put into a buffer ready to be sent if this bit is set. The next bit is then tested, and if set, the group data corresponding this is bit is appended to this buffer. A checksum is appended after the last bit in the “Engine Data Group” byte has been processed. After the checksum, the buffer is then sent to the front-end software. All data the engine data packet is"

Unfortunately thats where it ends. Looks like I need to find out what a "polynomial" is as well.

If I know the string being passed 07 01 00 00 and the checksum is FF F8 is there some way of checking all of the variables that might return FF F8?
Reply With Quote
  #20  
Old 07-21-2006, 02:33 AM
namrekka namrekka is offline
Junior Contributor
 
Join Date: Feb 2005
Location: Netherlands
Posts: 219
Default

Look at:
http://en.wikipedia.org/wiki/Cyclic_redundancy_check

Or try Poly: &H1021
And crc=0 and crc=&HFFFF
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Calculating Checksums
Calculating Checksums
Calculating Checksums Calculating Checksums
Calculating Checksums
Calculating Checksums
Calculating Checksums Calculating Checksums Calculating Checksums Calculating Checksums Calculating Checksums Calculating Checksums Calculating Checksums
Calculating Checksums
Calculating Checksums
 
Calculating Checksums
Calculating Checksums
 
-->