Understanding Serial.available()

Serial.available() can return -1 if no data available and values from 0 to 255.

Question 1:When it can return 0?

Question2: We are checking if serial available for exanple via:

while(serial.available()); Or while(!serial.available());

!serial.available will check if its not 0 but if its -1 then whouldnt it return true? Cos its not 0.

Thanks.

Serial.available() can return -1 if no data available and values from 0 to 255.

No, that is what serial read returns when you try to read an empty buffer. When there is no data to read it returns 0.

This solves the rest of your problems.

From the reference page

Returns The number of bytes available to read .

No mention of -1

Grumpy_Mike: No, that is what serial read returns when you try to read an empty buffer. When there is no data to read it returns 0.

I think the OP may find that a little confusing.

Serial.available() never returns -1, it can only return values from 0 to 64 (assuming the standard 64-byte buffer).

Serial.read() will return -1 if there is no data in the buffer.

...R

Sorry!!! Messed up with serial.available and read.

Dont want to edit the post. So yes when serial.read can return 0?

Returns
The first byte of incoming serial data available (or -1 if no data is available) - int.

So yes when serial.read can return 0?

Only when the first byte available is 0.

Can you give example when first byte can be 0?

surepic: Can you give example when first byte can be 0?

Without knowing what is sending the serial data? No.

Situation like this:

Int c;
//serial device on the other end will //send ‘null’
If(serial.available()==1){
c=Serial.read();
}

What is in c?

surepic: Can you give example when first byte can be 0?

That is a strange question.

If the device that is sending the data sends the byte value of 0 then there will be a 0 to be read. Bytes can have any value from 0 to 255.

If the sending device is just sending human-readable text then the byte values will be in the range 32 to 126 and there won't be a 0 in the data stream.

...R

which means its when serial.available is not 0 then its impossible for serial.read() to return 0?

which means its when serial.available is not 0 then its impossible for serial.read() to return 0?

When Serial.available() is not 0, it is either -1 (no data in serial input buffer) or there are 1 or more bytes in the buffer, any of which could be 0.

Thanks everybody for answers now i got it! And sorry for the mess with wrong thread title and description.

groundFungus:
When Serial.available() is not 0, it is either -1 (no data in serial input buffer) or there are 1 or more bytes in the buffer, any of which could be 0.

Serial.available() returns the number of bytes in the buffer. There can NEVER be -1 bytes in the buffer.

PaulS: Serial.available() returns the number of bytes in the buffer. There can NEVER be -1 bytes in the buffer.

I also had to read that twice; in reply to the previous message it made sense. It refers to Serial.read(), not to Serial.available().

Serial.available() returns the number of bytes in the buffer. There can NEVER be -1 bytes in the buffer.

Yeah, I managed to confuse myself.

groundFungus: Yeah, I managed to confuse myself.

And the rest of the world :)

Serial.read() will return -1 if there is no data in the buffer.

It returns FF and not -1? If I am asked to take FF as -1, I would rather prefer to take FF as 255.

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{ 
  byte n1 = Serial.read();
  Serial.print(n1, HEX);
  while(1);
    
}

It returns FF and not -1? If I am asked to take FF as -1, I would rather prefer to take FF as 255.

But you would be wrong to do so unless you cast the variable it returns as an unsigned byte and not as the default signed byte.

But you would be wrong to do so unless you cast the variable it returns as an unsigned byte and not as the default signed byte.

If FF is taken as a signed byte by default, I have do some manipulation (shown below) to show -1 on the Serial Monitor.

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{ 
  byte n1 = Serial.read();
  n1 = n1-1;
  n1 = ~n1;
  Serial.print("-");
  Serial.print(n1, HEX);  //Serial Monitor show: -1
  while(1);
 }