A program to calculate the checksum of one variable is here below (source: http://forum.arduino.cc/index.php?topic=488003.0).
However, I need a program (based on this below if possible) that calculates this checksum for 3 floats, 2 integers and one ASCII character. These variables are individually available.
In reverse, the same calculation needs to be done at the receiving end where the data is available as string characters separated by commas. For example: 43.25,28.25,11.20,33,28,A. (the dot after A is included).
So at the receiving end the same values as at the transmitting side are available but separated with commas.
Code:
String dataToParse;
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop()
{
// put your main code here, to run repeatedly:
if (Serial.available() > 0)
{
dataToParse = Serial.readString();
Serial.print("ToCheckSum.:");
Serial.println(dataToParse);
Serial.print("");
Serial.print("ToCheckLength.:");
Serial.println(dataToParse.length());
Serial.print("");
char toCheck[dataToParse.length()];
Serial.print("Size ToCheck.:");
Serial.println(sizeof(toCheck));
dataToParse.toCharArray(toCheck,dataToParse.length());
byte aaa = getCheckSum(toCheck);
Serial.print("CheckSum HEX.:");
Serial.println(aaa,HEX);
Serial.print("CheckSum DEC.:");
Serial.println(aaa,DEC);
Serial.print("");
dataToParse ="";
}
}
uint8_t getCheckSum(char *string)
{
int XOR = 0;
for (int i = 0; string[i] != '\0'; i+= 2) // <-- increment by 2
{
Serial.print("i.:");
Serial.println(string[i]);
Serial.print("");
// make single byte value out of 2 characters from the string...
byte b1,b2,b;
// First byte: hex to bin
b1 = string[i];
if (b1 >= 'a')
b1 = b1 - 'a' + 10;
else if (b1 >= 'A')
b1 = b1 - 'A' + 10;
else
b1 -= '0';
// Second byte: hex to bin
b2 = string[i + 1];
if (b2 >= 'a')
b2 = b2 - 'a' + 10;
else if (b2 >= 'A')
b2 = b2 - 'A' + 10;
else
b2 -= '0';
// Combine the two
b = 0x10 * b1 + b2;
XOR = XOR ^ b;
}
return XOR;
}