LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 11-16-2007, 10:02 AM   #1
dovkruger
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 deluo.com, 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
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,527

Rep: Reputation: 147Reputation: 147
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.
 
  


Reply

Tags
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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 02:42 AM.

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