Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-16-2007, 10:02 AM   #1
LQ Newbie
Registered: Nov 2003
Location: NY area
Distribution: redhat EE3, Fedore Core 4
Posts: 23

Rep: Reputation: 15
strange behavior with setting GPS serial port speed

I have a USB gps that uses a serial interface.
It's from, and the chipset in this case is Sony, but the same thing is true of other GPS systems I've been trying.

In order to read it, they told me to:

cat </dev/ttyUSB0

and then in another shell:

stty -F /dev/ttyUSB0 4800

Sure enough, it works.

But I wanted to do this programmatically. I tried writing code in C using termios.h
It didn't work, so I figured there was something stty was doing that my code wasn't doing. I got the source code to stty and walked through it, and all I can see are the same calls. I include the code here, it's pretty small:

#include <termios.h>
#include <sys/fcntl.h>
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
char* deviceName = argv[1];
struct termios tty;
int fd = open (deviceName, O_RDONLY | O_NONBLOCK);
int fdflags;
if ((fdflags = fcntl (fd, F_GETFL)) == -1) {
cerr << "error!\n";
if (tcgetattr (fd, &tty)) {
cerr << "screwed up serial port!\n";
// error (EXIT_FAILURE, errno, "%s", device_name);
cfsetospeed (&tty, B4800);
cfsetispeed (&tty, B4800);
tcsetattr (fd, TCSADRAIN, &tty);

The code, as it turns out, does work, but only in the sense that stty works. That is, I have to write code to open the gps, then in a subshell run the above, and it will (at least on the Mac where I just tested it) start showing the incoming data. But if I try to write a single code where I Open the file, set the baud rate, then read, it doesn't work. If I open the file, do a read, then attempt to set the baud rate, it hangs. I guess I could do it in a background thread, but the real question is why should i have to?

The only way I have gotten this to work in a program is to write perl that first opens the file, and then executes stty in a subshell. And it has to be IN THAT ORDER.
The exact same story is true on the Mac, except that the device names are different since it's BSD, /dev/cu.usbserial

Can anyone tell me what I'm doing wrong, how I can write code that will do this.
Old 11-17-2007, 03:55 PM   #2
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,571

Rep: Reputation: 180Reputation: 180
Strang thing. Try it in this order (one program): open, open again, change speed (second descriptor), close (second descriptor). Or open, change, close and open again.


serial, stty

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Speed of direct serial port access Vinter Linux - Hardware 1 08-01-2007 01:00 PM
strange serial port problem..can you help? djsroknrol Linux - Software 6 06-28-2006 04:28 PM
How to use stty to set serial port speed Cara25 Linux - Newbie 1 06-18-2006 12:46 AM
GPS via serial port MeLassen Linux - Hardware 3 08-19-2004 11:07 AM
speed/specs for ttyS0 or some other standard serial port? BigDawg Programming 3 01-23-2004 11:47 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:39 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration