Go Down

Topic: how to extract a desired data from running set of string? (Read 183 times) previous topic - next topic

say for example there is a continuous stream of data
"hagsbdgsjana"HELLO"jahshssksj
and i want to extract HELLO from the desired stream of bits running.
any code to achieve so?

KenF


@KenF
yes it worked! but it rarely can't extract the exact data yet, like 9/10 times it worked fine but 10th time it was not extracting the exact required data.
so i was asking if there is any other code too to extract out the desired data?

KenF

If the quality of your data is that bad, NOTHING is going to sort it out.  You need to work out what's creating all the rubbish on the line.

@KenF
is there any way that when my desired data is inside any delimiter , say "<< DATA >>" , only that data should appear over Serial monitor, otherwise discard it?
The previous program is using single character delimiter, such as '<DATA>' !!
If there would be two fixed starting and two fixed ending characters then it would be even more efficient. So kindly help me out .
Quote
"<<DATA>>" instead of '<DATA>'

zoomkat

Quote
If there would be two fixed starting and two fixed ending characters then it would be even more efficient. So kindly help me out
You might add a sub conditional to check for the second delimiter after the first check for the delimiter.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

nilton61

There are two approaches that work.
  • Acceptor state machines
  • JSP diagrams


@zoomkat
how subconditional works?
Also do tell that whether the below given code is true to check first '<' then '*' and then allows data up to it encounters an endmarker which is '>' in this case?
i.e. only <*DATA> will be transferred over serial1 and rest of the data will be discarded?
Code: [Select]
#define SOP '<'
#define MOP '*'
#define EOP '>'
bool hope = false;
bool started = false;
bool ended = false;

char inData[80];
byte index;

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

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial1.available() > 0)
  {
    char inChar = Serial1.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
       hope = false;
    }
    else if(inChar == MOP)
    { 
       //index=0;
       inData[index] = '\0';
       hope = 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 && hope && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    Serial.println(inData);
    started = false;
    ended = false;
    hope = false;
    index = 0;
    inData[index] = '\0';
  }
if(Serial.available())
  {
   char a=Serial.read();
   Serial1.print(a);
  }
}
   

KenF

You  could introduce a checksum to ensure that the other chatter is not getting into your string, this would allow you to ignore the strings that are getting corrupted.

A better approach would be to find the source of the other chatter and turn it off while you're sending your own data. 

Robin2

@surajveer37, the 3rd example code in the Thread I linked to in Reply #7 is specifically designed to accept characters between start and end markers. It uses < and > but you could easily change them.

You could easily modify it to look for pairs of markers - but I can't see how that would make the system any more reliable.

...R


zoomkat

The properties of the character stream need to be explained better. Do you have control of the character stream being sent? Do you have the ability to control the character/bytes that will be used as the data delimiters? Is it possible that the character delimiters you are currently using are also present in the character stream being received (ie., will random < and > also be in the character stream)? 
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy