7 years ago.

device.readable() function is not working properly.

device.readable() function is not working properly.

hello all,

i am trying to receive length of whole packet (using serial receive interrupt) using following function. initially length is set to j=0.

void Rx_interrupt()                             // Interrupt Routine to read in data from serial port
      {                   
                                                // Loop just in case more than one character is in UART's receive FIFO buffer
                                                // Stop if buffer full
                    
                while(device.readable())          //if data is available on external UART
                     {   
                   //  pc.putc(device.getc());      //print it on pc
                     r[j]= device.getc();               //store in array.       
                     j++;                      
                     }
                     pc.putc(j);                                   //print length of received packet.

it is expected that till the whole packet is receive interrupt should remain in while(device.readable) loop. but actually it read single character & come out of while loop. & for eg.if packet is consist of 20 character then it print 1 2 3 ...20 instead of only single 20. but if you see same case in Arduino platform in which they uses

void serialEvent()
          {
              while(Serial.available())
                       {
                         r[j]= Serial.read();
                          j++;
                        }
                        length=j; 
                        serial.print(j);                 //print length of received packet.
                     //   j=0;                                  //re-initialize the length.

then value of j print is 20 single time.

what should i do to get result like these? thank you all.

Please use <<code>> and <</code>> when posting code so that the formatting is preserved otherwise the code becomes unreadable.

posted by Andy A 07 Mar 2017

1 Answer

7 years ago.

I'm assuming that half of your code is supposed to be commented out, you have two getc() calls in the loop which is just weird.

However going by your comments the code is doing exactly what it should do. It reads all of the waiting data in the buffer and then exits.

Serial ports are very slow, the processor can read a byte from the buffer and move on to something else in far less time than it takes the next byte to arrive. You should code to allow for multiple bytes in case the interrupt is delayed by another interrupt but under normal operation you'll only get one byte at a time in the interrupt.

Why don't you see this on the arduino? The only explanation I can think of is that arduinos are also very slow.

Accepted Answer

Assigned to Rahul Bhat 7 years ago.

This means that the question has been accepted and is being worked on.