Go Down

Topic: Printing Char from DEC in serial (Read 958 times) previous topic - next topic

Grumpy_Mike

Quote
The issue is that Serial.read() returns an int (I'm betting).

You loose your bet, it returns the byte that was sent, normally an ASCII value but it depends what was sent.

PaulS


Quote
The issue is that Serial.read() returns an int (I'm betting).

You loose your bet, it returns the byte that was sent, normally an ASCII value but it depends what was sent.

No, he wouldn't. The return type for Serial.read() is an int. In general, the data is in the low order byte. The error code, if any, is in the high order byte.

The Serial.print() overload for int does not know that only the low order byte contains data of interest, as a char, so it doesn't print the value that way.

You can do several things. One is to store the low order byte only in a char variable, then print the char variable. Another is to cast the Serial.read() output as a char, when printing.

taswanj

To: wd40bomber7, that was it! Thanks for teaching me something. That forced the 'receiver' side to print out the data in the correct CHAR format. Thanks a ton, I can think of many other ways to use this casting now.

Thanks a ton.

Go Up