Data is constantly coming into the serial line, and a certain order needs to be extracted from it. It starts at '0', and has a fixed size.
I can’t figure out what is causing the byte reading error.
If you display the data in the place where I wrote '85', then the first byte there will actually be '85', as it should be immediately after '0'.
But a few lines below this number becomes like '-1'
Because of this, I cannot compare the data stream with the target string.
I tried to redo it in different ways, but always when I call 'Serial.read()' for the second time, the first byte breaks. The rest of the bytes are fine.
What is the reason for this and how can I fix it?
byte dataError[] = {85, 149, 133, 237, 249, 139, 63, 159};
int errCount = 0;
if (Serial.available() > 0) {
if (Serial.read() == 0) {
// '85' !
for (int i = 0; i < 8; i++) {
if (Serial.read() == dataError[i]) {
errCount++;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(Serial.read()); // '-1' instead of '85' !
break;
}
}
}
if (errCount == 8) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("error");
delay(2000);
errCount = 0;
}
}
If you get back a value of -1 from Serial.read() then it means that there is nothing to read
You are testing what Serial.available() returns and if at least 1 byte is available then you read 8 bytes. What if all of the required data is not yet available ?
-1 is that Serial.read returns when you call it but there is no byte there to be read.
If you get back a value of -1 from Serial.read() then it means that there is nothing to read
Thanks guys, that's basically what I thought.
But the problem is only with the first byte after '0'. The rest are output without problems. I also lengthened the output data string, and all the bytes are displayed normally. Even if I set a delay, I can see every byte on the display, but not '85' (only first Serial.read() will print this out)
It doesn’t matter how the problem manifests itself - in reading only the first byte or the rest. In any case, the fact that your code is incorrect is obvious at first glance.
So fix the bug you were told about and test again.
if (Serial.available() >= 8)
if (Serial.read() == 0) {
for (int i = 0; i < 8; i++) {
while (!Serial.available()) {}
if (Serial.read() == dataError[i]) {
. . .
Every time you call Serial.read, you either get -1 if there is nothing available, or the waiting byte. Once that happens, that byte is gone. If you need to use it more than once, you need to store it in a variable.
Inside the loop, read is definitely called there and then sometimes also a second time here
for what the loop is implying should be the same byte. So you may get -1 again.
lcd.print(Serial.read()); // '-1' instead of '85' !
The reason for this output in the code is that I needed to make sure which of the incoming bytes did not match the byte from the array. I also displayed them together:
If there are exactly bufferSize bytes available, the read consumes one, and then readBytes will consume and return bufferSize - 1 (which you don't check). The last byte in buffer will not be set/overwritten, and may coincidentally match (or not) the last byte of dataErr