0

I got the following data as shown in the figure from imu razor which is connected to Arduino Mega 2560 in the serial monitoring and I want to get only the data that starts with "#A-R= "

Can any one provide me with code to do this?

enter image description here

This is my code:

#define BUFF_SIZE 40
char buffer[BUFF_SIZE];
String header ;
char Bufferheader[4] ;

void setup(){
Serial.begin(57600);
Serial2.begin(57600);
Serial2.write("#osct");
}

void loop() {

int i = 0;
while(Serial2.available()) {
buffer[i++] = Serial2.read();
if(i == BUFF_SIZE) break;
}

for(int j = 0; j < i; j++) {
Serial.print(buffer[j]);

}
}
2
  • Change your code so that it only prints the things you want it to print? Commented Jun 27, 2022 at 19:27
  • Or stop the serial monitor and type grep '#A-R=' /dev/ttyACM0 Commented Jun 27, 2022 at 19:49

2 Answers 2

2

The method outlined by Majenko is the general way of dealing with this kind of requirement, and also probably the “cleanest” way. If, however, you are confident that the incoming data always follows the pattern you are showing, then you can “cheat” and cut some corners.

Consider this: in the sample you show, the character 'A' only ever appears as the second character in a line, and always as part of the sequence "#A-R=". If you are confident that this is always the case, then you can apply the following algorithm:

  • the program processes one input character at a time, and decides whether to print it to its output or not
  • it has two possible states: either it is printing every character it reads (because it believes the character belongs to a “#A-R” line), or it is not
  • if it is printing and it reads an end of line, it prints that end of line, and then stops printing
  • if it is not printing and it reads an 'A', it outputs "#A", then starts printing all its input.

In code:

void loop() {
    static bool printing = false;  // are we printing what we read?
    if (Serial2.available()) {
        char c = Serial2.read();
        if (printing) {
            Serial.write(c);
            if (c == '\n')  // on end of line, stop printing
                printing = false;
        } else if (c == 'A') {  // on 'A', start printing
            Serial.print("#A");
            printing = true;
        }
    }
}

This code assumes your device uses ASCII LF ('\n') as end-of-line indicator. You will have to replace '\n' by '\r' if it uses ASCII CR instead.

1

Your code is doing literally nothing except reading from one serial port and passing it to another.

You need to throw away pretty much all that code and do some actual processing of the data.

You need to:

  1. Read the incoming data a line at a time (that is, read everything up to the line ending into a buffer)
  2. Examine the start of that buffer
  3. Output whatever data from the buffer your examinations deem appropriate.

I can't go into detail here about how to do it, but there are tutorials covering all those topics available on the web. For the serial reading I have a tutorial that you may find helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.