element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog C++ write your own stream class - part 2: a working Pico UART IO stream (embedded friendly C++)
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 15 Nov 2025 4:45 PM Date Created
  • Views 34 views
  • Likes 3 likes
  • Comments 3 comments
Related
Recommended
  • Modern C++
  • pico
  • pico_eurocard
  • c++

C++ write your own stream class - part 2: a working Pico UART IO stream (embedded friendly C++)

Jan Cumps
Jan Cumps
15 Nov 2025

In C++ it's common to stream data. You write to a file using the << operator. You read with >>.

example: 

cout << "hello, world! << endl;

In this blog I'm making my own minimal in, out and in-out stream class for Pico UART. 

I put embedded friendly in the title because this is a resource-friendly exercise. Can be used on the smallest controllers. 

How would your code look like?

You 'd stream data to UART with <<

uartiostream u;
    u << "hello element14!\n" ;

and stream from UART with >>

    std::string s;
    u >> s;

An example that first streams a constant string. Then echos everything you type in a serial monitor:

#include <string>

int main() {
    // ... usual Pico UART enable

    // c++ stream example
    uartiostream u;
    u << "hello element14!\n" ;  

    while (true) {
        std::string s;
        u >> s;
        u << s;
    }
}

Pico UART Stream classes: in, out and io

image

The code for the stream classes is surprisingly simple. 

class uartostream {
public:
    uartostream() {}

    uartostream& operator << (const char* msg) {
        uart_puts(UART_ID, msg);
        return *this;
    }

    uartostream& operator << (const std::string& msg) {
        uart_puts(UART_ID, msg.c_str());
        return *this;
    }
};

class uartistream {
public:
    uartistream() {}
  
    uartistream& operator >> (std::string& msg) { // blocking
        char c  = 0;
        do { 
            c = uart_getc(UART_ID);
            if (c != 255) {
                msg += c;
            }
        } while (c != '\n');
        return *this;
    }
};

class uartiostream: public uartistream, public uartostream {
};

These classes are small in code size. And small in runtime resource use. Using them is similar for the Pico as calling the API uart_puts() and uart_getc() functions.
In essence: 30 lines of source code to get the streaming abstraction. And no runtime (memory, clock ticks, code size) overhead.

Thoughts?

related post

  • Sign in to reply
  • beacon_dave
    beacon_dave 7 minutes ago in reply to Jan Cumps

    image

    Joe or Averall ?  Slight smile

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 44 minutes ago

    what about his?:

    • Customer record:

    struct customer {
        customer (const std::string& name, int credit) : name(name), credit(credit) {}
        const std::string name;
        int credit;
        operator std::string() const {
            return name;
        }
    };
    

    • Customer database:

    std::array<customer, 3> customers {{{"joe", 3}, {"jane", 500}, {"averall", -8}}};

    • Stream insolvent customers to uart:

        u << "insolvent customers:" << "\n";
        for(const auto &c : customers | std::views::filter([](const customer& c){ return c.credit < 0;})) {
                    u << c << "\n";
        }

    • Output:

    image

    and wealthy customers-:

        u << "rich customers:" << "\n";
        for(const auto &c : customers | std::views::filter([](const customer& c){ return c.credit > 100;})) {
                    u << c << "\n";
        }

    Output:

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 1 hour ago

    You could get code like this Slight smile

        dog d("odie");
        cat c("garfield");
        u << d << "\n" << c << "\n" ;  

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube