MTSkull
Centurion
Part 2 of an earlier post. Going in a different direction.
A vendor gave me some code to generate a checksum. I got the vendor code working and it does not even come close to returning the right value, so I suspect they did not understand what I was asking. It does generate a correct CRC 16 value but that is not what their program produces. If i can figure this out then I can automate one labor intensive process and eliminate a test further down the factory line.
I found this tutorial for generating checksums at http://www.netfor2.com/checksum.html I used this to base my new algorithm on because I downloaded 010 Editor by Sweetscape. It calculates various checksums for bin files. Running this against my hex file I find that it generates a 16 bit Bigendian Checksum that equals the vendors correct checksum.
Against the attached Random Binary File I get. I had to change the extension to .txt, changing it back to .bin should yield the same data.
010Editor
Checksum, Ushort (16bit), Bigendian = 00000004 00162847
Vendor Value I am trying to Duplicate, generated by their manual software.
Data Sumcheck Unswapped=162847 (same as above without the carry)
My algorithm returns 16284B or 4 off the correct answer. Which, incidentally, is the same as the carry left over in the upper bytes from 010Editor.
Here is the algorithm I developed.
A vendor gave me some code to generate a checksum. I got the vendor code working and it does not even come close to returning the right value, so I suspect they did not understand what I was asking. It does generate a correct CRC 16 value but that is not what their program produces. If i can figure this out then I can automate one labor intensive process and eliminate a test further down the factory line.
I found this tutorial for generating checksums at http://www.netfor2.com/checksum.html I used this to base my new algorithm on because I downloaded 010 Editor by Sweetscape. It calculates various checksums for bin files. Running this against my hex file I find that it generates a 16 bit Bigendian Checksum that equals the vendors correct checksum.
Against the attached Random Binary File I get. I had to change the extension to .txt, changing it back to .bin should yield the same data.
010Editor
Checksum, Ushort (16bit), Bigendian = 00000004 00162847
Vendor Value I am trying to Duplicate, generated by their manual software.
Data Sumcheck Unswapped=162847 (same as above without the carry)
My algorithm returns 16284B or 4 off the correct answer. Which, incidentally, is the same as the carry left over in the upper bytes from 010Editor.
Here is the algorithm I developed.
Code:
// following comments copied from http://www.netfor2.com/checksum.html
//01 00 F2 03 F4 F5 F6 F7 00 00
//(00 00 is the checksum field)
//Form the 16-bit words
//0100 F203 F4F5 F6F7
//Calculate 2's complement sum
//0100 + F203 + F4F5 + F6F7 = 0002 DEEF (store the sum in a 32-bit word)
//Add the carries (0002) to get the 16-bit 1's complement sum
//DEEF + 002 = DEF1
//Calculate 1's complement of the 1's complement sum
//~DEF1 = 210E
//We send the packet including the checksum 21 0E
//01 00 F2 03 F4 F5 F6 F7 21 0E
UInt32 OnesCompCheckSum_WCarry(byte[] Bytes)
{ // developed based on above comments
UInt64 CheckSum = 0; // bigger to capture overflow in upper bytes
UInt32 tempOnes = 0;
string temp = "";
for (int x = 0; x < Bytes.GetUpperBound(0); x += 2)
{
//swapped
//temp = Bytes[x + 1].ToString("X").PadLeft(2, (char)48);
//temp += Bytes[x].ToString("X").PadLeft(2, (char)48);
//unswapped // very close to desired
temp = Bytes[x].ToString("X").PadLeft(2, (char)48);
temp += Bytes[x + 1].ToString("X").PadLeft(2, (char)48);
tempOnes = UInt32.Parse(temp, NumberStyles.HexNumber);
tempOnes = ~tempOnes;
CheckSum += tempOnes;
}
//add the carry back in.
temp = CheckSum.ToString("X").PadLeft(16, (char)48); // check sum to 16byte hex string
UInt32 Carry = UInt32.Parse(temp.Substring(0,8),NumberStyles.HexNumber); // first 8 hex chars, to int
tempOnes = UInt32.Parse(temp.Substring(8), NumberStyles.HexNumber); // last 8 hex chars, to int
tempOnes += Carry;
CheckSum = ~tempOnes; //so I can set a break point and see what I am returning
temp = CheckSum.ToString("X").PadLeft(16, (char)48);
return ~(tempOnes);
}