When I take (char)Serial.read() and sends it out to the serial like this: Serial.println((char)Serial.read());
Then if (char)Serial.read() doesn't contain anything, it writes ⸮ in the Serial.
I would like to be able to make an if-statement, to check if the (char)Serial.read() is equal to ⸮. But I can't seem to figure that out. I tried a couple of if-statements that didn't work:
Now it is important to note, that I have the right Serial-configurations. It is set to 9600 baud, and it works if the (char)Serial.read() has something in it, or if say I were to put another variable in the Serial. So the problem is in figuring out how to check whether it is going to write ⸮.
if there is nothing to read, Serial.read() returns -1 as an int, so 0xFFFF. when you cast as a char, you take the LSB 0xFF and so you are sending the ASCII character 255 to the Serial terminal. As there is no ASCII character matching that, you get the ⸮
so you can go through a variable:
int r = Serial.read();
if (r == -1) {
// there was nothing to read
...
} else {
// the LSB of r contains the byte you just received
}
alternatively, if you want to read only when something is available then check for it
if (Serial.available() != 0) {
// then you have something to read
}
void loop() {
int c = Serial.read(); // note read() returns an int
if (c != -1) { // read() return -1 if there is nothing to be read.
// got a char handle it
Serial.println((char)c); // need to cast to char c to print it otherwise you get a number instead
}
}
and lots of other examples of how to read/parse from Serial with their pros and cons