How to get certain output from multiple outputs in the serial monitoring?

So, you have data coming in over a serial port and only want to keep the line that starts with #A-R=. Is that right?

Here is one way to do it. I wrote a test program to run on my Uno to send the data from a software serial port (thus the 38400 baud rate) and another program that goes on the Mega to receive the data via Serial1. It receive each line and then looks at the first 5 characters for the match and only prints the data with the matching 5 first characters. It may seem complex but it is the way that I would do it using methods from the serial input basics tutorial and the strstr() function.

Test code sender program on Uno (for your reference):

// forum test sender uno
#include <SoftwareSerial.h>

const char DATA[][32] =
{
   "#A-R=212.00,-29.00,-82.00",
   "#M-R=4.00,1.00,-2.00",
   "#G-R=-74.00,-23.00,32.00",
   "#A-R=211.00,-34.00,-83.00",
   "#M-R=-4.00,2.00,-1.00",
   "#G-R=-72.00,-26.00,38.00",
   "#A-R=188.00,-25.00,-65.00",
   "#M-R=-2.00,2.00,-2.00",
   "#G-R=-71.00,-42.00,37.00",
   "#A-R=212.00,-42.00,-93.00",
   "#M-R=3.00,-6.00,2.00",
   "#G-R=-73.00,-17.00,37.00",
};


const byte SS_RX = 2;
const byte SS_TX = 3;

SoftwareSerial ss(SS_RX, SS_TX);

void setup()
{
   Serial.begin(115200);
   ss.begin(38400);
}

void loop()
{
   static byte index = 0;
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      Serial.println(DATA[index]);
      ss.println(DATA[index]);
      index++;
      if(index >= 12)
      {
         index = 0;
      }
   }
}

Receiving and parsing program on a Mega:

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup()
{
    Serial.begin(115200);
    Serial1.begin(38400);
}

void loop()
{
   recvWithEndMarker();
   showNewData();
   if (newData)
   {
      parseData();
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial1.available() > 0 && newData == false)
   {
      rc = Serial1.read();
      if(rc == '\r') // ignore carruage return
      {
         return;
      }
      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("data from input ... ");
      Serial.println(receivedChars);
      //newData = false;
   }
}

void parseData()
{
   if(strstr(receivedChars, "#A-R=") != 0)  // does the new line have the correct header?
   {
      Serial.print("This is the data to keep >> ");
      Serial.println(receivedChars);
   }
   newData = false;
}

Sample of output:

data from input ... #M-R=3.00,-6.00,2.00
data from input ... #G-R=-73.00,-17.00,37.00
data from input ... #A-R=212.00,-29.00,-82.00
This is the data to keep >> #A-R=212.00,-29.00,-82.00
data from input ... #M-R=4.00,1.00,-2.00
data from input ... #G-R=-74.00,-23.00,32.00
data from input ... #A-R=211.00,-34.00,-83.00
This is the data to keep >> #A-R=211.00,-34.00,-83.00

1 Like