user85692: 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?

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]);

}
}


user4143: 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:

Read the incoming data a line at a time (that is, read everything up to the line ending into a buffer)
Examine the start of that buffer
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.

user7508: 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.

85692: <p>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 &quot;#A-R= &quot;</p>
<p>Can any one provide me with code to do this?</p>
<p><a href="https://i.stack.imgur.com/iuE4E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iuE4E.png" alt="enter image description here" /></a></p>
<p>This is my code:</p>
<pre><code>#define BUFF_SIZE 40
char buffer[BUFF_SIZE];
String header ;
char Bufferheader[4] ;

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

void loop() {

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

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

}
}
</code></pre>
 4143: <p>Your code is doing literally nothing except reading from one serial port and passing it to another.</p>
<p>You need to throw away pretty much all that code and do some actual processing of the data.</p>
<p>You need to:</p>
<ol>
<li>Read the incoming data a line at a time (that is, read everything up to the line ending into a buffer)</li>
<li>Examine the start of that buffer</li>
<li>Output whatever data from the buffer your examinations deem appropriate.</li>
</ol>
<p>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 href="https://majenko.co.uk/blog/our-blog-1/reading-serial-on-the-arduino-27" rel="nofollow noreferrer">a tutorial</a> that you may find helpful.</p>
 7508: <p>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.</p>
<p>Consider this: in the sample you show, the character <code>'A'</code> only ever
appears as the second character in a line, and always as part of the
sequence <code>&quot;#A-R=&quot;</code>. If you are confident that this is always the case,
then you can apply the following algorithm:</p>
<ul>
<li>the program processes one input character at a time, and decides
whether to print it to its output or not</li>
<li>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</li>
<li>if it is printing and it reads an end of line, it prints that end of
line, and then stops printing</li>
<li>if it is not printing and it reads an <code>'A'</code>, it outputs <code>&quot;#A&quot;</code>, then
starts printing all its input.</li>
</ul>
<p>In code:</p>
<pre class="lang-cpp prettyprint-override"><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(&quot;#A&quot;);
            printing = true;
        }
    }
}
</code></pre>
<p>This code assumes your device uses ASCII LF (<code>'\n'</code>) as end-of-line
indicator. You will have to replace <code>'\n'</code> by <code>'\r'</code> if it uses ASCII CR
instead.</p>
