The data of the two sensors in serial monitor are messy

Change...

to...
if (strstr(buffer2, "#A-R=") == NULL)

That means if the timing between the 2 inputs is not the same... then you will have problems. Example... sensor 1 sends data every 2 seconds, sensor 2 sends data every 3 seconds.

Data from sensor 1 will fill up the serial buffer as it effectively waits for data from the other sensor.

1 Like

I got the same output as the previous

Post the code.

This problem will not be happened if the two sensors are exactly the same time, right?

Correct.

const byte BUFF_SIZE = 128;

char buffer1[BUFF_SIZE];
char buffer2[BUFF_SIZE];

boolean newData1 = false;
boolean newData2 = false;

uint8_t ndx1 = 0;
uint8_t ndx2 = 0;


void setup()
{
  Serial.begin(57600);

  Serial2.begin(57600);
  Serial2.write("#osrt");

  Serial3.begin(57600);
  Serial3.write("#osrt");
}

void loop()
{
  if (!newData1)               // Have we finished getting buffer1 data?
    recvWithEndMarker1();      // Keep getting data

  if (!newData2)               // Have we finished getting buffer2 data?
    recvWithEndMarker2();      // Keeping getting data

  if (newData1 && newData2)    // Have we received both sets of data.
  {
    parseData();               // Display the data & clear the buffers.
  }
}


void recvWithEndMarker1()
{
  char c;

  if (Serial2.available() > 0) // Is there data ready to be read?
  {
    c = Serial2.read();        // Get a character.

    if (c == '\r' )            // Carriage return?
    {}                         // Do nothing
    else if (c == '\n')        // End of record?
    {
      buffer1[ndx1] = '\0';    // Terminate the string
      ndx1 = 0;                // Reset the buffer index
      newData1 = true;         // Set the found flag
      return;                  // Finish reading
    }
    else
    {
      buffer1[ndx1++] = c;      // Add to the buffer
    }


    if (ndx1 > BUFF_SIZE)       // Have we exceeded the buffer length?
    {
      Serial.println("Buffer 1 overflow");
      ndx1--;
      buffer1[ndx1] = '\0';
    }
  }
}


void recvWithEndMarker2()
{
  char c;

  if (Serial3.available() > 0) // Is there data ready to be read?
  {
    c = Serial3.read();        // Get a character.

    if (c == '\r' )            // Carriage return?
    {}                         // Do nothing
    else if (c == '\n')        // End of record?
    {
      buffer2[ndx2] = '\0';    // Terminate the string
      ndx2 = 0;                // Reset the buffer index
      newData2 = true;         // Set the found flag
      return;                  // Finish reading
    }
    else
    {
      buffer2[ndx2++] = c;     // Add to the buffer
    }


    if (ndx2 > BUFF_SIZE)      // Have we exceeded the buffer length?
    {
      Serial.println("Buffer 2 overflow");
      ndx2--;
      buffer2[ndx2] = '\0';
    }
  }
}


void parseData()
{
  Serial.print("Sensor1:\t");
  Serial.print(buffer1);
  if (strstr(buffer1, "#A-R=") == NULL)
    Serial.println();
  else
    Serial.println(" - Invalid header");

  Serial.print("Sensor2:\t");
  Serial.print(buffer2);
  if (strstr(buffer2, "#A-R=") == NULL)
    Serial.println();
  else
    Serial.println(" - Invalid header");

  newData1 = false;
  newData2 = false;
}

sorry I mean This problem will not be happened if the two sensors are exactly the same type, right?

  if (strstr(buffer1, "#A-R=") == NULL)

Change to !NULL

It depends... how do they know when to send the data?

Still the same

The same firmware are installed on both sensors, will that make the two sensors send the data at the same time?

Sorry... I meant != NULL

Not neccessarily... what determines how often they send data?

It's working now, thanks

What usually determines this? I will mount the two sensors on a certain structure in different locations and I want to to take the readings at the same time. Will this affect?

@drmpf
I tried what did you recommend for one serial but I didnt get any data in the serial monitor,
Could you please tell me what is the wrong in this?

const byte BUFF_SIZE = 40;
char buffer[BUFF_SIZE]; // an array to store the received data
boolean newSerial1Data = false;



void initSerial1() {
  Serial2.begin(57600);
  Serial2.write("#osrt");
}

void processSerial1() {
  recvSerial1WithEndMarker();
  if (newSerial1Data) {
      parseSerial1Data();  // print output here when data parsed
  }
}




void setup() {
 // other setup stuff here
 initSerial1();
 //initSerial2();
}

void loop() {
  processSerial1();
}


void recvSerial1WithEndMarker()
{
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial2.available() > 0 && newSerial1Data == false)
{
rc = Serial2.read();
if(rc == '\r') // ignore carruage return
{
return;
}
if (rc != endMarker)
{
buffer[ndx] = rc;
ndx++;
if (ndx >= BUFF_SIZE)
{
ndx = BUFF_SIZE - 1;
}
}
else
{
buffer[ndx] = '\0'; // terminate the string
ndx = 0;
newSerial1Data = true;
}
}
}
void showNewData()
{
if (newSerial1Data == true)
{
Serial.print("data from input ... ");
Serial.println(buffer);

}
}
void parseSerial1Data()
{
if(strstr(buffer, "#A-R=") != 0) // does the new line have the correct header?
{
Serial.println(buffer);
}
newSerial1Data = false;
}

WHY? WHY? WHY?

You had it working in the previous post based on the code I gave you ??

Regarding the issue of timing...

Based on the documentation found here...

You need to turn off continuous mode by sending this command...

"#o0"

Then you can request a single record with this command...

"#f"

If you do that you can control the timing of messages from the Arduino.

2 Likes

Yes, Its working very well.
But I was afraid of the point that you said about the timings that's why I was trying other solutions.

but now when you recommend this:

I will try it

Thanks much

@red_car
I tried this

void setup()
{
  Serial.begin(57600);

  Serial2.begin(57600);
  Serial2.write("#o0");
  Serial2.write("#osrt");

  Serial3.begin(57600);
  Serial3.write("#o0");
  Serial3.write("#osrt");
}

Then I requested "#f" in the serial monitor but I didnt any data in the serial monitor.

How did you do that?

Try adding in setup... after the commands above. You should get one set of data returned.

Did you mean as this?

void setup()
{
  Serial.begin(57600);

  Serial2.begin(57600);
  Serial2.write("#o0");
  Serial2.write("#osrt");

  Serial3.begin(57600);
  Serial3.write("#o0");
  Serial3.write("#osrt");
  
  Serial2.write("#f");
  Serial3.write("#f");
  
}

I got only two readings in the serial monitor:

Sensor1: #A-R=163.00,-53.00,-122.00
Sensor2: #A-R=-2.00,177.00,153.00

and that is exactly what you should expect. You now have control of the sensors... when you send another "#f" you will get another reading.

In loop, you can now request a reading whenever you want one... then read them... then request another one.

I'll let you figure out that code for yourself...

1 Like