how to use a second buffer

Hi everyone,

I am using an arduino MEGA to read 2 serial ports, the info received on Serial1 goes to buffer but now I am having trouble finding a way to stock the info from Serial2.

Both serials receive information at the same time, and not the same info is received. I am able to stock the info on Serial1 in the 64 byte buffer but I need to a second one for Serial2.

Any ideas?

Thank you
Filipe

Can't you just do exactly the same thing with Serial2 as you did with Serial1, using another buffer?
Edit: The serial buffers can by default hold 64 bytes, but no more. (That limit can be increased though.)

Perhaps you should post your code, (between

[code]code tags[/code]

of course).

Any ideas?

Just do it. Like OldSteve, I fail to see what the problem is. You know which port you are reading from. You, therefore, know which buffer you want to write to.

here is the code I am trying to use, it works OK for Serial3 but not for Serial

void loop() {

  myNextion.listen().toCharArray(rx_message_A, 25);
  myNext.listen().toCharArray(rx_message_B, 25);

  Serial3.print("IF;"); // send request for frequency only when bandecoder is connected alone to the radio
  Serial3.flush(); // wait for the transmission of outgoing serial data from the Arduino to the radio to be completed
  //delay(200); // we don't have to be always polling the radio frequency... we can wait for a while...XD
  
  if (Serial3.available() > 0) 
  {    
    // get the first byte
    inByte = (char) Serial3.read();
    
    if (inByte == 'I')  // when the first byte is 'I' then we have to continue reading the input data 
    {
      buffer += inByte; 
      while (Serial3.available() > 0) 
      {
        inByte = (char) Serial3.read(); 
        buffer += inByte; 
        
        if (inByte == ';')  // the reading procedure finishes when we read the ';' character
        {
          stringComplete = true; 
        }
      }
    }
    // update band output only if the complete data has been received 
    if (stringComplete) 
    {
      writeBand();
      buffer = ""; // clear the RX buffer
      stringComplete = false; 
    }
  }


  Serial.print("IF;"); // send request for frequency only when bandecoder is connected alone to the radio
  Serial.flush(); // wait for the transmission of outgoing serial data from the Arduino to the radio to be completed
  //delay(200); // we don't have to be always polling the radio frequency... we can wait for a while...XD
  
  if (Serial.available() > 0) 
  {    
    // get the first byte
    inByte_b = (char) 
    Serial.read();
    
    if (inByte_b == 'I')  // when the first byte is 'I' then we have to continue reading the input data 
    {
      buffer += inByte_b; 
      while (Serial.available() > 0) 
      {
        inByte_b = (char) 
        Serial.read(); 
        buffer += inByte_b; 
        
        if (inByte_b == ';')  // the reading procedure finishes when we read the ';' character
        {
          stringComplete_b = true; 
        }
      }
    }
    // update band output only if the complete data has been received 
    if (stringComplete_b) 
    {
      writeBand_b();
      buffer = ""; // clear the RX buffer
      stringComplete_b = false; 
    }
  }
}

now I am having trouble finding a way to stock the info from Serial2.

here is the code I am trying to use, it works OK for Serial3 but not for Serial

Serial2 or Serial3?

it works OK for Serial3 but not for Serial

Regardless of which port you read from, you append to the same String (that is NOT a buffer).

Why on earth would you do that?

        buffer += inByte;
        buffer += inByte_b;

You have chosen to use the same String variable, named buffer, to store the incoming data from different Serial links. No wonder you have a problem.

Here is a radical idea. Put the data from, say Serial1 in a variable named buffer1 and the data from Serial2 in a variable named buffer2. Even more radical would be to use an array to hold the data with each Serial link having its own level in the array.

By the way, you may want to consider using arrays of chars instead of Strings to hold the data.

You should never expect all bytes to be available in one go. If you send 20 bytes, it might be that you receive 10 bytes, 1ms nothing and next 10 bytes again. The 1ms might be the time that it takes the hardware to receive the byte (roughly at 9600 baud) or the sender has to do something else for a short while so does not send.

Look at the serial input basics - updated to get an idea how to read serial data in general.

You should also not share the buffer if there is a chance that they interfere with each other; there is a very good chance that that happens. Use two, three or N buffers.

With your use of String

String buffer; // buffer for Serial
String buffer3; // buffer for Serial3

And I thought the whole point of the thread was "how to use a second buffer".
As everyone points out, to use a second buffer, you need to use a second buffer.

Thanks guys, got it working now.

OldSteve:
And I thought the whole point of the thread was "how to use a second buffer".

Which is why his code had only one buffer. The question was how to get a second one. :wink: