[SOLVED] CRC16 - Visual Basic

General discussion of using Roboclaw motor controllers
Post Reply
GeeklyGrey
Posts: 42
Joined: Thu Oct 08, 2015 11:41 am
[SOLVED] CRC16 - Visual Basic

Post by GeeklyGrey »

Great to see some of that stuff available now.
I code my own routines in Visual Basic - available libraries not useful to me I think. (advice?)
Would appreciate two things.

Thing 1
PLEASE provide a line of data that shows exactly what is being sent for a typical command.
I suggest a format something like "80 26 00 00 07 D0 00 00 00 2C xx yy" where xx and yy are replaced by the ACTUAL CRC16 checksum values you would get - This example is for port 128 command 38 Accel 2000 as 4bytes and some speed as 4bytes. I believe the CRC16 is now going to be 2 bytes.
I can plug your values into an array and when I can get my code to produce your answer I will know I am getting somewhere. I don't have C to test for answers using the sample.

Thing 2
Please supply a pseudo English code explanation for the CRC example you provided in C on page 36 of the new manual.
It looks like a simple enough thing to code if I could get my head around exactly what it is achieving. My C is nothing like good enough to read that example. I have found some other coded examples I am trying to analyse to find/build a matchup.
Last edited by GeeklyGrey on Sun Oct 18, 2015 12:37 am, edited 1 time in total.
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Major Update Available

Post by Basicmicro Support »

I moved your question to a seperate topic. I forgot to lock the announcement from replies.

Thing 1. The CRC16 is the standard CRC16 using polynomial 0x1021. You can calculate the CRC16 of any string of bytes using this online calculator.

http://depa.usst.edu.cn/chenjq/www2/sof ... lation.htm

Press the CCITT_16 button to set the polynum and order. Then select hex for data input and type in the hex values as a single number(8026000007D00000002C) and click the checksum button, This calculates the checksum as 30D so the two checksum bytes are 0x93 0x0D, high byte then low byte.

Thing 2.
What language are you using? I'll see if I can write something close to that for you. A psuedo english description of CRC16 calculations would be as complex or more complex than the C code which is why I wrote the example in C.
GeeklyGrey
Posts: 42
Joined: Thu Oct 08, 2015 11:41 am
New CRC16 -calculations

Post by GeeklyGrey »

Continued from replies to Major Software Release
This thread relates to the CRC16 code example on page 36 of the new manual.

Thanks for the link on that reply - that will be very useful. Quoted here for continuity.
http://depa.usst.edu.cn/chenjq/www2/sof ... ation.htm
Select CCITT and Hex then insert hex values as one continuous string

The answer you quoted as 30D - should that be hi byte 0x03 and not 0x93 I think
Great to have use of a tool that matches what RoboClaw expects. There are so many versions out there,

I am working with Visual Basic 5.

Have been attempting to build my own pseudo English from your sample and a lot of digging into C syntax. Cannot say I am there yet but have learned quite a bit today along the lines of

Assume crc starts as &H0 (- some of these CRC16 start with &HFF it seems)
For byte = 0 to quantity of bytes in the packet - 1

? cannot figure the line that reads
crc = crc ^ ((unsigned int)packet[byte] << 8);
looks like crc = crc xor'ed with selected byte in packet shifted 8 bits to the left? not sure what is happening there. is there anything useful left?

I think that << shift left 1 bit achieves a similar result to multiply by 2 (dec or hex)at the bit level.
<<8 has to be shift a whole byte to left. So it seems like parsing every byte is happening.

And crc & 0x8000 seems to be flagging that if the leftmost bit out of 16 (2 bytes)is a 1 then
shift crc one bit left (multiply by 2) then
xor with 0x1021 which I understand is the CCITT polynom.
else
shift crc one bit left (multiply by 2)
and go around again

Constructive comment welcome!

BTW did not see your moved post CNC16 - do you want to move this thread onto that one?
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: New CRC16 -calculations

Post by Basicmicro Support »

1. Yes you are correct. I hit 9 instead of 0, 0x03(high byte) and 0x0D(low byte)

2. In this

For byte = 0 to quantity of bytes in the packet - 1

bytes would be the packet bytes not including the CRC16. Then compare the CRC16 calculated to the CRC16 bytes received. You can process the CRC16 bytes received which if valid will cause your CRC16 calculation to come out to 0 but that is more processor intensive than just stopping before the received CRC16 bytes and comparing them.

