I'm using gcc-arm-none-eabi via NXP MCUXpresso.
For device type FileHandles, c library buffering is turned off.
freopen()
is causing stdout
to become fully buffered, which is not what I want. I think it is the opposite of the intent stated above, too. Am I correct that this is unexpected behavior?
The following is my new experimentation that demonstrates this.
To simplify things a bit, when I print just a couple lines like this, without freopen()
, I can tell stdout
is line buffered.
#define SERIAL_BAUD_RATE (115200)
Serial s(USBTX, USBRX, SerialStreamName, SERIAL_BAUD_RATE);
int main()
{
for (size_t i = 0; i < 2; i++)
{
printf("%3u\r\n", i);
wait_ms(1000);
}
while(true) {}
}
But when I call freopen()
, it becomes fully-buffered, with a 1024 buffer, as demonstrated by the fact that the following code doesn't print. When I raise the loop bound by one (to 205), it prints everything except the last '\n'. (Five chars per line, 5 * 205 = 1025.)
#define SERIAL_BAUD_RATE (115200)
Serial s(USBTX, USBRX, SerialStreamName, SERIAL_BAUD_RATE);
int main()
{
freopen(SerialStreamPath, "w", stdout);
for (size_t i = 0; i < 204; i++)
{
printf("%3u\r\n", i);
}
while(true) {}
}
What is the recommended way to redirect stdout to a different serial port? Can it be redirected and maintain line-buffering behavior? Without supplying a new buffer?
Currently I'm doing it like this:
...but this changes it from line-buffered to fully-buffered.
Can I make it line-buffered? Without calling
setvbuf()
? I don't want to change the location or size of the buffer that it's already using; I just want it to behave as a line buffer, not a full buffer.