I have a GPS that has a serial port, and it has a transfer cable from serial to USB. It is connected to the laptop through USB, and is transmitting data in Hex. I want to use this data in my C++ code on Ubuntu. Any ideas where could I start, and how I could manage to read the data?
-
1See this stack overflow on setting up the connection http://stackoverflow.com/questions/5347962/how-to-connect-to-a-terminal-to-serial-usb-device-on-ubuntu-10-10 – Richard Chambers Apr 30 '13 at 13:18
-
And then this one on reading and writing the device http://stackoverflow.com/questions/5014451/reading-and-writing-to-from-serial-device-via-usb-on-linux-with-perl-or-php – Richard Chambers Apr 30 '13 at 13:19
-
And you can help us help you by posting the exact manufacturer and model of the GPS. – dbasnett Apr 30 '13 at 13:30
-
http://www.navilock.de/produkte/G_60109/merkmale.html?setLanguage=en this is the exact GPS we are using here. – Zeyad May 05 '13 at 00:14
-
It's not "transmitting data in hex". It's transmitting data in some encoded binary form like all computers do - you're just looking at the values in hex. – Lightness Races in Orbit Oct 23 '18 at 20:28
2 Answers
Start by opening a file (fstream, FILE * or OS handle) to "/dev/ttySn" where n
is the number of the device, then read away.
#include <string>
#include <fstream>
#include <iostream>
std::string str;
std::fstream f;
f.open("/dev/ttyS0");
while (f >> str)
{
std::cout << str;
}
That should "echo" out the contents of ttyS0 - that may of course be the "wrong" port for your device, but if you know where it is connected that should do the job. You may also need to set the configuration of the port. For example, using the stty
command - baudrate would be the most important, so something like stty -F /dev/ttyS0 19200
will set the baudrate (actually bitrate) to 19200 bits per second. Most other parameters are probably ok.

- 378,754
- 76
- 643
- 1,055

- 126,704
- 14
- 140
- 227
-
Thank you. I have a second question how do I know the port of the device ? – Zeyad May 05 '13 at 00:02
-
1I have no idea. Try having a look in `dmesg` or something to find which port the kernel gives it when you plug your USB serial port in. – Mats Petersson May 05 '13 at 07:21
#include <iostream>
#include <fstream>
int main () {
std::string str;
std::fstream f;
f.open("/dev/ttyS0");
while (f >> str)
{
std::cout << str;
}
}
Here's a working snippet. When I had this question I struggled to find the needed include
statements and the correct namespace.
This answer was based on Mats Petersson's answer
Edit: This would be more appropriate as a comment to Mats Petersson's answer. The comment could be:
"
#include fstream
forfstream
and#include iostream
forstring
andcout
. The namespace offstream
andcout
isstd
."

- 1,017
- 3
- 13
- 24
-
I've corrected Mats's answer; probably no need for this now – Lightness Races in Orbit Oct 23 '18 at 20:30