3. crc = crc ^ ((unsigned int)packet[byte] << 8);

Yes you are correct. This is taking some byte received(say 0x5A) and then shifting it left 8 bits, like so, 0x5A00, and then xors the new 16bit value(0x5A00) with the previous 16bit crc to get the new updated crc.
GeeklyGrey
Posts: 42
Joined: Thu Oct 08, 2015 11:41 am
Re: CRC16

Post by GeeklyGrey »

Thanks for that info - I will work through it with interest.
I have found a piece of code that can duplicate the same values as the CRC calculation tool you referred me to. This after building three small systems from web page examples and testing them. The code uses a look up table and I was sceptical about that but it worked right out of the box. Others were totally different results
My hope is that the calc tool is one that you used maybe in development and we are all on the same page now.
I have checked both against some standard tests and some commands previously posted etc.
The code is included below more or less in original form -some notes added.
I have included various credits if that is appropriate. Adjust if necessary.

Just need IONmotion new version to work now. refer other thread

Code: Select all

http://www.tek-tips.com/viewthread.cfm?qid=1587721
' THIS CODE WORKS CORRECTLY COMPARED WITH PROFESSOR CHEN JIAGI- CRC CALCULATOR TOOL
' RECOMMENDED BY 'acidtech' IONMOTION FORUM.

SJA (TechnicalUser) (OP) 22 Jan 10 07:10 
Here is the code to calculate the CRC16-CCITT checksum
which uses the x^16,x^12,x^5,x^1 polynomial.
It uses a lookup table for speed.
DataByte() is the byte array which the checksum is calculated for.
In the example the array is loaded from a hex string in Text1.Text.
The initial value 0 is loaded from Text2.Text.
The CRC is displayed in Text3.Text.
The lookup table is pasted in at the end of the code.
It should be copied into a text file called CRCTAB.txt
and saved in the same folder as the Project.

Option Explicit
Dim CRCTab(255) As Long '               Array for single byte CRCs loaded from table
Dim DataByte() As Byte '                Array for byte data

Private Sub CmdStart_Click()
Dim TempStr As String
Dim DataSize As Long
Dim X As Long
Dim Index As Long
Dim TempLng As Integer
Dim CRC As Long

TempStr = Text1.Text '                              Load text box hex bytes
DataSize = (Len(TempStr) / 2) - 1 '                 Calc nunber of pairs of chrs
ReDim DataByte(DataSize) '                          Resize the array
Index = 1
For X = 0 To DataSize '                             Load array into memory
    DataByte(X) = "&h" & Mid(TempStr, Index, 2)
    Index = Index + 2
Next

CRC = CLng("&h" & Text2.Text) '  Load initial value use 0     (normally 0xFFFF)

For X = 0 To DataSize '                             Loop through data bytes
    TempLng = ((CRC \ 256) Xor DataByte(X)) '       Shift left (>>8) XOR with data
    CRC = ((CRC * 256) And 65535) Xor CRCTab(TempLng) ' Shift right (<<8) prevent overflow, XOR with table
Next
Text3.Text = Right("0000" & Hex(CRC), 4)

End Sub

'Load single byte CRC table
Private Sub Form_Load()
Dim X As Long
Dim TempStr As String

Open App.Path & "\CRCTAB.txt" For Input As #1
    For X = 0 To 255
        Input #1, TempStr
        CRCTab(X) = CLng(TempStr)
    Next
Close #1
End Sub
'cut and paste below info into notepad, check for a clean copy, save as CRCTAB.txt in project dir.

