This one's for Lewis

| | Comments (2)

You asked for a 16-bit CRC algorithm that works with the Crystalfontz CFA633-YYB-KS, and so I found one. Well, I didn't really find one. They had a C version in their docs that I ported into RB. Btw, they also have that nasty table-driven one that you found in your VB code. Yech! The reason we couldn't get the algorithm I had working is because the device requires a non-standard version of the algorithm. Essentially, the checksums all seem to "start" from a different point. So it takes a bit of extra munging.

This code should work for you, but I have to admit that I haven't tested it. I left my Keyspan at home, and my laptop has no serial ports. :-P
[rbcode]Function CRC(buffer as MemoryBlock, len as UInt16) As UInt16
dim newCRC as UInt32
dim data as UInt8
dim bit_count as Integer

newCRC = &h00F32100

dim pos as Integer
while len > 0
data = buffer.UInt8Value( pos )
pos = pos + 1

for bit_count = 0 to 7
newCRC = Bitwise.ShiftRight( newCRC, 1 )

if Bitwise.BitAnd( data, &h1 ) <> 0 then newCRC = Bitwise.BitOr( newCRC, &h00800000 )

if Bitwise.BitAnd( newCRC, &h80 ) <> 0 then newCRC = Bitwise.BitXor( newCRC, &h00840800 )

data = Bitwise.ShiftRight( data, 1 )
next bit_count

len = len - 1
wend

for bit_count = 0 to 15
newCRC = Bitwise.ShiftRight( newCRC, 1 )

if Bitwise.BitAnd( newCRC, &h80 ) <> 0 then newCRC = Bitwise.BitXor( newCRC, &h00840800 )
next bit_count

return Bitwise.ShiftRight( Bitwise.OnesComplement( newCRC ), 8 )
End Function[/rbcode]
If the code doesn't work, just email me (or post here) and I'll work on it some more. But I figure that once I get back to the office where I have a Keyspan, I'll play around with it to test it out as well. Thanks for the loaner!

2 Comments

There, I fixed it up now that I've had the chance to test it. I seem to be getting the same CRC as the test app on the 633's site. I'm excited to start playing with this! :-D

Works great!

Thanks!!!

I was just getting around to sending you the docs for the 633 today but you beat me to the punch. Crystalfontz has a very complete site and decent documentation. Glad you found it.

I sat in the bathtub yesterday with the code you so nicely adapted from the 633 docs and tried to figure it out. I don't think I will ever understand C. I really should keep a closer eye on your blog! It was still a good soak.

On the other algorithm from New RB2006r1 Features.pdf I don't understand how the checksum variable is updated with the results of the for j loop. Am I missing something?

Leave a comment

Disclaimer

I'm currently an employee of REAL Software. My blog is mine. The opinions represented in this blog are mine as well and may not represent my employer's opinions. All original material is copyrighted and property of the author.

REALbasic® is a registered trademark of REAL Software, Inc. REAL SQL Server™ and Lingua™ are pending trademarks of REAL Software, Inc. All rights reserved.