11 years, 10 months ago.

Printf statement appears to be non-func. for outputing data to a UART when located within a for-loop

I am trying to program the mbed to output data parameter values via UART interface to a data logger card. For some reason, that I don't understand, using a printf statement within a for-loop does not output this data to the UART IF. The printf works fine not in a loop and even works in a while-loop. Tried this operation using UART2 or UART3. I found this same operation "abnormality" in trying to transfer data via a printf statement within a for-loop over USB to a terminal in my PC. I can certainly live with this restriction but I would like to understand what might be causing it.

Question relating to:

2 Answers

11 years ago.

You can try using function: writeable()

Serial PC

// Print "Hello World" to the PC

#include "mbed.h"

Serial pc(USBTX,USBRX);

int main() {
    
    while(!pc.writeable());
    pc.printf("\n\rHello World!!");
    while(1)
    {
        if(pc.readable())
            pc.putc(pc.getc());
    }
}

Yes, I found that a while-loop would work. I was just curious if there was a specific reason that the printf statement was not implemented in the for-loop or was it just left out since other options could be used to accomplish the same results. Thanks for your response.

posted by James Curts 11 Dec 2013
11 years ago.

There should not be any problem with printf in a for-loop. You may be sending data too fast and somehow loose characters. There could also be other issues related to interrupts (printf should not be used in interrupt handlers). Please post smallest code example that shows your problem.