0

I have three sensors latched on to three different Arduino devices.

The respective Arduino devices are connected on to an Arduino Mega on Serial1, Serial2 and Serial3.

The data from each device component (i.e., an Arduino with an accompanying sensor) were sent in data packets with different starter byte headers ('$' , '/' & '#' ) accompanying the buffered data from each individual device component onto the Mega on the Tx/Rx lines.

My question here is, how can I simultaneously read out all the data from the different serial ports on the Arduino mega sketch?

3 Answers 3

4

My question here is how can I simultaneously read out all the data from the different serial ports on the arduino mega sketch.

As you are using several serial ports data can already be received simultaneously. Each serial software class (Serial1, Serial2, Serial3) has an internal buffer where data is stored until it is read.

You only need to read the data before the internal buffers are filled and there is a risk for over-flow. Check that data is available and read as usual per serial port. Avoid any blocking calls to avoid buffer over-flow.

Cheers!

1

I had my arduino mega receiving the data from Serial2 and Serial3, by adopting a sketch that was developed by @Majenko in answer to the post Processing Serial data. The sketch below outputs data to the serial monitor. The NEW problem I have now is trying to develop a python sketch that would seperate the data from the buffers into two different csv files, with each csv files representing the data from each of the two sensors.

    static char buffer[20];
    int count =0;

    void setup() 
   {
    Serial.begin(115200);
    Serial2.begin(115200);
    Serial3.begin(115200);
    delay(50);

    }

    void loop(){

if (Serial.available()){  //Slide the window
   for(count=0; count<10; count++){
      buffer[count] = Serial.read();
      //Add the new character
      buffer[9] = Serial.read();

      //Check the framing window
   if((buffer[0] == '$') && (buffer[9] == 0xAC)){
     //Calculate the checksum
      uint16_t cs = 0;
   for (uint16_t i = 2; i < 8; i++){
    cs += buffer[i];
    }
    //if the checksum matches....
    if(cs == buffer[8]){
      //Send the byte to serial port three
      Serial2.write(buffer, count); 
  }
  }

 }
}

 if (Serial2.available()){
    for(count=0; count<10; count++){
      buffer[count] = Serial2.read();
      //Send the byte to serial port three
      Serial.write(buffer, count); 
     }
 }

 if (Serial.available()){  //Slide the window
   for(count=11; count<20; count++){
      buffer[count] = Serial.read();
      //Add the new character
      buffer[19] = Serial.read();

      //Check the framing window
   if((buffer[11] == '+') && (buffer[19] == 0xAE)){
     //Calculate the checksum
      uint16_t cs2 = 0;
   for (uint16_t i = 6; i < 18; i++){
    cs2 += buffer[i];
    }
    //if the checksum matches....
    if(cs2 == buffer[18]){
      //Send the byte to serial port three
      Serial3.write(buffer, count); 
  }
  }

 }
}

 if (Serial3.available()){
    for(count=11; count<20; count++){
      buffer[count] = Serial3.read();
      //Send the byte to serial port three
      Serial.write(buffer, count); 
     }
 }

}

0

The easiest and most overlooked way to read ANY hardware serial ( Serialx) is to utilize Arduino IDE main() function layout and let the serialEventx do the work.

Here is much simplified layout of IDE main () function.

main() { Setup() for(;;) { loop() serialEventx() } }

However using serialEvent requires much different approach to your program flow - the loop() cannot have any logic which would introduce a delay. Your program must process the serialEvent FIRST, loop() processing is secondary.

1
  • SerialEvent adds nothing and solves nothing. It is just as susceptible to delay as any code in (or called from) loop. The user-written serialEvent must still check available before doing read, so it is truly equivalent. It essentially moves the serial handling to the end of loop, without adding any capability. In spite of its name, the event routine is still blocked by loop, whether there are delays or not. If serialEvent had been called from the ISR, well, that would have been useful. :-/
    – slash-dev
    Feb 14, 2016 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.