MBed 6 Serial Port Stream Adapter

Is it just me, or does it break a lot of stuff that MBed 6’s serial port classes (BufferedSerial and UnbufferedSerial) no longer implement the Stream interface? Don’t get me wrong, I love that MODSERIAL-like buffering functionality is now integrated into the main OS. However, I have a huge amount of existing code that passes around multiple serial ports as Stream objects and writes to them with printf(), and this no longer works properly.

So, I wrote a quick little adapter class that lets you use a serial port as a Stream: https://os.mbed.com/users/MultipleMonomials/code/SerialStream/ . If anyone else out there has this same problem as me, hopefully this will be helpful.

If any MBed developers are reading this, would it be possible to get something like this integrated into the OS itself? Ideally the serial classes could implement Stream again, but I can understand why this likely isn’t going to happen since Stream adds some overhead due to its use of C file handles. Alternately, you could just add a wrapper class or subclass like this one. It’s really tiny, like 20 lines of code, and it restores a lot of compatibility that was broken in this update.

1 Like

another way to use BufferedSerial is fprintf((FILE*)&serial, "hello");
This is not exactly compatible, but a file handle could be passed and redirection to another device is also possible.

HI Jamie,

do you know that in Mbed OS 6 printf is removed in favour of minimal_printf ? I’m not sure if this may be part of why you are seeing breakages. Also if you have suggestions for improvements then please feel free to raise a pull request against Mbed OS to get technical feedback on your suggestions and whether or not they could be included in a further release…


Yes, I have noticed the printf() change. The code I work with needs access to full-featured float formatting, so I had to change it back, and this was not exactly easy to do. I finally figured out that I had to add "target.printf_lib": "std" to my mbed_app.json, but this is really not documented well anywhere that I could see, especially since target.printf_lib seems to be missing from the regular mbed compile --config help message. I only got the target.printf_lib setting from the MBed 6 release notes, and I had to read through the MBed build tools source code to find out what to actually set the value to.

For what it’s worth, I really don’t think that making minimal printf the default in MBed 6 was a good move. While it is a pretty significant space savings, 10kB is still only a few percent of the total flash available on most processors, so it’s unlikely to make a difference in the majority of apps that aren’t using all the available space. And having a printf() that isn’t even close to C99 standard compliant could be a real stumbling block for new users, who won’t have heard about this whole minimal printf thing and are just trying to figure out why they can’t print floats. Minimal printf is a great option to have for advanced users who are using close to the full capacity of their chip and know that they don’t need full printf features, but forcing it on everyone, especially since it’s hard to disable, seems like an unwise plan.

you’ll find documentation here:

This is also linked from the documentation site, but I don’t like this hidden menu system, you do not see that there are subitems because no ‘+’ marker is shown.


But I’m missing also the padding stuff, it should not be difficult to add. Handling the padding outside of printf makes it very inefficient and ugly to use.

Oh, I see, that’s where it’s documented. Thanks! However I still think that target.printf_lib should be included in the mbed CLI help message.