News:

  • July 04, 2026, 04:30:25 AM

Login with username, password and session length

Author Topic: Help with CRC  (Read 7517 times)

MarkTTU

  • Hero Member
  • *****
  • Posts: 293
    • SamJackson.com
Help with CRC
« on: September 11, 2015, 08:30:19 PM »
I'm reading in a string via serial. Have managed to get all the data in and split out into D memory like I need it, but I'm having a hard time trying to do a check on the CRC I'm getting with the string.

String is 18 bytes.
Byte 1 = Not Used / Don't care
Bytes 2-17 = Data I care about and am successfully pulling out of the string
Byte 17 = CRC = inverse of the sum of bytes 2 to 17 so that the 8 bit sum of bytes 2 to 17 + CRC = 0

I've managed to use the STRGETB command to pull out byte 17 and put it into D0. Problem I have is I'm not sure how to calculate the 8 bit sum of bytes 2-17. Once I've got that I figure I can use a MATH instruction to add the two together and see if I get 0 or not. Any ideas?

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6164
  • Yes Pinky, Do-more will control the world!
Re: Help with CRC
« Reply #1 on: September 11, 2015, 10:54:43 PM »
Create a byte block. Use STRGETB to move data from the string to the byte block. Use MATH's SUM function the sum the array in one instruction.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3833
    • Host Engineering
Re: Help with CRC
« Reply #2 on: September 12, 2015, 09:12:56 AM »
CHECKSUM should work on the existing D memory buffer.  Based on your description, it looks like the checksum is in Byte #18, not byte #17, which is data?  I have written the logic below assuming byte #17 is data and byte #18 is the checksum byte.

Note that Do-more uses 0-based offsets, not 1-based offsets.  Hence Byte #2 is Offset 1, Byte #18 is Offset 17 (this is probably the source of 17 vs. 18, since you stated that you had extracted checksum at the 18th byte, at offset 17  ;D).

CHECKSUM
On Buffer: D0
Function: CheckSum:8
Initial Value: 0
Starting Offset: 1  // 0-based offset
Ending Offset 17  // 0-based offset, INCLUDES the Checksum byte; make it 16 to NOT include the checksum byte
Destination V0

V0 should equal 0 when you include the checksum byte



BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6164
  • Yes Pinky, Do-more will control the world!
Re: Help with CRC
« Reply #3 on: September 12, 2015, 09:29:50 AM »
Was reading this on my phone...without glasses...and missed stuff. You can use the MATH SUM, and then either output to a byte, or use a cast to pick the low byte from the non-byte target.

As for CHECKSUM, I assumed you didn't use it because it didn't do what you needed...but it will definitely add bytes. So yeah, what Franj said.

"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO