element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
Code Exchange
  • Technologies
  • More
Code Exchange
Forum do you have experience using a Linux serial port as C++ stream
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 3 replies
  • Subscribers 46 subscribers
  • Views 515 views
  • Users 0 members are here
Related

do you have experience using a Linux serial port as C++ stream

Jan Cumps
Jan Cumps over 1 year ago

I'm trying to use a Linux serial port (say /dev/ttyx) as a sting stream in C++. Just like std::cout is used.

Do you have experience with it, or do you know a decent example?

  • Sign in to reply
  • Cancel

Top Replies

  • vmate
    vmate over 1 year ago +1
    Threw something together quick, I didn't have time to test it, but give it a try: https://gist.github.com/03vmate/dbd3d423bc033367ae24faa6a6616860 Use it like this: SerialStream serial( "/dev/ttyX…
  • shabaz
    shabaz over 1 year ago

    Interesting question. I've not worked in this area, and don't have an example. However, I saw this:

    https://www.nuget.org/packages/crozone.SerialPorts.LinuxSerialPort

    It's quite old code. But am wondering if there is useful stuff there that could be resurrected as a serial port class with stream methods for accessing.

    EDIT: forget that. I just peeked at the code, it's C# : (

    EDIT 2: Looks like Qt supports streaming access: https://doc.qt.io/qt-6/qserialport.html

    Don't know if you wish to add that much middleware to your app though. 

    There are other serial port stream implementations but they seem to be in other languages like Java : (

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • vmate
    vmate over 1 year ago

    Threw something together quick, I didn't have time to test it, but give it a try:
    https://gist.github.com/03vmate/dbd3d423bc033367ae24faa6a6616860

    Use it like this:

    SerialStream serial("/dev/ttyX", 9600);

    // Write a string to the serial port
    serial << "AT\n\r";

    // Read a line from the serial port
    std::cout << "Response: " << serial << std::endl;

    // Read a line, in a different way
    std::string response;
    serial >> response;
    std::cout << "Response: " << response << std::endl;
    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 1 year ago in reply to vmate

    That looks very promising.

    I found something that I will also test out: https://stackoverflow.com/a/24992028

    #ifndef DEVTTY
    #define DEVTTY "con"    // on Windows
    // #define DEVTTY "/dev/tty" // on Linux/MacOS
    #endif
    
    using namespace std;
    
    int main()
    {
       cout << "DEVTTY = " << DEVTTY << endl;
       printf("Print[f]ed to stdout.\n");
       fprintf(stdout, "Print[f]ed to stdout.\n");
       cout << "Printed to std::cout" << endl;
    
       fprintf(stderr, "Print[f]ed to stderr.\n");
       cerr << "Printed to std::cerr" << endl;
    
       {
          // C, stdio version
          FILE* fd = fopen(DEVTTY, "w");
          fprintf(fd, "Printed to \"%s\"\n", DEVTTY); // will not be redirected
          fclose(fd);
       }
    
       {
          // C++, fstream version
          std::ofstream ofs(DEVTTY);
          ofs << "Printed via std::ofstream to \"" << DEVTTY << "\"" << endl; // will not be redirected
       }
    
       return EXIT_SUCCESS;
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • 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