Call function when new data available on serialport

Hi everybody,

I am working on a project and I have a function to call when a new data available on serial port.

void loop() {

  if (Serial.available() > 5)
  {
    SpeedGPS = (double)Serial.parseFloat();
  }
  getser1();
  getser2();

  if (SpeedGPS > 0.1)
  {
    double _zgyro1=(double)zgyro1;
    double _zgyro2=(double)zgyro2;
    x = calculatesensor(_zgyro1, _zgyro2, SpeedGPS);
  }
  angvel = (float)x;
  Serial.println(angvel);




}


My problem is when there is no new data it process old data again. How can I prevent it to process old data.

Thanks

a common problem, code processes input regardless i there is any

move the code to process data under the condition when data is available

Put everything inside the

if (Serial.available() > 5)

block ?

then the problem becomes what happens with the 5 bytes being captured is out of sync (not contiguous

it would be better to receive a complete "line" of data, possibly using readBytesUntil() and then processing the complete line

I have one more question which I forgot to mention on first post. there is 3 serial port and getser1() and getser2() also receives data on serial port 1 and 2 of mega.

void getser1()
{
  if (Serial1.available() > 13)
  {
    for (int ii = 0; ii < 31; ii++)
    {
      msg1[ii] = 0x00;
    }
    inChar1 = Serial1.read();
    if (inChar1 == 0x68) // start of frame
    {
      inChar1 = Serial1.read(); // Data Length
      if (inChar1 == 0x0D)
      {
        msg1[0] = inChar1; // Data length
        inChar1 = Serial1.read(); // Address
        msg1[1] = inChar1; // Adress
        inChar1 = Serial1.read(); // Command means data
        if (inChar1 == 0x84)
        {
          msg1[2] = inChar1; // command
          msg1[3] = Serial1.read(); // Z Gyro 1
          msg1[4] = Serial1.read(); // Z Gyro 2
          msg1[5] = Serial1.read(); // Z Gyro 3
          msg1[6] = Serial1.read();// Y acc 1
          msg1[7] = Serial1.read();//Y acc 2
          msg1[8] = Serial1.read();//Y acc 3
          msg1[9] = Serial1.read();//heading byte 1
          msg1[10] = Serial1.read();//heading byte 2
          msg1[11] = Serial1.read();//heading byte 3
          msg1[12] = Serial1.read();//checksum


        }

      }
      Sumcrc1 = checksumcalc(msg1);
      if (msg1[12] == Sumcrc1)
      {
        sensor1 = convertingvalue(msg1[9], msg1[10], msg1[11]);
        zgyro1 = convertingvalue(msg1[3], msg1[4], msg1[5]);
      }
               else
      {

        Serial.println("sumcrc error1");
        delay(10000);

      }

    }

  }

}



void getser2()
{

  if (Serial2.available() > 13)
  {
    for (int ii = 0; ii < 31; ii++)
    {
      msg2[ii] = 0x00;
    }
    c2 = Serial2.read();
    if (c2 == 0x68) // start of frame
    {
      c2 = Serial2.read(); // Data Length
      if (c2 == 0x0D)
      {
        msg2[0] = c2; // Data length
        c2 = Serial2.read(); // Address
        msg2[1] = c2; // Adress
        c2 = Serial2.read(); // Command means data
        if (c2 == 0x84)
        {
          msg2[2] = c2; // command
          msg2[3] = Serial2.read(); //Z axis angular 1
          msg2[4] = Serial2.read(); // Z axis angular 2
          msg2[5] = Serial2.read(); // Z axis angular 3
          msg2[6] = Serial2.read();//Y Acc 1
          msg2[7] = Serial2.read();//Y Acc 2
          msg2[8] = Serial2.read();//Y Acc 3
          msg2[9] = Serial2.read();//heading byte 1
          msg2[10] = Serial2.read();//heading byte 2
          msg2[11] = Serial2.read();//heading byte 3
          msg2[12] = Serial2.read();//checksum


        }

      }
      Sumcrc2 = checksumcalc(msg2);
      if (msg2[12] == Sumcrc2)
      {
        sensor2 = convertingvalue(msg2[9], msg2[10], msg2[11]);
        zgyro2 = convertingvalue(msg2[3], msg2[4], msg2[5]);
      }
      else
      {
        Serial.println("sumcrc error2");
        delay(10000);

      }


    }

  }


}

There is no end for data so I can't use readBytesUntil.

Thanks

sure, not so uncommon. but the code you posted waited for 5 bytes, so it looks like your processing differently on different interfaces

so as you're code does, it scans for the start char(s), resetting the char index when it doesn't see it, then read the data length when the char index == 1 and then read up to the data length when char index > 2

very often, the checksum appended to a msg, is not necessarily the actual checksum, but a value that causes the calculated checksum to become zero which is easily recognized

and why is there a 10 sec delay at the very end?

We added the delay when thereis checksum error. Our device send missed data sometimes and we need to check data is ok or not. we just wrote 10s but we will decrease it to 0.5seconds.

i just thought that can i appent calculate function to getser1() or getser2() function. Speed change is not very important but gyro data is important.

If somebody should help checking how it can be done with the highest possible reliability
you have to explain in much more detail what is happening
and how things depend on each other
and post how do the messages look like

As far as I have understood it
you are receiving five bytes on Serial
these five bytes are a digits (0..9) with a decimal-point
example 123.8
is this example correct? or does the byte-sequence look different?

Only in case you have received a valid SpeedGPS-value
the data rushing in from Serial1 / Serial2 shall be processed
if there is no valid SpeedGPS-value drop the data from the Serial1-receive-bufferal2-receive-buffer
and wait for a valid SpeedGPS-value.

is this a correct description?
If this description is not correct. Post a correct discription in this grade of details
Also it would help to see how the byte-sequence looks like
and that you describe what in the bytesequence is always the same value which bytes can be different.

Another question:
at what time-intervals does the data come in on Serial?
at what time-intervals does the data come in on Serial1?
at what time-intervals does the data come in on Serial2?

You wrote

You are receiving three different "data" Which "data" = coming from which serial interface do you mean?

and which "data" is meant in your sentence

This "data" must be something different because if no data is received there is nothing that could be processed.
My conclusion the "old-data" is something different than "new data"

You should specifiy each kind of "data" in such a specific way that there is absolutely no room left for speculation.

best regards Stefan

I got speed data as 5 byte, yes it send 123.8 or 001.50

Actually, sensor 1 data is more important than Serial0 (SpeedGPS data).

Serial0 sends data at 10Hz, Serial 1 and Serial 2 send data at 25 Hz.

Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.