9 years, 8 months ago.

Logic behind the 'Serial' class implementation for 'getc' and 'putc'?

Hello -

I've been working with the mbed SDK 'Serial' class, and I am having trouble following the logic behind the use of the 'stdio' buffered streams, flushing, and non-buffered streams.

Specifically, why do 'Stream::putc' and 'puts' flush before writing?

Why do 'getc' and 'gets' flush before reading? (And my understanding is that fflushing on an input stream either does nothing, or is formally undefined - in the latter case, one could get different results with Keil's libc and newlib, ulibc, or others...)

Then, somewhat inconsistently, 'read' and 'write' use the unbuffered protected '_putc' and '_getc'... and there is no public 'flush' method, and the underlying FILE object is protected...

I would expect that 'setbuf(_file,nullptr)' would be all that is needed in the Stream constructor... assuming that there is logic here that I'm missing.

Is there something obvious that I'm missing? I'd really like to understand the logic behind the class. Thanks!

Question relating to:

Be the first to answer this question.