How do you know when a serial port is NOT getting data?

I am reading 6 encoders from motors using 2 serial ports 1, and 5. (Teensy)

The serial goes to a RS 485 convertor that is not duplex, So I got to wait 2 sec after each inquiry before I quire the next set of encoders.

So I got a function like Read_Enc (1); // Serial 1 Read_Enc (11); //serial 5 delay(2); Read_Enc (2); Read_Enc (12); delay(2); Read_Enc (3); Read_Enc (13); delay(2); Show_Encs(); // function to print out all values I got

I hate the delay, is messing my other readings , So I would like to send the next query when the response was received back.

So I tried

Read_Enc (1); Read_Enc (11); if (!Serial1 && !Serial5 ) // this means wait till the port is available to send the next quiery TX??? { Read_Enc (2); Read_Enc (12); }

I only get the first set of readings...

So how do I know when the message is over?

Thanks a lot,

Mitch

If data arrives the value returned by Serial.available() will increase. If no data arrives it will not change.

You could do something like this non-blocking pseudo code

if (requestSentTime == 0) {
   Serial.println("please send something");
   requestSentTime = millis();
}
if (millis() - requestSentTime > waitInterval) {
  if (Serial.available() > 0) {
     // processMessage
  }
  requestSentTime = 0; // move on
}

...R

Nice Idea Robin

I implemented it , but the problem is that all these encoder readings are being in a function that I call sometimes. It is not in the loop. Reading 6 encoders every 20mS is intensive and I don't need it all the time.

So I got the function

bool Read_All_Enc(void *)   // this is for repeat reading
{ 
  Read_Enc (1);
  Read_Enc (11);
  

  if (millis() - SentTime > 3)
   {
    Read_Enc (2);
    Read_Enc (12);
    Serial.println ("SecondBatch");
  }



  Show_Encs();

  return true;

}

The part after the IF does not execute since the function does not "wait".

What to do ?

Thanks

laptophead: Reading 6 encoders every 20mS is intensive and I don't need it all the time.

I don't call that intensive at all. 20ms is 320,000 machine cycles on a 16MHz Uno.

Finally I used your idea, a time stamp for each query and 5 ms later I go to the next query

I used an incrementor to move to the next set of encoders. Seems stable and fast, I am on a teensy 3.6

Here is the sequence , maybe others need it.

 if (millis() - SentTime > 5  and Mode == 0)  // reading all encoders every 5 ms
   {
    if (EncPair_Incr==1)
    {Read_Enc (1);
    Read_Enc (11);
    }
    
    else if (EncPair_Incr==2)
    {Read_Enc (2);
    Read_Enc (12);
    }

    else if (EncPair_Incr==3)
    {Read_Enc (3);
    Read_Enc (13);
    Show_Encs();
    
    }
    
  }

Thanks a lot, the time stamp was a great idea

laptophead: Finally I used your idea, a time stamp for each query and 5 ms later I go to the next query

I believe that's what I suggested.

...R

If you just want to know if data is available, but don’t care how many just call read() or peek() (depending if you want to grab that byte or not) and It will return -1 if nothing is there.

The advantage versus using available() is that it’s faster.

Indeed to count the number of bytes available there is a subtraction between the head and tail pointers used for the serial circular buffer and a costly modulo opération versus just a compare between those 2 pointers for read or peek