&h0000, &h1021, &h2042, &h3063, &h4084, &h50A5, &h60C6, &h70E7, &h8108, &h9129, &hA14A, &hB16B, &hC18C, &hD1AD, &hE1CE, &hF1EF, &h1231, &h0210, &h3273, &h2252, &h52B5, &h4294, &h72F7, &h62D6, &h9339, &h8318, &hB37B, &hA35A, &hD3BD, &hC39C, &hF3FF, &hE3DE, &h2462, &h3443, &h0420, &h1401, &h64E6, &h74C7, &h44A4, &h5485, &hA56A, &hB54B, &h8528, &h9509, &hE5EE, &hF5CF, &hC5AC, &hD58D, &h3653, &h2672, &h1611, &h0630, &h76D7, &h66F6, &h5695, &h46B4, &hB75B, &hA77A, &h9719, &h8738, &hF7DF, &hE7FE, &hD79D, &hC7BC, &h48C4, &h58E5, &h6886, &h78A7, &h0840, &h1861, &h2802, &h3823, &hC9CC, &hD9ED, &hE98E, &hF9AF, &h8948, &h9969, &hA90A, &hB92B, &h5AF5, &h4AD4, &h7AB7, &h6A96, &h1A71, &h0A50, &h3A33, &h2A12, &hDBFD, &hCBDC, &hFBBF, &hEB9E, &h9B79, &h8B58, &hBB3B, &hAB1A, &h6CA6, &h7C87, &h4CE4, &h5CC5, &h2C22, &h3C03, &h0C60, &h1C41, &hEDAE, &hFD8F, &hCDEC, &hDDCD, &hAD2A, &hBD0B, &h8D68, &h9D49, &h7E97, &h6EB6, &h5ED5, &h4EF4, &h3E13, &h2E32, &h1E51, &h0E70, &hFF9F, &hEFBE, &hDFDD, &hCFFC, &hBF1B, &hAF3A, &h9F59, &h8F78, &h9188, &h81A9, &hB1CA, &hA1EB, &hD10C, &hC12D, &hF14E, &hE16F, &h1080, &h00A1, &h30C2, &h20E3, &h5004, &h4025, &h7046, &h6067, &h83B9, &h9398, &hA3FB, &hB3DA, &hC33D, &hD31C, &hE37F, &hF35E, &h02B1, &h1290, &h22F3, &h32D2, &h4235, &h5214, &h6277, &h7256, &hB5EA, &hA5CB, &h95A8, &h8589, &hF56E, &hE54F, &hD52C, &hC50D, &h34E2, &h24C3, &h14A0, &h0481, &h7466, &h6447, &h5424, &h4405, &hA7DB, &hB7FA, &h8799, &h97B8, &hE75F, &hF77E, &hC71D, &hD73C, &h26D3, &h36F2, &h0691, &h16B0, &h6657, &h7676, &h4615, &h5634, &hD94C, &hC96D, &hF90E, &hE92F, &h99C8, &h89E9, &hB98A, &hA9AB, &h5844, &h4865, &h7806, &h6827, &h18C0, &h08E1, &h3882, &h28A3, &hCB7D, &hDB5C, &hEB3F, &hFB1E, &h8BF9, &h9BD8, &hABBB, &hBB9A, &h4A75, &h5A54, &h6A37, &h7A16, &h0AF1, &h1AD0, &h2AB3, &h3A92, &hFD2E, &hED0F, &hDD6C, &hCD4D, &hBDAA, &hAD8B, &h9DE8, &h8DC9, &h7C26, &h6C07, &h5C64, &h4C45, &h3CA2, &h2C83, &h1CE0, &h0CC1, &hEF1F, &hFF3E, &hCF5D, &hDF7C, &hAF9B, &hBFBA, &h8FD9, &h9FF8, &h6E17, &h7E36, &h4E55, &h5E74, &h2E93, &h3EB2, &h0ED1, &h1EF0
  
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: CRC16

Post by Basicmicro Support »

Yes, I used that online calculator to test against my own code originally. Also I have used a table lookup version before. I don't use it in the arduino library because it takes a lot of flash memory(although it is faster).

You can tell this table is correct by looking at the first few bytes("&h0000, &h1021, &h2042,") You can see it is a 0x1021 polynomial based table.

There are a lot of different formats of CRC16. They can be revered, then can start with an initial value of FFFF instead of 0000 and they can have any number of polynomials.
GeeklyGrey
Posts: 42
Joined: Thu Oct 08, 2015 11:41 am
Re: CRC16- Visual Basic - SOLVED

Post by GeeklyGrey »

UPDATE - The issues I had been having with my RoboClaw seem to have gone away and the CRC16 software mentioned above is now online and working well so far. Run the software with starting number of '0' as coded. That code wants a command button and three text boxes but you folks will figure it out.

Think we can call this thread SOLVED as far as Visual Basic is concerned.
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: CRC16 - Visual Basic - SOLVED

Post by Basicmicro Support »

Thanks for the VB code. Im planning on writing an example using VB.NET and the roboclaw.dll sometime soon. Just dont know when yet.
GeeklyGrey
Posts: 42
Joined: Thu Oct 08, 2015 11:41 am
Re: CRC16 - Visual Basic - SOLVED

Post by GeeklyGrey »

Thank you Dawn. Had a lot of help from forum and I like to give back when I can.

Post Reply