10 years, 7 months ago.

Redirect stdout via MODSERIAL - is this possible?

Hi Andy, Great work on this, I use it!

For simple debugging needs, basic construction like this works:

#define DBG(x, ...)  std::printf("[DBG FILE%4d] "x"\r\n", __LINE__, ##__VA_ARGS__);

Unfortunately, when I have applied MODSERIAL to the USB uart, then these DBG(...) macros don't get routed through MODSERIAL and preempt the buffered stream.

Is there an unobtrusive(1) way to route them through the MODSERIAL service?
(1) modifying all the files and other peoples libraries fails the "unobtrusive" check...

Would the same hold true for STDERR?
thanks, David Smart

Question relating to:

That is actually a very good idea, wonder why no-one else thought of that before :P

I *think* it shouldn't be too much of an issue, will check it probably later today. And yeah I am not Andy, so you will need to use someone elses library. (I have a MODSERIAL copy which added KL25 functionality, only I hope you don't need DMA since DMA stuff is removed from it, but besides that it should be identical).

posted by Erik - 11 Oct 2013

1 Answer

10 years, 7 months ago.

So worked a bit on it, made a stupid mistake which cost me way too much time (didn't realize I had to add '\' in front of the instance name when redirecting stdout). But I think it works now:

Import libraryMODSERIAL

MODSERIAL with support for more devices

In case you really want to use the original MODSERIAL: just check the claim function how to redirect stdout. In the library I set stdout as default argument, but it works the same for stderr.

Whatever you do, it is important you give a name to the MODSERIAL object (for example: pc(USBTX, USBRX, "MOD");). Otherwise redirecting doesn't work.

Accepted Answer

Thanks Erik, How could I have imagined I'd catch someone in the know that fast - that was also able to apply the change. My first test - 2 minutes to import, build, test, and it looks like it works great! [With the hints above, it was clear. With the built-in documentation, the next user might struggle a bit more. Perhaps an example around the claim would be good]

This is, more or less, the different printf paths in many code bases that use MODSERIAL:

#include "MODSERIAL.h"
...
MODSERIAL pc(USBTX, USBRX, "modser");

int main() {
    pc.claim()            // capture <stdout>
    pc.printf("goes to thru the MODSERIAL library\r\n");
    printf("so does this!\r\n");  // after the last one, not preempting it.
}
posted by David Smart 11 Oct 2013

Thanks, I updated the documentation with your (slightly modified) example :)

posted by Erik - 12 Oct 2013