Thread: Hardware IO in linux

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    Hardware IO in linux

    I'm a fairly new programmer in C++. I was wondering if there were any built-in function in C++ that could access the ports on my computer (which is running Ubuntu) or should I just start going through the big linux programming book and figure I'll get there eventually?

    Thanks,
    Charlie West

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Any particular ports in mind?
    Also "port" is a massively over-used term, can you clarify which you mean?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ports like a bound socket listener? Or ports like a usb port?

    If the former, sort of... sockets are a standardized library that is usually shipped with your compiler, but is not part of the C standard. If the latter, yes. The iostreams can handle this for many types of ports.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    The parallel port if possible, though I may end up using an USB to parallel adapter. I'm sorry, what is a socket listener?

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It "listens" to the socket and handles incoming data, if any (not a great explanation).

    You can read/write to the parallel port. You will need I think the address of the port (google, maybe from /proc/ioports), use something like ioperm() to get permission to use the port and then use something like outb() to send/receive data

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    fstream printer("/dev/lpt0");

  7. #7

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Fair enough:

    Better example:
    Code:
    fstream printer("/dev/parport0");

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Can't use ioctl() with an fstream. Could use FILE* and then fileno() to get the integer descriptor for ioctl(). Or just use open().

    gg

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am a fan of C file streams over C++, so that would be my way to go. But I am not the OP so perhaps....

    Example:
    Code:
    fstream *printer(mode_t perm)
    {
    #ifdef ELYSIA
      fstream *f = NULL;
    #else
      fstream *f = 0;
    #endif
    
      int fd = open("/dev/parport0", O_RDWR, perm);
    
      // do whatever you need to do....
    
      try
      {
        f = new fstream(fd);
      } catch(std::exception e)
      {
        close(fd);
        abort();
      }
    
      return f;
    }

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by master5001 View Post
    Code:
    ..
    #ifdef ELYSIA
      fstream *f = NULL;
    #else
      fstream *f = 0;
    #endif
    ...
    LOL and feel a bit worried I understood the joke...

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I just try to cover the bases since its halfway amusing halfway me seriously not wanting to bicker about meaningless details.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a linux hardware driver
    By nexusone in forum Linux Programming
    Replies: 1
    Last Post: 08-12-2008, 07:33 PM
  2. Why Linux, for the average user?
    By Hunter2 in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 07-07-2006, 02:36 PM
  3. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 05:15 AM
  4. Linux Hardware Handbook
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-02-2002, 06:06 AM