www.smartbusinesschoices.com

Leading Business and Technology,
News and information


Part of the Identityscape.com network...

getxfactor.com jmoodmusic.com smartbusinesschoices.com mintdepot.com lowfaresalways.com evangelicalview.com shoppingpodder.com soproudlywehail.com webnews.ws currenthumor.com

 

 

Reading the Linux Serial Port in C++
   Smart Linux Business Choices! - the Best of UseNet Postings! Forum Index -> Linux - Help  
View previous topic :: View next topic  
Author Message
Hal Vaughan
Guest






PostPosted: Tue Mar 11, 2008 11:48 pm    Post subject: Reading the Linux Serial Port in C++ Reply with quote

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!

Hal
Back to top
Joe Zeff
Guest






PostPosted: Wed Mar 12, 2008 11:18 am    Post subject: Re: Reading the Linux Serial Port in C++ Reply with quote

On Tue, 11 Mar 2008 18:48:00 GMT, Hal Vaughan <hal@halblog.com> wrote:

Quote:
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.
http://www.lasfs.info http://www.zeff.us
Back to top
Hal Vaughan
Guest






PostPosted: Wed Mar 12, 2008 8:31 pm    Post subject: Re: Reading the Linux Serial Port in C++ Reply with quote

Joe Zeff wrote:

Quote:
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
Back to top
Display posts from previous:   
   Smart Linux Business Choices! - the Best of UseNet Postings! Forum Index -> Linux - Help  
Page 1 of 1
All times are GMT

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum