Serial.readBytes

Hello, This is krishna, I am trying to read data from the serial port, for that i am using serial.readBytes(), this is serial.readBytes() function is blocking until it reaches the count specified in the second argument. So i want terminate this Serial1.readBytes() function before it reaches the count, for that i am using Serial1.setTimeout() function but i am unable to terminate serial.readBytes() function.

void setup() {

 Serial1.begin(9600);
  Serial1.println("Serial Test");
  
}
char buf[200];
unsigned int i = 0;
void loop() {
  Serial1.setTimeout(10);
  int len = Serial1.readBytes(buf,136);
  
 if(len > 0)
 {
  Serial1.print("len");
  Serial1.println(len);
  Serial1.println(buf);
   Serial1.flush();
 }
}

This is the code i used. Please tell me how use Serial.setTimeout() function to terminate the Serial1.readBytes() ?

Thanks, regards, krishna.

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

...R

Hi robin, First of all thank you very much for reply,

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

i just want to know how to break Serial.readBytes() function using Serial.setTimeout().

Thank you, regards, krishna.

serial.timeout reference

What makes you think the timeout is not happening? You don't have any way to see that. Try adding some output:

 if(len > 0)
 {
  Serial1.print("len");
  Serial1.println(len);
  Serial1.println(buf);
   Serial1.flush();
 } else {
Serial1.println("TIMEOUT!");
 }

You should also set Serial Monitor to "No line termination" so it sends characters out as you type them rather than waiting until you hit Enter.

johnwasser: You should also set Serial Monitor to "No line termination" so it sends characters out as you type them rather than waiting until you hit Enter.

That does not work, one still has to press enter to send.

Hi johnwasser, Thank you very much for reply,

What makes you think the timeout is not happening? You don't have any way to see that. Try adding some output

Yes john, I tried this code, after that olny i came to know that Serial.readBytes() function was blocking.

Is there any other way to terminate Serial.readBytes() function?

Thanks, regards, Krishna.

Is there any other way to terminate Serial.readBytes() function?

Yes. Don't use it. Pay attention to what Robin2 posted. It is the proper way to read serial data AS IT ARRIVES. Quit using crutches.

Hi PaulS,

Thank you very much for reply,

Yes. Don't use it. Pay attention to what Robin2 posted. It is the proper way to read serial data AS IT ARRIVES. Quit using crutches

As per my requirement i have to read more then 200 bytes at a time, this data doesn't have any starting byte and ending bytes, and the size of the data will vary.

By using the examples posted by Robin2 can read only 64 bytes. If i have 100 bytes on my serial port i can read only 63 bytes of data remaing 37 byte of data will be lost. To avoid this problem i am using Serial.readBytes() function and reading 200 bytes at a time. if i have 100 bytes of data Serial.readBytes() is blocking until it reaches 200 bytes. So i am decided to break or terminate Serial.readBytes() function using Serial.setTimeout(); function . I used the above code to break Serial.readBytes(). But it is not working.

Please tell me how break the Serial.readBytes() or how can i read huge amount of data at a time?

Thank you very much,

regards, Krishna.

So, increase the buffer size in Robin's code; can't be that difficult :) But that will not solve the problem as Robin's code does not handle timeouts (as far as I can remember).

The below demo will do what you want. Note that it's a demo and only uses Serial. Adjust to your needs.

// timeout time
#define RX_TO 100UL

// serial input buffer
char rxBuffer[200];

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  // index in receive bufffer
  static int index = 0;
  // time when last character was received
  static unsigned long lastRxTime = 0;

  if (Serial.available())
  {
    // set the last time that a character was received
    lastRxTime = millis();
    // if the buffer is full
    if (index >= sizeof(rxBuffer))
    {
      Serial.println("buffer overflow; data discarded");
      index = 0;
      lastRxTime = 0;
    }
    else
    {
      rxBuffer[index++] = Serial.read();
    }
  }

  if (lastRxTime != 0 && millis() - lastRxTime >= 100)
  {
    Serial.print("timeout, received ");
    Serial.print(index); Serial.println(" characters");
    index = 0;
    lastRxTime = 0;
  }
}

krishnaAmp: By using the examples posted by Robin2 can read only 64 bytes.

As posted my examples use a 32 byte buffer but that can be extended to any size you want by changing the line

const byte numChars = 32;

As @sterretje has provided a version with a timeout I won't re-invent the wheel.

I think I would use a between-character timeout rather than a whole-message timeout as it would allow for a much shorter wait in the case of a short message. In general if something sends a message all the characters will be sent at the baud-rate without any unexpected intervals so if the gap between characters exceeds X it will be clear that the end has been received.

...R

hi, i am trying to serially communicate between two arduino ex- one should send character(hello) and receive (cool) and other should send (cool) and receive(hello)

in serial monitor i am expecting the output as arduino 1(hello cool) & arduino 2(cool hello)

i am getting helloc helloo helloo hellol as my output can some one help me to program

thankyou,

@divyakumar, show your code.

Hi, What model Arduino are you using?

Thanks.. Tom.. :)