Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6078913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:53:14+00:00 2026-05-23T10:53:14+00:00

How can I read/write to a device in C++? the device is in /dev/ttyPA1

  • 0

How can I read/write to a device in C++? the device is in /dev/ttyPA1.
I thought about fstream but I can’t know if the device has output I can read without blocking the application.
My goal is to create and application where you write something into the terminal and it gets sent into /dev/ttyPA1. If the device has something to write back it will read it from the device and write to screen. If not it will give the user prompt to write to the device again.
How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:53:15+00:00Added an answer on May 23, 2026 at 10:53 am

    Use open(2), read(2), and write(2) to read from and write to the device (and don’t forget to close(2) when you’re done). You can also use the C stdio functions (fopen(3) and friends) or the C++ fstream classes, but if you do so, you almost definitely want to disable buffering (setvbuf(3) for stdio, or outFile.rdbuf()->pubsetbuf(0, 0) for fstreams).

    These will all operate in blocking mode, however. You can use select(2) to test if it’s possible to read from or write to a file descriptor without blocking (if it’s not possible, you shouldn’t do so). Alternatively, you can open the file with the O_NONBLOCK flag (or use fcntl(2) to set the flag after opening) on the file descriptor to make it non-blocking; then, any call to read(2) or write(2) that would block instead fails immediately with the error EWOULDBLOCK.

    For example:

    // Open the device in non-blocking mode
    int fd = open("/dev/ttyPA1", O_RDWR | O_NONBLOCK);
    if(fd < 0)
        ;  // handle error
    
    // Try to write some data
    ssize_t written = write(fd, "data", 4);
    if(written >= 0)
        ;  // handle successful write (which might be a partial write!)
    else if(errno == EWOULDBLOCK)
        ;  // handle case where the write would block
    else
        ;  // handle real error
    
    // Reading data is similar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a SQLite database on Android device. The program can read/write to database
I want to write a file where an external application can read it, but
i'm trying to read and write on a char device in ubuntu but when
Is there any class in the .NET framework that can read/write standard .ini files:
How can I read/write serializable object instances to a RandomAccessFile in Java? I want
I'm looking for an editor that can read and write remote PHP files via
Is there a stand-alone GUI designer that can read and write the .form files
With D and Tango library can I read and write in the ANSI encoding
How can i check the read/ write permission of the file storing media? ie
Can anoyne recommend a good library that will let me easily read/write private member

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.