Groups > Unix Linux > LinuxHelp > Re: Reading the Linux Serial Port in C++




Reading the Linux Serial Port in C++

Reading the Linux Serial Port in C++
Tue, 11 Mar 2008 18:48:00 GMT
I posted this on comp.lang.c++ and was told to go to a group on my OS
(Linux) and ask it, so I'm asking it here.  Thanks for any help!

I've done a fair amount of Googling for information on reading the serial
port in C++ (and in Linux).  Unfortunately, out of every 4 hits, 1 seems to
be an unanswered question, 1 is someone saying, "That's easy, there's a
lot
out there, Google it,", 1 is a discussion on it without examples and the
other is who knows what.

I did find some info on it and have been experimenting.  The one example
that I liked the best in terms of explanations and readability for a new
C++ programmer (coming over from Java and Perl) used this to open the
serial port (yes, with the .h on each include, I know it's older):

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
int fd1;
fd1=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

I understand the serial port will be binary and may be different from other
files, but I don't understand why this won't work just as well:

#include <iostream>
#include <fstream>

ofstream myfile;
myfile.open("/dev/ttyS0", ios::out | ios::binary);

Would that work as well or is there a reason for handling it the first way? 
Basically, after I catch up on everything, one program will be reading the
serial port and reporting the output and another will be sending data to
the port.  (It's possible, if I can ever find a good thread tutorial for
C++ that instead of different programs, they'll be different threads.)

Thanks for any help on the significance and differences of these two
methods!

Post Reply
Re: Reading the Linux Serial Port in C++
Wed, 12 Mar 2008 06:18:49 GMT
On Tue, 11 Mar 2008 18:48:00 GMT, Hal Vaughan <hal@halblog.com> wrote:

>I posted this on comp.lang.c++ and was told to go to a group on my OS
>(Linux) and ask it, so I'm asking it here.  Thanks for any help!

Since you're presumably doing this in Linux, why not just get the
kernel source and look at the appropriate functions?  See how it's
done in C, and you'll probably know how to do it in C++.  HTH, HAND.

-- 
Joe Zeff
     The Guy With the Sideburns
This has been a D'oh! moment.
Post Reply
Re: Reading the Linux Serial Port in C++
Wed, 12 Mar 2008 15:31:23 GMT
Joe Zeff wrote:

> On Tue, 11 Mar 2008 18:48:00 GMT, Hal Vaughan <hal@halblog.com>
wrote:
> 
>>I posted this on comp.lang.c++ and was told to go to a group on my OS
>>(Linux) and ask it, so I'm asking it here.  Thanks for any help!
> 
> Since you're presumably doing this in Linux, why not just get the
> kernel source and look at the appropriate functions?  See how it's
> done in C, and you'll probably know how to do it in C++.  HTH, HAND.

I hadn't though about that.  Thanks!

Hal 
Post Reply
about | contact