Modified Hardware Serial code for Arduino MEGA 2560

Hello all,

I am currently facing issues with the hardware serial available on the arduino MEGA.

I am getting a lengthy string of data from 9 different sensors (X-IMU’s by X-IO Technologies) which are sent to the MEGA via Rx1, Rx2 and Rx3 available on the MEGA and is printed on the serial monitor via Tx0. However, the problem is that, when seen on the serial monitor the data is erroneous at certain times and I’m guessing it is due to a lot of data loss.

All sensors send the data in floating point values.

Is there a new improved version of the hardware serial that I can make use of so as to improve efficiency and in-turn reduce data loss?

The circuit is as follows:

Sensor1 ----->

Sensor2 -----> MEGA 1------------------|

Sensor3 -----> |


Sensor4 -----> |/

Sensor5 -----> MEGA 2 -------> MEGA 4

Sensor6 -----> /|\


Sensor7 -----> |

Sensor8 -----> MEGA 3 -----------------|

Sensor9 ----->

I have also attached the program that I am using with this post. Please help!


megatomultiwii.ino (680 Bytes)

The Serial.readStringUntil() function waits for incoming data with a timeout of 1 second.

While the function waits until the data of one serial port is received, the other serial ports are also receiving data.

I think you have no choice, but receive everything as fast as possible and put it in a buffer.

The Serial.readStringUntil reads the data into a String object. That makes it slow. Using an array is faster. That requires some coding, filling an array, checking for '\n', not writing beyond the array, and so on.

  if(Serial1.available() > 0)
     // read a byte and put it in a String object or character array buffer
     ... =;
  if(Serial2.available() > 0)
     ... =;
  if(Serial3.available() > 0)
     ... =;

  // Check here if a complete string has been received.

I think I read this same question a few days ago, but I can't find it.

Look at the examples in serial input basics. They receive data without blocking.

HardwareSerial works fine.

How much data are you trying to collect (total bytes per second) and what baud rates are you using?