473,548 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading the Serial Port

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
Mar 11 '08 #1
9 14356
On Mar 11, 12:22 pm, Hal Vaughan <h...@halblog.c omwrote:
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
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.
Mar 11 '08 #2
Christopher wrote:
On Mar 11, 12:22 pm, Hal Vaughan <h...@halblog.c omwrote:
>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

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
Mar 11 '08 #3
On Mar 11, 1:22*pm, Hal Vaughan <h...@halblog.c omwrote:
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 seemsto
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
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::binar y" 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(&d ata,sizeof(data ));

Hope that helps
Lance
Mar 11 '08 #4
Hal Vaughan <ha*@halblog.co mwrote in
news:VvABj.5932 $HA3.2326@trndd c02:
Christopher wrote:
>On Mar 11, 12:22 pm, Hal Vaughan <h...@halblog.c omwrote:
>>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
Mar 11 '08 #5
Paavo Helde wrote:
Hal Vaughan <ha*@halblog.co mwrote in
news:VvABj.5932 $HA3.2326@trndd c02:
>Christopher wrote:
>>On Mar 11, 12:22 pm, Hal Vaughan <h...@halblog.c omwrote:
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
Mar 11 '08 #6
Lance Diduck wrote:
On Mar 11, 1:22*pm, Hal Vaughan <h...@halblog.c omwrote:
>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
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::binar y" 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(&d ata,sizeof(data ));

Hope that helps
Lance
It helps a LOT!

Thank you!

Hal
Mar 11 '08 #7
Hal Vaughan <ha*@halblog.co mwrote in news:dzCBj.3956 $z13.99@trnddc0 6:
Paavo Helde wrote:
>Hal Vaughan <ha*@halblog.co mwrote in
news:VvABj.593 2$HA3.2326@trnd dc02:
>>Christopher wrote:

On Mar 11, 12:22 pm, Hal Vaughan <h...@halblog.c omwrote:
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
Mar 11 '08 #8
Hal Vaughan wrote:

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.
*plonk*

Brian
Mar 12 '08 #9
Christopher wrote:
On Mar 11, 1:50 pm, Hal Vaughan <h...@halblog.c omwrote:
>fd1=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
>ofstream myfile;
>myfile.open( "/dev/ttyS0", ios::out | ios::binary);
Sure, the first is C and the second is C++.
Not even that. The first is POSIX, which happens to have a C API. It's
no more a C function that it is a C++ one.


Brian
Mar 12 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9066
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the same port to change the direction of the robot. The trajectory frame is managed by an applet, and the project works good when the applet is called by...
1
4576
by: ORC | last post by:
I have made a serial port class inspired bu the MTTTY example and the serial port class from OpenNETCF . below is the code from the readfile method in which there is a problem. The code calls the WaitForSingleObject function but always locks there until the timeout happens (GetOverlappedResult is never called). If data is present in the buffer...
13
4798
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have been told it is not possible to access the serial ports from asp.net. The application is used to control custom hardware. The hardware is...
8
16336
by: Vivek Menon | last post by:
Hi, I am using a C program to write/read from a serial port. The writing part is working perfectly fine. However, I am not able to read the values correctly and display them. To debug this issue I am also seeing the values on minicom. Now I have used fcntl() function and then read refp = fcntl(fd, F_SETFL, 0); res = read(fd,buf,1024); /*...
13
6173
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I am sending a short message to a group of embedded boxes daisy chained via the serial port. When I send a 'global' message, all the connected units...
16
4141
by: bloggsfred00 | last post by:
I need to read incoming bytes on a COM port but I do not want to have the script hang if there is nothing to read. Is there any way to have PHP interrogate a COM port buffer to see if there is any content before reading it? Any other method to achieve the result would also be welcome. Cheers,
0
1994
by: RG | last post by:
I am trying to read from my serial port a 24 bit binary number. I was able to read this number as a HEX but I was getting errors as at times using the vBCrLf indicator. I also can read it as an incoming string looking for the vbCrLf indicator but it too gave me errors as the length of the stream was not always 24 bits for some reason. I...
1
11520
by: ghjk | last post by:
I'm trying to read sms from GSM modem using c#. my code is only working for at command. When I enter at+cmgr=1 it says"ERROR". but when i typr it hyperterminal. It is working.Could you please tell me what's wrong. I'm posting my code here. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using...
6
8431
by: anu29dolly | last post by:
Hello everyone... I have written a program to write and read data from serial port.... I am able to write 80(in binary)..and is expecting 1B but i am uable to read it... My code goes as follows,,.... int main()
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.