I am at a loss, the strings() are new to me, and I am getting some confusing results, my aim is to receive a message from a serial port, then generate a checksum to compare with that of the incoming message.
The code below only receives a message and generates a checksum, the checksum function works and is tested.
The bit that is odd is the Serial.print(msg), it prints out the hex for the received chars rather then their representation e.g. 'A' prints as 65... why?
String msg;
void setup() {Serial.begin(57600); Serial.print("send a message...");}
void loop()
{
msg="";
if (Serial.available())
{
while (Serial.available() > 0) {msg += Serial.read();Serial.println(msg);}
Serial.print("msg= ");
Serial.print(msg);// ***<---- this line is the problem!***
Serial.print("checksum=");
Serial.print(generate_checksum(msg), HEX);
Serial.println();
}
}
byte generate_checksum(String this_string)
{
int num = this_string.length()-1;
byte XOR = 0;
for(int i=0; i<(num); i++){XOR = XOR ^ (this_string.charAt(i)*i);}
return XOR;
}
sample output in response to 123
49
4950
495051
49505110
msg= 49505110checksum=C4
No, it's printing the decimal representation for the ASCII character. I.E., key "1" = decimal 49. If you want the numeric representation, subtract 40 ("0").
Hi Gents, thanks for you replies, they are correct and would work if the incoming data was only numeric, however it is mixed... an example would be: @T1625003730; i.e a command setting the internal clock
So the above solution works just fine, in reality the messages never get printed out/displayed, they are fed to an interpreter which acts upon correctly formatted and checksum blessed messages
The underlying problem is that Serial.read() returns an int NOT a char
So when you go to print it OR add it to a String it is treated as a int and printed and added as such.
this works
int i = Serial.read();
msg += (char) i;
Serial.println( char) i);
My Arduino Software Solutions tutorial as various sketches for reading from Serial, with their pros and cons.