Does Serial.getc block forever ? How big is the buffer ?

05 Nov 2009 . Edited: 05 Nov 2009

I'm talking to a camera over a serial connection, and I seem to be losing bytes. Which raises two questions:

- Does getc() block forever when it is waiting for input ?

- While getc() is not getting called, does the mbed buffer incoming bytes on that port ? And if so, how big is the buffer ?

Thanks,

Richard

05 Nov 2009 . Edited: 05 Nov 2009

Hi Richard,

Richard Sewell wrote:
- Does getc() block forever when it is waiting for input ?
- While getc() is not getting called, does the mbed buffer incoming bytes on that port ? And if so, how big is the buffer ?

Yes, getc() blocks forever. You can use readable() to see if there is anything to read or not (and hence would block). e.g.

while(myserial.readable()) {
    char c = myserial.getc();
}

The UART hardware has a buffer of 16-bytes, so you will get some buffering, but any more than this and they'll be getting dropped.

We are just experimenting with some interrupt driven UART abstractions which would allow you to build things like a software buffered UART very easily if that is of interest.

Simon

05 Nov 2009

Simon - thanks.

I expect my lost bytes will turn out to be my own foolishness, but they are proving hard to track down. I'd certainly be interested to play with your buffered abstraction when you have it baked!

 

Richard

05 Nov 2009

Simon - another serial question for you - I'm trying to read from this camera, which ought to be belting out 512 byte chunks at 115200 baud.

I only ever get the first 16 bytes, which is suspiciously reminiscent of the hardware buffer size you mentioned. If I'm going round a simple getc loop, what baud rate should I be able to keep up with, do you think ?

 

Thanks,

Richard

06 Nov 2009

Oh - and another related question - if I am in my getc loop, waiting for my 500 bytes, but they aren't coming, I'd like to time out eventually.

Right now I do that by wrapping each getc in a loop that tests on readable(), and uses a timer to give up when it has been too long. It would be tidier to use a simple loop on getc and a Timeout to do the giving up.

 

But I don't know what to do in the Timeout interrupt method to cause the waiting getc() to give up waiting. Is there a way ?

 

Thanks,

Richard

06 Nov 2009 . Edited: 06 Nov 2009

Maybe this will be useful.

06 Nov 2009

Igor - thanks - I had already implemented a rather uglier version of that idea. But I am getting failures at any baud rate higher than 14400, which I think means I need to put less code in my read loop.

 

Richard