Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reading the Serial Port

Reply
Thread Tools

Reading the Serial Port

 
 
Hal Vaughan
Guest
Posts: n/a
 
      03-11-2008
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:ut | 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
 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      03-11-2008
On Mar 11, 12:22 pm, Hal Vaughan <(E-Mail Removed)> wrote:
> 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:ut | 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


The C++ language has no concept of what a serial port is, therefor it
is off topic here. However, a newsgroup specific to your operating
system may very well hold answers for you.
 
Reply With Quote
 
 
 
 
Hal Vaughan
Guest
Posts: n/a
 
      03-11-2008
Christopher wrote:

> On Mar 11, 12:22 pm, Hal Vaughan <(E-Mail Removed)> wrote:
>> 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:ut | 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

>
> The C++ language has no concept of what a serial port is, therefor it
> is off topic here. However, a newsgroup specific to your operating
> system may very well hold answers for you.



So nobody here would have insight or help on reading from a device overall?
Or what the difference between the two methods are even if it's not just
Linux?

I'm sure an experienced C++ programmer could still give me some good info
about the two different ways to open a file/device that would help with an
overall understanding of the situation. I'm sure, for instance, that the
first method is not Linux specific and I know the 2nd one is not.

Hal
 
Reply With Quote
 
Lance Diduck
Guest
Posts: n/a
 
      03-11-2008
On Mar 11, 1:22*pm, Hal Vaughan <(E-Mail Removed)> wrote:
> 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:ut | 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

There is no reason that the ofstream cant be used. There are tons of
differences in how errors are handled, how formatted IO is handled,
and so forth. (the "ios::binary" will turn off some of the character
translations that go on between different encodings -- something you
dont need in serial ports)
The other differences is that once myfile goes out of scope, all
system resources will be released. And of course ofstream will format
your data into character based streams. If you really want to send raw
bytes through your serial port, avoid doing this
myfile<<data;
and do this instead:
myfile.write(&data,sizeof(data));

Hope that helps
Lance
 
Reply With Quote
 
Paavo Helde
Guest
Posts: n/a
 
      03-11-2008
Hal Vaughan <(E-Mail Removed)> wrote in
news:VvABj.5932$HA3.2326@trnddc02:

> Christopher wrote:
>
>> On Mar 11, 12:22 pm, Hal Vaughan <(E-Mail Removed)> wrote:
>>> I've done a fair amount of Googling for information on reading the
>>> serial port in C++ (and in Linux).


Maybe the reason of your failure is that serial port is becoming
obsolete. Most peripheral devices are using USB nowadays. From your post
I gather that you are planning to make two threads in the same process
communicate over the serial port - why on the earth you would want to do
that?

[..]
>>> 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.)

[...]
>
> So nobody here would have insight or help on reading from a device
> overall?


For interprocess or interthread communication you don't need any device!

Regards
Paavo
 
Reply With Quote
 
Hal Vaughan
Guest
Posts: n/a
 
      03-11-2008
Paavo Helde wrote:

> Hal Vaughan <(E-Mail Removed)> wrote in
> news:VvABj.5932$HA3.2326@trnddc02:
>
>> Christopher wrote:
>>
>>> On Mar 11, 12:22 pm, Hal Vaughan <(E-Mail Removed)> wrote:
>>>> I've done a fair amount of Googling for information on reading the
>>>> serial port in C++ (and in Linux).

>
> Maybe the reason of your failure is that serial port is becoming
> obsolete. Most peripheral devices are using USB nowadays. From your post
> I gather that you are planning to make two threads in the same process
> communicate over the serial port - why on the earth you would want to do
> that?


It's not failing, what I'm trying to understand is what is the difference in
the two methods? Are there things that won't show up in a simple test?
Are there implications in one method that aren't there in the other that
someone new to C++ won't think of without help?

Because that's where the device is I need to communicate with.

If I use a USB/Serial adaptor on a USB port, will reading and writing to the
USB port be just the same as to/from the serial port? (I would think so,
but I don't know if the USB/serial adaptor makes things different.)

> [..]
>>>> 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.)

> [...]
>>
>> So nobody here would have insight or help on reading from a device
>> overall?

>
> For interprocess or interthread communication you don't need any device!


Sorry, it's a separate issue and I was thinking of the whole thing together
and I shouldn't have combined the two in one post.

Hal
 
Reply With Quote
 
Hal Vaughan
Guest
Posts: n/a
 
      03-11-2008
Lance Diduck wrote:

> On Mar 11, 1:22*pm, Hal Vaughan <(E-Mail Removed)> wrote:
>> 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:ut | 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

> There is no reason that the ofstream cant be used. There are tons of
> differences in how errors are handled, how formatted IO is handled,
> and so forth. (the "ios::binary" will turn off some of the character
> translations that go on between different encodings -- something you
> dont need in serial ports)
> The other differences is that once myfile goes out of scope, all
> system resources will be released. And of course ofstream will format
> your data into character based streams. If you really want to send raw
> bytes through your serial port, avoid doing this
> myfile<<data;
> and do this instead:
> myfile.write(&data,sizeof(data));
>
> Hope that helps
> Lance


It helps a LOT!

Thank you!

Hal
 
Reply With Quote
 
Christopher
Guest
Posts: n/a
 
      03-11-2008
