Shop OBEX P1 Docs P2 Docs Learn Events
CRC and Basic Language — Parallax Forums

CRC and Basic Language

ArchiverArchiver Posts: 46,084
edited 2001-10-26 18:30 in General Discussion
Function CRC(message$) as long


Dear all;

I have found a basic program that calculates crc checksum.

I would like to use this program on a BS2.

I would be grateful for any tips, or a converted program listing.

many thanks

A.J.Neal

'' CRC runs cyclic Redundancy Check Algorithm on input message$
'' Returns value of 16 bit CRC after completion and
'' always adds 2 crc bytes to message
'' returns 0 if incoming message has correct CRC
'' Must use double word for CRC and decimal constants

crc16& = 65535
FOR c% = 1 to LEN(message$)
crc16& = crc16& XOR ASC(MID$(message$, c%, 1))
FOR bit% = 1 to 8
IF crc16& MOD 2 THEN
crc16& = (crc16& \ 2) XOR 40961
ELSE
crc16& = crc16& \ 2
END IF
NEXT BIT%
NEXT c%
crch% = CRC16& \ 256: crcl% = CRC16& MOD 256
message$ = message$ + CHR$(crcl%) + CHR$(crch%)
CRC = CRC16&
END FUNCTION CRC

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-10-26 14:14
    There are several problems here. The biggest one is that usually you compute
    the CRC over a large number of bytes (message$ in this algorithm). Where
    would those come from in the Stamp? You don't have 256 bytes of storage (or
    whatever).

    The algorithm specifies double words for several variables, but that's not
    insurmountable. I'm not sure it really needs it anyway. (Regular basic
    doesn't like unsigned so you need a bigger word.) If you are getting your
    data from a stream, you'd "unwrap" the for loop. So this is an attempt at a
    rewrite.

    crc16 var word ' running crc
    crc_char var byte ' incoming character
    crcbit var byte ' bit counter


    crc_init:
    crc16=$FFFF
    return

    crc_next:
    crc16=crc16 ^ crc_char
    for crcbit=1 to 8
    crc16=crc16>>1
    if crc16.bit0 = 0 then crcskip
    crc16=crc16 ^ 40961
    next
    return

    So to use this, you'd call crc_init. Then as each character arrives, you'd
    put it in crc_char and call crc_next. At the end of the data stream, your
    CRC is in crc16.

    A few notes:
    1) This is my interpretation of the code you sent. I didn't bother to verify
    that it really computers a CRC.

    2) I didn't test the code.

    3) ^ = XOR, >>1 is the same as \2, crc16.bit0 will be 0 if MOD 2 would be 0.
    You can use // to get MOD on the Stamp, but MOD 2 is a special case where
    you are really looking at the last bit so I rewrote it to be faster.

    4) Your original code has this in it:
    > crch% = CRC16& \ 256: crcl% = CRC16& MOD 256
    > message$ = message$ + CHR$(crcl%) + CHR$(crch%)
    > CRC = CRC16&

    This splits the CRC into bytes (crch = crc16.highbyte, crcl = crc16.lowbyte)
    and then adds it to the message (you'd have to do something else here). The
    last line causes a return from the function which the BS doesn't need.

    Let me know how that works for you.

    Al Williams
    AWC
    * Floating point math for the Stamp, PIC, SX, or any microcontroller
    http://www.al-williams.com/awce/pak1.htm

    >
    Original Message
    > From: a.neal@u... [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=dUfqAIxJSaEPomr9TscIOpy7xHBcFoDe4Jrbfo9cN6zls90HFm1FTTk4UghMHbO448HMhWfpUFxnrqnnsBvD]a.neal@u...[/url
    > Sent: Friday, October 26, 2001 3:54 AM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] CRC and Basic Language
    >
    >
    >
    >
    >
    > Function CRC(message$) as long
    >
    >
    > Dear all;
    >
    > I have found a basic program that calculates crc checksum.
    >
    > I would like to use this program on a BS2.
    >
    > I would be grateful for any tips, or a converted program listing.
    >
    > many thanks
    >
    > A.J.Neal
    >
    > '' CRC runs cyclic Redundancy Check Algorithm on input message$
    > '' Returns value of 16 bit CRC after completion and
    > '' always adds 2 crc bytes to message
    > '' returns 0 if incoming message has correct CRC
    > '' Must use double word for CRC and decimal constants
    >
    > crc16& = 65535
    > FOR c% = 1 to LEN(message$)
    > crc16& = crc16& XOR ASC(MID$(message$, c%, 1))
    > FOR bit% = 1 to 8
    > IF crc16& MOD 2 THEN
    > crc16& = (crc16& \ 2) XOR 40961
    > ELSE
    > crc16& = crc16& \ 2
    > END IF
    > NEXT BIT%
    > NEXT c%
    > crch% = CRC16& \ 256: crcl% = CRC16& MOD 256
    > message$ = message$ + CHR$(crcl%) + CHR$(crch%)
    > CRC = CRC16&
    > END FUNCTION CRC
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the
    > Subject and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
  • ArchiverArchiver Posts: 46,084
    edited 2001-10-26 18:30
    Hi all!!, can u explain more what is crc (I only know that's used to
    validate if the data in a transmisi
Sign In or Register to comment.