I have a program that sends and receives serial data. The board picks this up and does stuff depending on that the input was (in this case a char). Pretty straight forward stuff. However I cant seem to get the return value to be treated as a char. Any help would be appreciated.
I have cobbled together this form stuff on the net.
...snip
if (Serial.available() > 0){
returned[0] = Serial.read() - 48;
if (returned[0] == 'p') { //p for processing
isProcessing = true;
break;
} // else it remains false and we abort
}
...snip
First of all I am not exactly sure why i need to take of 48.
Secondly, how do I get returned[0] == 'p' to actually work as a comparison?
Subtracting 48 from a byte received over a serial link is a commonly used technique for converting an ASCII-encoded digit to its binary equivalent.
'0' = 0x30 = 48: subtract 48 and you get 0
'1' = 0x31 = 49: subtract 48 and you get 1
...
If, however, the byte is NOT a ASCII-encoded digit, this doesn't make any sense at all.
If you expect someone to send the letter 'p', then just check for it directly:
if (Serial.available() > 0){
returned[0] = Serial.read();
if (returned[0] == 'p') { //p for processing
isProcessing = true;
break;
} // else it remains false and we abort
}