Serial.Event() with two serial ports and serial.readbytes

Hello all,

I am trying to use Arduino Mega 2560 to communicate with two serial ports. They both are using 19200 baud.
I was using Serial.readBytes in my loop() but i think this can give problems with timing since sometimes packets will not get directy through.

So i was looking into Serial.Event maybe this could solve my timing related problems(i want real-time packet delivering)
Is this the correct way of using Serial.Event and Serial.readBytes

char receivevalue[40] = "";
bool XbeeRXavailable = false;
int rcvsize = 0;

void setup() {
  Serial1.begin(19200)
  Serial3.begin(19200)
}

void loop() 
{
  if(XbeeRXavailable)
  {
    Serial3.write(receivevalue, rcvsize);
    receivevalue ="";
    XbeeRXavailable = false;
  }
}
void serialEvent()
{
  int rcvsize = Serial.readBytes(receivevalue,40);
  XbeeRXavailable = true;
}

Or is there a problem with reading serial data into a Array? how is this best performed? i wan't to use the incomming packet in my Arduino sketch so i can't just receive 1 byte(serial1) and send 1 byte(serial3).

serialEvent() will run at the end of the loop() function if serial data has been received on the Serial input. However, you cannot be sure how many bytes have been received (it may only be one).

Once you know that serial data has been received you need to read it into a variable. If that variable is an array you can use Serial.readBytes() but you must be sure that all the data is available before you use it or use Serial.read() to read individual bytes to read them one at a time as they become available.

Thanks,

I got one working with Serial.Event() and Serial.readBytes()

But it doesn't seem to be working for serial port 3 on the Mega. I got no errors but Serial.Event3() won't pick up the serial data:

void loop()
{
  if (UARTavailable) 
  {
    Serial1.println("UART available");
  }
  if(XbeeRXavailable)
  {
   Serial1.println("Xbee available");
  }
}
void serialEvent()
{
  rcvsize = Serial.readBytes(receivevalue,40);
  XbeeRXavailable = true;
}
void serialEvent3()
{
  rcvsize3 = Serial3.readBytes(value,16);
  UARTavailable = true;
}

So in this example SerialEvent() works and SerialEvent3() won't work

Because you only posted a snippet (hint) we can't tell if you set up Serial3 correctly

Sorry, here is the code:

char receivevalue[40] = "";
bool XbeeRXavailable = false;
bool UARTavailable = false;
int rcvsize = 0;

void setup() {
  Serial.begin(19200,SERIAL_8E1);
  Serial3.begin(19200,SERIAL_8E1);
  Serial1.begin(19200);
  Serial.setTimeout(4);
  Serial3.setTimeout(4);
}

void loop()
{
  if (UARTavailable) 
  {
    Serial1.println("UART available");
  }
  if(XbeeRXavailable)
  {
   Serial1.println("Xbee available");
  }
}
void serialEvent()
{
  rcvsize = Serial.readBytes(receivevalue,40);
  XbeeRXavailable = true;
}
void serialEvent3()
{
  rcvsize3 = Serial3.readBytes(value,16);
  UARTavailable = true;
}

I am using Serial1 only for debugging, my two serial ports communicatie over 19200 baud 8E1.
I can't even get the println to show. It seems the code won't run the serialevent3.

I am going to retract what I said in reply #1 about not knowing how many bytes are available because I have just realised that readBytes() is a blocking function that waits until the required number of bytes has been read or until a timeout occurs. Apologies for any confusion.

It seems the code won't run the serialevent3.

Flash an LED or print a message from inside serialEvent3() so that you know that it is being triggered or not.

What pins are you using for Serial3 ?