On Mar 11, 1:50 pm, Hal Vaughan <(E-Mail Removed)> wrote:
> Christopher wrote:
> > On Mar 11, 12:22 pm, Hal Vaughan <(E-Mail Removed)> wrote:
> >> 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:ut | 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

>
> > The C++ language has no concept of what a serial port is, therefor it
> > is off topic here. However, a newsgroup specific to your operating
> > system may very well hold answers for you.

>
> So nobody here would have insight or help on reading from a device overall?
> Or what the difference between the two methods are even if it's not just
> Linux?
>
> I'm sure an experienced C++ programmer could still give me some good info
> about the two different ways to open a file/device that would help with an
> overall understanding of the situation. I'm sure, for instance, that the
> first method is not Linux specific and I know the 2nd one is not.
>
> Hal


Sure, the first is C and the second is C++.
If that is all wanted to know, you could leave mention of serial ports
and everything else out of your post and could have entitled it
"opening a file", otherwise you are looking for information specific
to serial port IO, which is not part of the C++ language.

 
Reply With Quote
 
Paavo Helde
Guest
Posts: n/a
 
      03-11-2008
Hal Vaughan <(E-Mail Removed)> wrote in news:dzCBj.3956$z13.99@trnddc06:

> Paavo Helde wrote:
>
>> Hal Vaughan <(E-Mail Removed)> wrote in
>> news:VvABj.5932$HA3.2326@trnddc02:
>>
>>> Christopher wrote:
>>>
>>>> On Mar 11, 12:22 pm, Hal Vaughan <(E-Mail Removed)> wrote:
>>>>> I've done a fair amount of Googling for information on reading the
>>>>> serial port in C++ (and in Linux).

>>
>> Maybe the reason of your failure is that serial port is becoming
>> obsolete. Most peripheral devices are using USB nowadays. From your
>> post I gather that you are planning to make two threads in the same
>> process communicate over the serial port - why on the earth you would
>> want to do that?

>
> It's not failing, what I'm trying to understand is what is the
> difference in the two methods? Are there things that won't show up in
> a simple test? Are there implications in one method that aren't there
> in the other that someone new to C++ won't think of without help?
>
> Because that's where the device is I need to communicate with.
>
> If I use a USB/Serial adaptor on a USB port, will reading and writing
> to the USB port be just the same as to/from the serial port? (I would
> think so, but I don't know if the USB/serial adaptor makes things
> different.)


Sorry, I misunderstood your problem! I don't know answers to your
questions. Alas, the serial ports are outside of C++ and off-topic in
this ng, please try some linux newsgroup!

(I myself would go for POSIX open() route just to avoid any chances that
the C++ layer fails to provide some seemingly needed flags like O_NOCTTY
or O_NDELAY. But this would be non-portable of course.)

Regards
Paavo
 
Reply With Quote
 
Hal Vaughan
Guest
Posts: n/a
 
      03-12-2008
Christopher wrote:

> On Mar 11, 1:50 pm, Hal Vaughan <(E-Mail Removed)> wrote:
>> Christopher wrote:
>> > On Mar 11, 12:22 pm, Hal Vaughan <(E-Mail Removed)> wrote:
>> >> 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:ut | 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

>>
>> > The C++ language has no concept of what a serial port is, therefor it
>> > is off topic here. However, a newsgroup specific to your operating
>> > system may very well hold answers for you.

>>
>> So nobody here would have insight or help on reading from a device
>> overall? Or what the difference between the two methods are even if it's
>> not just Linux?
>>
>> I'm sure an experienced C++ programmer could still give me some good info
>> about the two different ways to open a file/device that would help with
>> an
>> overall understanding of the situation. I'm sure, for instance, that the
>> first method is not Linux specific and I know the 2nd one is not.
>>
>> Hal

>
> Sure, the first is C and the second is C++.


Any reason why you didn't want to say that at the start? It would have been
quite helpful to a new C++ programmer.

> If that is all wanted to know, you could leave mention of serial ports
> and everything else out of your post and could have entitled it
> "opening a file", otherwise you are looking for information specific
> to serial port IO, which is not part of the C++ language.


Because I didn't know all that until I asked and some people were quite
helpful in explaining what they knew. Some people gave me whatever info
they could along with pointing out the serial port was OS specific.

And some people decided that rather than try to provide helpful information
from the start and addressing the parts of my post they could, that they'd
rather just play Internet Policeman.

I'll remember in the future that if there is something I truly don't
understand and don't know that much about that instead of just asking the
question in what seems, at the time, to be the appropriate forum, to just
not ask because Christopher would rather make sure people post in exactly
the right place than that they get what help could be offered within the
context of that situation.

Hal
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
emulate a serial port in windows (create a virtual 'com' port) Pom Python 2 01-31-2007 07:49 PM
Serial Port programming - Reading DSR from port msalerno Perl Misc 3 07-14-2005 12:58 PM
Can I connect router Serial interface directly to a PC serial port? Faustino Dina Cisco 2 08-18-2004 02:30 AM
Re: Serial port and PS/2 port schematics OR Assistive Tech. suggestion naive.verizon@locality.net Computer Support 1 07-10-2003 11:46 AM
Re: Serial port and PS/2 port schematics °Mike° Computer Support 1 07-09-2003 10:30 PM



Advertisments