Serial Usage

Hello

I'm using a Mega and two serial ports.

Does the Serial buffer always get emptied after a Serial.read()?

I am expecting a response byte, before I drop into a loop

I am using Serial1.available() code example cut & paste from the reference section
I am reading this byte with Serial1.read()
I am printing this Byte to the screen

LIKE I said exactly as the Serial.available()

Now, when I read in the 8 subsequent bytes I am waiting for I actually get the response byte and then 7 new Bytes (as expected)

Am I really expected to use flush() in this scenario?
Thanks

Each call to Serial.read() removes one byte from the buffer (if there's one available) and returns its value.

The .flush() function has not had anything to do with the input buffer for years. These days it waits until the last OUTPUT character has left the hardware. It only flushes the output buffer.

If there are 5 bytes in the serial buffer it takes 5 read()s to empty the buffer. If you only read 4 bytes, 1 will be left in the buffer.

You can clear the buffer with:

void clearBuffer()
{
  while(Serial.available())
  {
    Serial.read();   // read a byte from the receive buffer and throw it away
  }
}

Thank all for the replies - yeah unfortunately that's what I presumed!!!!

As a cheeky follow up - I'm also having strange results with the Serial.available() command. It doesn't seem to always detect a byte in the buffer. In the examples given in the reference section Serial.available() is used in a for loop. So on face value, to me, this is a 'polling' command - Am I right? Or does it do anything 'fancy' behind the scenes eg using interrupts?

By way of an explanation the only reliable way I have found to detect my required response byte is to include a Serial.read() in Do while loop, which I find odd.

Docara:
Thank all for the replies - yeah unfortunately that's what I presumed!!!!

As a cheeky follow up - I'm also having strange results with the Serial.available() command. It doesn't seem to always detect a byte in the buffer. In the examples given in the reference section Serial.available() is used in a for loop. So on face value, to me, this is a 'polling' command - Am I right? Or does it do anything 'fancy' behind the scenes eg using interrupts?

By way of an explanation the only reliable way I have found to detect my required response byte is to include a Serial.read() in Do while loop, which I find odd. The cheeky part is asking about something like this, without displaying your code. Of course nobody can guess what you're doing...

Serial.available() always detects a byte in the buffer. The Serial input is interrupt driven, it does not need to be polled. You are doing something wrong.

Docara:
LIKE I said exactly as the Serial.available()

Now, when I read in the 8 subsequent bytes I am waiting for I actually get the response byte and then 7 new Bytes (as expected)

It would help me to see the exact code you are running. Could you post your sketch?

Hi

Will do however I've just been burned and lost my original code. I didn't realise the "Save when verifying and upload" option was set in preferences and have been troubleshooting the code which over wrote the original version - so I have to rewrite (and remember) my code.

Also, I've just dropped a breadboard jumper wire on my buffer chip and ...well I need to do some soldering!!!!!!

Will post back when up and running again ggrrr

Hi
Ok so here is my (now greatly reduced) code and screen grabs showing Serial1.available() not detecting my response byte

void setup() {

  Serial.begin(115200);
  Serial1.begin(9600);
  Serial.println("Starting...");
  Serial1.write(Reset);                 //    0xC1 - Calibration Reset @ 9600
  Serial1.write(ResetFlex);             //    0xCD - Reset and Change to Flex Mode


  if (Serial1.available() > 0) {        //    Check for recieved Byte
    response = Serial1.read();          //    if yes write to response
    Serial.print("I received: 0x");
    Serial.print(response, HEX);
    Serial.print(" Using Serial.available()");

  }   // End of If curlies
}    // End of Setup() Section curlies

void loop() {

}

Thanks

And here is my code amended code that works

void setup() {

  Serial.begin(115200);
  Serial1.begin(9600);
  Serial.println("Starting...");
  Serial1.write(Reset);                 //    0xC1 - Calibration Reset @ 9600
  Serial1.write(ResetFlex);             //    0xCD - Reset and Change to Flex Mode

 do{
  response=Serial1.read(); 
 }while(response!=0xCD);
       
    Serial.print("I received: 0x");
    Serial.print(response, HEX);
    Serial.print(" Using Serial.available()");

  
}   // End of Setup() Section curlies
void loop() {

}

The code in reply #8 isn’t waiting for a response.

HI -

Why - I don't understand?

Serial1.available() is in an 'if' loop which is, on face value, continually checking if data is in the receive buffer before proceeding - is it not?

Here is the code given from the Arduino Reference what's the difference?

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

void loop() {
  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.print(inByte, DEC);
  }
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.print(inByte, DEC);
  }
}

Serial1.available() is in an ‘if’ loop

No such thing.
if does not loop.
setup does not loop.

Update

I have cut and pasted the example code into a new sketch as is and nothing, Serial.available() does not work no matter where I put it. However it does work on a genuine UNO in both Setup() or Loop()

TheMemberFormerlyKnownAsAWOL there's nothing wrong putting code in Setup() if you want it to run once

I am not using a genuine Mega - I am using a Mega Pro from RoboDyn. I wonder if the chip is genuine - any thoughts?

Can you send data to the serial monitor?

The code in reply #8 isn’t waiting for a reply.

Hopefully that’s a little clearer.

For further clues, check here

screen grabs showing Serial1.available() not detecting my response byte

Serial1.available is not prescient.

there's nothing wrong putting code in Setup() if you want it to run once

True, but in setup() you cannot us an if alone to wait for something to happen as it is a one time test and setup() does not loop

Docara:
I am not using a genuine Mega - I am using a Mega Pro from RoboDyn. I wonder if the chip is genuine - any thoughts?

RobotDyn is top shelf. Almost nobody makes boards with such high quality. I doubt that they would sully their reputation with a fake.

I have a couple of those Mega Pro boards and have had no issues with them. I would be very surprised if they are fake.

Good to know about RobotDyn

But I am still at a loss why Serial.available() doesn’t work for me on the Mega.

I light the inbuilt LED with a serial Byte is available.
I keep the same code on the screen in my sketch and change the board from ‘Uno’ to Mega2560 (with 2560 processor with same Com) and the code doesn’t work.

Ah! well one of those things …

anyway thank you all for the replies