Author Topic: CRC problems  (Read 12306 times)

mullacott

  • Newbie
  • Posts: 5
  • I'm a llama!
    • View Profile
CRC problems
« on: December 17, 2011, 03:12:03 PM »
 I am writing on a nano a communications package that will talk to a Heatmister RS485 room stat. I have the communications working fine except I am having trouble with the CRC CCITT (0xFFF) checksum. I have to use an online calculator to find the CRC and enter it by hand. What I need is to be able to calculate in the code the CRC. The built in CRC16 in TBASIC give the wrong answer. Has someone done this and can help.  Example of data frame is ? 03 0A 81 00 00 00 FF FF? and then I add a CRC of ?8A 86? low byte then high byte.

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:CRC problems
« Reply #1 on: December 18, 2011, 09:53:48 PM »
Your in luck.  I was bored and wrote you a brute force CRC16-CCITT algorithm in TBASIC.  

You can copy and and paste this algorithm into your test code.

Here is the guts of the algorithm, "CRC16_CCITT":

// CRC16_CCITT - custom function to compute CRC16-CCITT CRC
//
// This is a brute force bit at a time algorithm.
//
// On input: Byte8 holds the 8 bit data to be accumulated into the CRC
//           CRC is the 16 bit CRC
//
// On exit: CRC is updated
//          Byte8 is all cleared


Byte8 = Byte8 * 256??????// shift incoming data 8 bits to the left
??????????????????// this aligns the most significant bit of the
??????????????????// incoming data with the 16-bit CRC value to
??????????????????// slightly simplify the algrorithm.

FOR i = 1 to 8
???j = Byte8 ^ CRC & &H8000???// check to see if feedback multiplier will be needed
???LSHIFT CRC,1????????????

???if (j)
??????CRC = CRC ^ &H1021???// feedback multiplier for CRC16-CCIT
???ENDIF
???LSHIFT Byte8,1
NEXT


Here is the test harness:


// TestString - test CRC16-CCIT algorithm
//

CRC = &Hffff???// Initialze CRC accumulator to all ones

Byte8 = &h03???: CALL CRC16_CCITT
Byte8 = &h0a ???: CALL CRC16_CCITT
Byte8 = &h81 ???: CALL CRC16_CCITT
Byte8 = &h00 ???: CALL CRC16_CCITT
Byte8 = &h00 ???: CALL CRC16_CCITT
Byte8 = &h00 ???: CALL CRC16_CCITT
Byte8 = &hff ???: CALL CRC16_CCITT
Byte8 = &hff ???: CALL CRC16_CCITT

// 03 0A 81 00 00 00 FF FF  Expected CRC 86 8A


A couple of notes: the variables CRC and Byte8 are actually DM[1] and DM[2] respectively.  I am using the #Define feature that is available in iTRiLOGI 6.43 to create code that is marginally readable.

Good luck,

Gary D
« Last Edit: December 18, 2011, 10:04:45 PM by garysdickinson »

mullacott

  • Newbie
  • Posts: 5
  • I'm a llama!
    • View Profile
Re:CRC problems
« Reply #2 on: December 19, 2011, 05:40:01 AM »
Thanks. I have been playing with this for some time with not much joy. It is great to see how it should be done. Your code works and is now monitoring our heating in the house. This means when I am away I can see what is happening and make changes. Thanks again