Serial out - printf - interesting behaviour.

30 Oct 2010

Hi all.

Ok, not that interesting but... (paraphrased code)

Serial pc(USBTX, USBRX); // tx, rx
main() {

  while(1) {

   wait(1)

   printf("I'm here");
   pc.printf("load1", load1*1);
 }

}

With the missing "pc" on the printf, I STILL get both put out to teraterm.. BUT.. odd order.

The load1 ones,,. they pop up every second as it runs, the other 5, they all turn up in one go, as if being written out of a cache somewhere?

...I'm hereI'm hereI'm hereI'm hereI'm hereI'm hereI'm hereI'm hereload1load1load1load1load1load1load1load1I'm hereI'm hereI'm hereI'm hereI'm hereI'm hereI'm hereI'm hereload1load1load1....

(ps, yes I can see I haven't put a %f in my printf, I'm off to do that now!)

30 Oct 2010

I think the difference here is that plain old "printf()" is the same as "fprintf(stdout, ...)" where as pc.printf() is saying "stuff these bytes straight out the uart attached to pc."

With the Mbed libs, stdout is attached to Uart0, but it's a "logical" attachment. Where as Serial pc(USBTX, USBRX)) is a direct attachment to Uart0.

So doing pc.printf(...) will send the stream straight out the Uart0, where as fprintf(stdout, ...) takes a different logical route to the same hardware.

30 Oct 2010

printf() is traditionally buffered so things can sit a while. If pc.printf() doesn't use the same buffering, things will get out of order.

there is fflush(stdout) in the usual libraries.