How to check a data received from a Serial1 port?

Hello there,
i wanted to know, if i am receiving a data from Serial1 port and wants to check it, so which command i need to use to check a received data ?
My requirement is, if first character is N and second is O and 3rd is P and last is E, only then it prints over the serial monitor, otherwise not?

Starting of code would be:

if Serial1.available()
{
char a=Serial1.read();

What to write thereafter??
Thank you

Here's one approach, I don't know if it would work.

int index=0;
void loop()
{
if(Serial1.available())
  {
   if(Serial1.read() == "NOPE"[index])
     {
     index++;
     if(index>3)
        {
        Serial.println("yep we got NOPE");
        index=0;
        }
       }
   else
     index=0;
  }
}

This Thread serial input basics may be useful

...R

@kenF
Yes it is working thank you :slight_smile:
I wish you can help me with one more thing ;
I am sending a data from a Serial monitor to serial1 port but the problem is , some random characters are also interfering in my data (noise) which i dun want to include ! so i was thinking to have some fixed characters say "NOPE" and after which any data will be sent to Serial1 and other data will be discarded. that means i want to use "NOPE" characters as my starting characters for recognizing pattern?
can you help me out with dis?

How long is the data that follows "NOPE" ?
AND
Are you going to have anything to act as a terminator for the characters you want to forward?

@kenF
Data can be as long as possible say about 15 to 20 characters after NOPE.
and if you find it necessary to include a terminator then guide me with it too! thank you :slight_smile:

@KenF
data after NOPE should be random.

Well the fact that you want to send "NOPE" first to indicate that the data is valid, kind of implies that there are occaisions when data will be sent that is invalid.

So you should really send some kind of marker in the stream to note the end of the data. The end marker would obviously have to be something that is not normally part of the data you are sending.

Could I ask what kind of data you're sending, or better still, what is going to recieve the data you're sending out?

@KenF
i have connected two laptops and i want both the laptops to communicate using Serial Monitor. the data sent from one serial monitor should display over the other serial monitor.
What i have done is, data sent from one serial monitor is transferred to serial1 port which further transfers it to serial1 port of the other connected laptop and it then finally displays the data over serial monitor of other laptop.
i have used power line communication module which includes a lot of noise in it and is allowing random characters to be displayed over serial monitor.
so therefore, i want only the typed data from one Serial monitor should display over the another laptop's serial monitor and rest of the data should be discarded..
Coding i have used is given.

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  }

void loop()
{
  if(Serial1.available())
  {
    char a=Serial1.read();
     
        {
Serial.print(a);}
  }
  }
  if(Serial.available())
  {
    char a=Serial.read();
    Serial1.print(a);
  }
}

now what need to be added further to include only that data which we are typing and noise data should be discarded?

If you're getting noise on the serial line I'd suggest you get to the bottom of what's causing that. Serial communication should be pretty noise free.

Could you show a schematic of what you have.

@KenF
i mean isn't there any way that i can receive only the specific data that i want?
There should be one or two characters fixed, working as start bit(s) for starting communication and if necessary there should be a terminator too.
like (starting characters)text(ending character) only such data is allowed to be transferred otherwise not! simple?
any way of achieving it?

like (starting characters)text(ending character) only such data is allowed to be transferred otherwise not! simple? any way of achieving it?

There are links to serial code using < for data start and > for data end posted quite often on the forum. The problem is that if you have noise on your serial line, the data packet can still be corrupted. You need to eliminate the noise.

noise as such alphabets and numeric
not such any other noise.
like i am trying to send "hello"
i am receiving (khbgkj123"hello"hajakak45663) instead of just hello.
so help e eliminating the other characters and numeric.

The below is somebody elses code that uses data start and stop delimiters.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

@zoomkat
thanks alot!
working perfectly and is the exact thing i was asking for.. :slight_smile: