Serial Problem

Hallo, I write me a function:

boolean cancel_serial() {
//int inByte='0';  
int inByte; 
boolean output = false;

      if (Serial.available() > 0) {
         inByte = Serial.read();  // Byte lesen
       if (inByte=='P') output=true; else output=false;
      }
      
return output;      
}

and this this I theck if the Serial P comes then the function is true oterwise it? false

if I now use if(check_serial()){
...
}

It do not work always right. Sometimes it works, sometimes not.

Why could this happen?

thx
druckgott

It do not work always right. Sometimes it works, sometimes not.

Why could this happen?

What is sending serial data? If you are using the Serial Monitor to send the data, what line ending do you have selected?

The code you posted snippets of does not wait for serial data to arrive. Yet the check seems to think that the function did wait.

I use Proccessing 2.1
There I call it with a button:
case(10): // Cancel
println("Cancel");
myPort.write('P');
break;

It should also do not wait, its in the mainloop, if the serial P is availibe then the if should work, otherwise not

if the serial P is availibe then the if should work, otherwise not

It should. That it appears not to implies that the problem is not in the snippets you posted. You ignored my hint last time, that you should post all of your code. Do not ignore it again.

Here it is, the Problem is in line 308

ArduinoCNC_1_5.ino.zip (5.74 KB)

the Problem is in line 308

The problem is that you only supplied one of the two programs that are talking to each other.

You call check_serial() at the top of loop. It reads ONE character, if there is any. Then, you make decisions about what to do. Some of those decisions involve reading more serial data. We have no idea what the program sending the data is doing, but you seem to be under the impression that sending a 'P' causes that letter to be at the head of the queue, so the next call to check_serial() will get the 'P'. That may not be a valid assumption. Hard to say without seeing the Processing application, too.

Hallo, I can upload the file, it says that the folder for upload is full.

So I Upload here:

is it posible to Print the queue by an way or Do I need to use a buffer?

is it posible to Print the queue

The serial buffer? No, there is no way, without modifying the HardwareSerial class, to print what is in the buffer. You would need to read the data, and save it somewhere else.

I have Put in q Print for testen the Funktion and i always get false, but the loop is Not ging on some timer

is tiere a posibility so sage the serial to a buffer or so