Go Down

Topic: Serial data at 115200. Read in one line. Act on it, then Read in another  (Read 1 time) previous topic - next topic

q2pilot

Hi, I'm reading serial data - example "00034060-004+00102610000-0002+005+01+1099004FF71084\n" at 115200 baud.  This data is always the same for a long time.  To output the serial data I used a Nano to just print that line over and over, then hooked the Nano Tx to my Uno Rx where I'm doing my programming.  I found I needed a twisted pair when working at this speed.

I can read it in and parse it but if I do too much programming then the data turns to garbage.  I don't want to read every line of data coming in.  I just want one line, then to be able to spend some time doing something with that data, then go back and read another line.

I used a delay(100) to test it out.  If I put the delay in, my data printed turns to garbage.  Don't say use 9600 baud, this is not an option for me.

I've searched everywhere for a way to skip some incoming messages while I do some work but haven't found anything!  Help this Arduino nube!  Thanks,  Mike

ieee488

Read How to Use This Forum and specifically how to post code.


.

sterretje

You can sync on the \n. And if the message is always the same size, you can count characters and character N should be the \n; if not, ignore what you received.

PS
Why is this in "interfacing with software on the computer"?
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Robin2

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

q2pilot

I figured it out.  The problem was that data was flying in so fast at 115200 that it left very little time to do something with that data.  My data is a stream of 55 characters that mostly did not change so I used a

for (int i = 0; i<= 1024; i++)  {    //this skips 1024 bytes of serial data so you can take some time to do something else.
   if (i = 100)  {
    recvWithEndMarker();
    showNewData();
   }
  }

to skip a bunch of the incoming data so I had time to work on the data.  This should prove useful on GPS serial data but that's typically only 9600 baud. 

my whole code: 
// Example 2 - Receive with an end-marker
// modified to let me skip reading 1024 bytes
// so I have time to do something before the next serial read.

const byte numChars = 60;       // reads in 60 bytes - my string length is 55
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    Serial.begin(115200);
    Serial.println("<Arduino is ready>");
}

void loop() {
  for (int i = 0; i<= 1024; i++)  {    //this skips 1024 bytes of serial data so you can take some time to do something else.
   if (i = 100)  {
    recvWithEndMarker();
    showNewData();
   }
  }
    delay(1000);   // huge delay does not effect my serial data now.

   
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
     
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        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("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
        Serial.flush();
    }
}

Robin2

I can't see any value in these lines of code
Code: [Select]
  for (int i = 0; i<= 1024; i++)  {    //this skips 1024 bytes of serial data so you can take some time to do something else.
   if (i = 100)  {


At best they act as a short delay(). They certainly don't skip any particular number of bytes of serial data.

I suspect all you need to do is hold off the line
Code: [Select]
newData = false;
until you are ready to receive more data.

Separately this line is useless where it is
Code: [Select]
Serial.flush();
That command is for outgoing data - it causes the Arduino to wait until the Serial Output Buffer is empty.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up