I’ve been using the standard C I/O library functions to print and get characters (ie, using scanf & printf directly).
However, this results in the loss of some functionality - specifically, there is no (obvious) way to enable/disable whether reads are blocking or not, or identifying if a character has been typed. Normally, I would call BufferedSerial::set_blocking(false) to stop blocking, and BufferedSerial::readable() to identify if a character is available.
Are there alternatives to this functionality without passing around the original BufferedSerial object? This is especially difficult to do because I’m defining the global object in mbed_app.json, not in mbed_override_console.
Thanks for the help here! However, it seems like some functionality doesn’t work when I use the redirected serial.
For example, I am trying to flush the output buffers without entering a new line. When I explicitly use a BufferedSerial, calling BufferedSerial::sync() immediately flushes the buffer. However, when I use the redirected printf calls, even when I get the FileHandle for stdout, calling console_out->sync() does not actually flush the buffers.
Working code:
#include "mbed.h"
BufferedSerial serial(CONSOLE_TX, CONSOLE_RX, 115200);
int main()
{
char *pre_buf = "before sync";
char *post_buf = "after sync\r\n";
for (int i = 0; i < 10; ++i)
{
serial.write(pre_buf, 11);
serial.sync();
ThisThread::sleep_for(1s);
serial.write(post_buf, 12);
}
}
This prints out before syncbeforeafter sync, as expected.