NiVZ Posted August 27, 2009 Posted August 27, 2009 (edited) Hello, I'm trying to write a program to read my Blood Glucose Meter but am stuck with the following piece of C code which is doing some BitShifting, BitXOR, and BitAND. Can anyone help me convert the C funciton to AutoIT unsigned short crc_calculate_crc (unsigned short initial_crc, const unsigned char *buffer, unsigned short length) { unsigned short index = 0; unsigned short crc = initial_crc; if (buffer != NULL) { for (index = 0; index < length; index++) { crc = (unsigned short)((unsigned char)(crc >> 8) | (unsigned short)(crc << 8)); crc ^= buffer [index]; crc ^= (unsigned char)(crc & 0xff) >> 4; crc ^= (unsigned short)((unsigned short)(crc << 8) << 4); crc ^= (unsigned short)((unsigned short)((crc & 0xff) << 4) << 1); } } return (crc); } Using the following array: unsigned char test_crc[4] = {02,06,06,03}; and the function call: unsigned short crc = crc_calculate_crc( 0xffff, test_crc, 4 ); you should get CRC of 0x41CD This is what I've got so far: $Test = "02,06,06,03" $arrTest = StringSplit($Test, ",") MsgBox(0,"", crc_calculate_crc("0xffff", $arrTest, 4)) Func crc_calculate_crc($initial_crc, $buffer, $length) $crc = $initial_crc If IsArray($Buffer) Then For $i = 1 to $Length ;ConsoleWrite($arrTest[$i] & @CRLF) $crc = BitOR(BitShift($crc, 8), BitShift($crc, -8)) $crc = BitXOR($crc, $Buffer[$i]) $crc = BitXOR($crc, BitShift($crc & "0xff", -4)) $crc = BitXOR($crc, BitShift($crc & "0xff", -1)) Next EndIf Return($crc) EndFunc Thanks, NiVZ Edited August 27, 2009 by NiVZ
NiVZ Posted September 2, 2009 Author Posted September 2, 2009 (edited) Hello, So I pressed on and made the C program output the result at each stage and got the following: BUFFER: 2 ffff fffd fff2 dff2 c1b2 BUFFER: 6 b2c1 b2c7 b2cb 2cb 1bab BUFFER: 6 ab1b ab1d ab1c 6b1c 689c BUFFER: 3 9c68 9c6b 9c6d 4c6d 41cd I've modified my AutoIt program and am pretty close. I get the following output from AutoIT: BUFFER: 2 FFFF FFFD FFF2 DFF2 C1B2 BUFFER: 6 B2C1 B2C7 B2CB 02CB 1BAB BUFFER: 6 FB1B FB1D FB1C 3B1C 389C BUFFER: 3 BE38 BE3B BE38 3E38 3938 The first two sections are correct, but the third and fourth are wrong. 3rd section starts FB1B when it should be AB1B. Any ideas? Program code below: expandcollapse popup$test_crc = DllStructCreate("byte[4]") DllStructSetData($test_crc, 1, Binary("0x02060603")) $crc = crc_calculate_crc(0xFFFF, $test_crc, 4) ; -- User Functions --------------------------------------------- Func crc_calculate_crc($initial_crc, $buffer, $length) $crc = $initial_crc If DllStructGetData($buffer, 1) <> "" Then For $i = 1 to $Length ConsoleWrite("BUFFER: " & DllStructGetData($buffer, 1, $i) & @CRLF) $crc = BitOR(BitShift($crc, 8), BitShift($crc, -8)) ConsoleWrite(Hex($crc,4) & @CRLF) $crc = BitXOR($crc, DllStructGetData($buffer, 1, $i)) ConsoleWrite(Hex($crc,4) & @CRLF) $crc = BitXOR($crc, BitShift(BitAND($crc, 0xff), 4)) ConsoleWrite(Hex($crc,4) & @CRLF) $crc = BitXOR($crc, BitShift(BitShift($crc, -8), -4)) ConsoleWrite(Hex($crc,4) & @CRLF) $crc = BitXOR($crc, BitShift(BitShift(BitAND($crc, 0xff), -4), -1)) ConsoleWrite(Hex($crc,4) & @CRLF) Next EndIf Return($crc) EndFunc Edited September 2, 2009 by NiVZ
NiVZ Posted September 2, 2009 Author Posted September 2, 2009 Figured out the problem. The CRC is supposed to be a SHORT. When I'm doing the BitShift, AutoIT isn't dropping the characters that should 'fall-off' the left hand side. eg when CRC is 1BAB and I left shift it by 8 I should get AB00 but I'm getting 1BAB00 Anyone give me a clue on how to fix this? NiVZ
jvanegmond Posted September 2, 2009 Posted September 2, 2009 This example might help you: ; 9: 0000 1001 $a = 9; $a = BitShift($a, -1) ; Output: 0001 0010 ; Expect: 0010 ; 15: 0000 1111 $b = 15 $a = BitAND($a, $b) ; Output: 0010 github.com/jvanegmond
Malkey Posted September 2, 2009 Posted September 2, 2009 Figured out the problem. The CRC is supposed to be a SHORT. When I'm doing the BitShift, AutoIT isn't dropping the characters that should 'fall-off' the left hand side. eg when CRC is 1BAB and I left shift it by 8 I should get AB00 but I'm getting 1BAB00 Anyone give me a clue on how to fix this? NiVZ This appears to work.expandcollapse popup; $test_crc = DllStructCreate("byte[4]") DllStructSetData($test_crc, 1, Binary("0x02060603")) $crc = crc_calculate_crc(0xFFFF, $test_crc, 4) ; -- User Functions --------------------------------------------- Func crc_calculate_crc($initial_crc, $buffer, $length) $crc = $initial_crc If DllStructGetData($buffer, 1) <> "" Then For $i = 1 To $length ConsoleWrite("BUFFER: " & DllStructGetData($buffer, 1, $i) & @CRLF) $crc = BitOR(BitShift(BitAND($crc, 0x0000FFFF), 8), BitShift(BitAND($crc, 0x0000FFFF), -8)) ConsoleWrite(Hex($crc, 4) & @CRLF) $crc = BitXOR($crc, DllStructGetData($buffer, 1, $i)) ConsoleWrite(Hex($crc, 4) & @CRLF) $crc = BitXOR($crc, BitShift(BitAND($crc, 0xff), 4)) ConsoleWrite(Hex($crc, 4) & @CRLF) $crc = BitXOR($crc, BitShift(BitShift($crc, -8), -4)) ConsoleWrite(Hex($crc, 4) & @CRLF) $crc = BitXOR($crc, BitShift(BitShift(BitAND($crc, 0xff), -4), -1)) ConsoleWrite(Hex($crc, 4) & @CRLF) Next EndIf Return ($crc) EndFunc ;==>crc_calculate_crc ;
NiVZ Posted September 2, 2009 Author Posted September 2, 2009 (edited) Thanks for the replies ) With your help, I've now done a BitAND with 0x0000FFFF after each XOR to eliminate the bits that move to far along. Here's the code: expandcollapse popup$test_crc = DllStructCreate("byte[8]") DllStructSetData($test_crc, 1, Binary("0x020A00051F010003")) $crc = crc_calculate_crc(0xFFFF, $test_crc, 8) MsgBox(0,"CRC", $crc) ; -- User Functions --------------------------------------------- Func crc_calculate_crc($initial_crc, $buffer, $length) $crc = $initial_crc If DllStructGetData($buffer, 1) <> "" Then For $i = 1 To $length ConsoleWrite("BUFFER: " & DllStructGetData($buffer, 1, $i) & @CRLF) $crc = BitOR(BitShift($crc, 8), BitShift($crc, -8)) $crc = BitAND($CRC, $initial_crc) ConsoleWrite(Hex($crc, 8) & @CRLF) $crc = BitXOR($crc, DllStructGetData($buffer, 1, $i)) $crc = BitAND($CRC, $initial_crc) ConsoleWrite(Hex($crc, 8) & @CRLF) $crc = BitXOR($crc, BitShift(BitAND($crc, 0xff), 4)) $crc = BitAND($CRC, $initial_crc) ConsoleWrite(Hex($crc, 8) & @CRLF) $crc = BitXOR($crc, BitShift(BitShift($crc, -8), -4)) $crc = BitAND($CRC, $initial_crc) ConsoleWrite(Hex($crc, 8) & @CRLF) $crc = BitXOR($crc, BitShift(BitShift(BitAND($crc, 0xff), -4), -1)) $crc = BitAND($CRC, $initial_crc) ConsoleWrite(Hex($crc, 8) & @CRLF) Next EndIf Return Hex($crc,4) EndFunc ;==>crc_calculate_crc Thanks again, NiVZ Edited September 2, 2009 by NiVZ
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now