Results 1 to 4 of 4
Hi!
I'm currently working on a small software to access GPS information
from a GPS receiver. I read a couple of tutorial to code this program :
(sorry I had ...
- 12-09-2010 #1Just Joined!
- Join Date
- Dec 2010
- Posts
- 3
[SOLVED] Serial programming and GPS mouse receiver
Hi!
I'm currently working on a small software to access GPS information
from a GPS receiver. I read a couple of tutorial to code this program :
(sorry I had to write URL differently to post)
easysw dot com/~mike/serial/serial dot html
and
linux dot org/docs/ldp/howto/...OWTO/x115 dot html
I try to access data from the GPS receiver thanks to my program but it doesn't work and I really don't know why. Indeed I can get NMEA lines (GPS information) by typing : `stty -F /dev/ttyUSB0 4800 cs8` and then `cat /dev/ttyUSB0`. But I cannot with my software whereas I use strictly the same configuration used in command line (that is with stty -F /dev/ttyUSB0 4800 cs8 && cat /dev/ttyUSB0)
the configuration used in command line (stty -a -F /dev/ttyUSB0) :
But when I run the program I receive no data at all.Code:speed 4800 baud; rows 0; columns 0; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
Here is my program :
Code:#include <stdio.h> #include <termios.h> #include <unistd.h> #include <fcntl.h> #include <error.h> #include <stdlib.h> int main(void) { int fd, result; char buffer[255]; struct termios options, storedOptions; if((fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY) == -1)) { perror("ERROR (device not opened) "); exit(EXIT_FAILURE); } else { /* if(fcntl(fd, F_SETFL, FNDELAY) == -1) { perror("ERROR (device problem) "); exit(EXIT_FAILURE); } */ if(tcgetattr(fd, &options) == -1) { perror("ERROR (port configuration problem) "); exit(EXIT_FAILURE); } storedOptions = options; // baudrate if(cfsetispeed(&options, B4800) == -1 || cfsetospeed(&options, B4800) == -1) { perror("ERROR (baudrate setting) "); exit(EXIT_FAILURE); } // line oriented mode // (notice : be careful to desactivate ECHO before sending something to the devive) options.c_lflag &= ~ECHONL; options.c_lflag &= ~NOFLSH; options.c_lflag &= ~ECHOPRT; options.c_lflag &= ~TOSTOP; options.c_lflag &= ~XCASE; options.c_lflag |= ICANON; options.c_lflag |= ISIG; options.c_lflag |= IEXTEN; options.c_lflag |= ECHO; options.c_lflag |= ECHOE; options.c_lflag |= ECHOK; options.c_lflag |= ECHOKE; options.c_lflag |= ECHOCTL; options.c_cflag &= ~PARENB; options.c_cflag &= ~PARODD; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CRTSCTS; options.c_cflag |= CS8; options.c_cflag |= HUPCL; options.c_cflag |= CREAD; options.c_cflag |= CLOCAL; options.c_iflag &= ~IGNBRK; options.c_iflag &= ~BRKINT; options.c_iflag &= ~IGNPAR; options.c_iflag &= ~PARMRK; options.c_iflag &= ~INPCK; options.c_iflag &= ~ISTRIP; options.c_iflag &= ~INLCR; options.c_iflag &= ~IGNCR; options.c_iflag &= ~IXOFF; options.c_iflag &= ~IUCLC; options.c_iflag &= ~IXANY; options.c_iflag &= ~IMAXBEL; options.c_iflag &= ~IUTF8; options.c_iflag |= ICRNL; options.c_iflag |= IXON; options.c_oflag &= ~OLCUC; options.c_oflag &= ~OCRNL; options.c_oflag &= ~ONOCR; options.c_oflag &= ~ONLRET; options.c_oflag &= ~OFILL; options.c_oflag &= ~OFDEL; options.c_oflag |= OPOST; options.c_oflag |= ONLCR; options.c_oflag |= NL0; options.c_oflag |= CR0; options.c_oflag |= TAB0; options.c_oflag |= BS0; options.c_oflag |= VT0; options.c_oflag |= FF0; /* character setting */ options.c_cc[VINTR]= 3; // "^C" options.c_cc[VQUIT]= 28; // "^\" options.c_cc[VERASE] = 127; // "^?" options.c_cc[VKILL]= 21; // ^U options.c_cc[VEOF]= 4; // ^D options.c_cc[VSTART]= 17; // ^Q options.c_cc[VSTOP]= 19; // ^S options.c_cc[VSUSP]= 26; // ^Z options.c_cc[VREPRINT]= 18; // ^R options.c_cc[VWERASE]= 23; // ^W options.c_cc[VLNEXT]= 22; // ^V options.c_cc[VMIN]=1; options.c_cc[VTIME]=0; if(tcsetattr(fd, TCSANOW, &options) == -1) { perror("ERROR (port configuration setting) "); exit(EXIT_FAILURE); } int nodataCnt = 0; while(nodataCnt < 200) { result = read(fd, buffer, sizeof(buffer)-1); //perror("ERROR? "); // keep returning "Success" // read fail if(result == -1) { perror("ERROR (read process) "); exit(EXIT_FAILURE); } // no data if(!result) { printf("no data\n"); nodataCnt++; } // data received if(result > 0) { buffer[result] = 0; printf("string : %s number : %d\n", buffer, result); } } tcsetattr(fd, TCSANOW, &storedOptions); close(fd); } return EXIT_SUCCESS; }
- 12-10-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
So, where did you get the body of your program from? Also, where did you get the stty settings for /dev/ttyUSB0? Also, are you sure that is the correct device ID for your GPS? What does the command lsusb (run as root or sudo) show?
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 12-10-2010 #3Just Joined!
- Join Date
- Dec 2010
- Posts
- 3
I wrote the body of the program thanks to a couple of tutorial that teaches me the basics of serial port programming (as I cannot post URLs yet, it's the first tutorial we get when we type in google : "serial programming posix"). It's written in the GPS mouse's documentaion that I have to select 4800 bauds / 8N1 mode (cs8 with stty).
Yes I'm sure ttyUSB0 is the right device :
(cp210x is the USB to UART bridge used by my GPS mouse)
Here is what I get when I type `lsusb` :Code:[ 6.699223] USB Serial support registered for cp210x [ 6.699259] cp210x 6-1:1.0: usb_probe_interface [ 6.699262] cp210x 6-1:1.0: usb_probe_interface - got id [ 6.699267] cp210x 6-1:1.0: cp210x converter detected [ 6.704048] hub 6-0:1.0: state 7 ports 2 chg 0000 evt 0002 [ 6.801031] usb 6-1: reset full speed USB device using uhci_hcd and address 2 [ 6.935079] usb 6-1: cp210x converter now attached to ttyUSB0
Code:Bus 001 Device 002: ID 174f:6a31 Syntek Web Cam - Asus A8J, F3S, F5R, VX2S, V1S Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 007 Device 002: ID 08ff:1600 AuthenTec, Inc. AES1600 Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 006 Device 002: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x Composite Device Bus 006 Device 003: ID 046d:c019 Logitech, Inc. Optical Tilt Wheel Mouse Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 004 Device 002: ID 0b05:1712 ASUSTek Computer, Inc. BT-183 Bluetooth 2.0+EDR adapter Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
- 12-10-2010 #4Just Joined!
- Join Date
- Dec 2010
- Posts
- 3
Ok I found the problem ....
I made a beginner mistake.
(the 'fd' variable cannot be used in the else block)
Here is the source of the working program :
Code:#include <stdio.h> #include <termios.h> #include <unistd.h> #include <fcntl.h> #include <error.h> #include <errno.h> #include <stdlib.h> #include <string.h> int main(void) { int fd, result; char buffer[255]; struct termios options, storedOptions; fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY); if(!fd) { perror("ERROR (device not opened) "); exit(EXIT_FAILURE); } if(fcntl(fd, F_SETFL, FNDELAY) == -1) { perror("ERROR (device problem) "); exit(EXIT_FAILURE); } if(tcgetattr(fd, &options) == -1) { perror("ERROR (port configuration problem) "); exit(EXIT_FAILURE); } bzero(&options, sizeof(options)); // baudrate if(cfsetispeed(&options, B4800) == -1 || cfsetospeed(&options, B4800) == -1) { perror("ERROR (baudrate setting) "); exit(EXIT_FAILURE); } // line oriented mode // (notice : be careful to desactivate ECHO before sending something to the devive) options.c_lflag &= ~ECHONL; options.c_lflag &= ~NOFLSH; options.c_lflag &= ~ECHOPRT; options.c_lflag &= ~TOSTOP; options.c_lflag &= ~XCASE; options.c_lflag |= ICANON; options.c_lflag |= ISIG; options.c_lflag |= IEXTEN; options.c_lflag |= ECHO; options.c_lflag |= ECHOE; options.c_lflag |= ECHOK; options.c_lflag |= ECHOKE; options.c_lflag |= ECHOCTL; options.c_cflag &= ~PARENB; options.c_cflag &= ~PARODD; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CRTSCTS; options.c_cflag |= CS8; options.c_cflag |= HUPCL; options.c_cflag |= CREAD; options.c_cflag |= CLOCAL; options.c_iflag &= ~IGNBRK; options.c_iflag &= ~BRKINT; options.c_iflag &= ~IGNPAR; options.c_iflag &= ~PARMRK; options.c_iflag &= ~INPCK; options.c_iflag &= ~ISTRIP; options.c_iflag &= ~INLCR; options.c_iflag &= ~IGNCR; options.c_iflag &= ~IXOFF; options.c_iflag &= ~IUCLC; options.c_iflag &= ~IXANY; options.c_iflag &= ~IMAXBEL; options.c_iflag &= ~IUTF8; options.c_iflag |= ICRNL; options.c_iflag |= IXON; options.c_oflag &= ~OLCUC; options.c_oflag &= ~OCRNL; options.c_oflag &= ~ONOCR; options.c_oflag &= ~ONLRET; options.c_oflag &= ~OFILL; options.c_oflag &= ~OFDEL; options.c_oflag |= OPOST; options.c_oflag |= ONLCR; options.c_oflag |= NL0; options.c_oflag |= CR0; options.c_oflag |= TAB0; options.c_oflag |= BS0; options.c_oflag |= VT0; options.c_oflag |= FF0; /* character setting */ options.c_cc[VINTR]= 3; // "^C" options.c_cc[VQUIT]= 28; // "^\" options.c_cc[VERASE] = 127; // "^?" options.c_cc[VKILL]= 21; // ^U options.c_cc[VEOF]= 4; // ^D options.c_cc[VSTART]= 17; // ^Q options.c_cc[VSTOP]= 19; // ^S options.c_cc[VSUSP]= 26; // ^Z options.c_cc[VREPRINT]= 18; // ^R options.c_cc[VWERASE]= 23; // ^W options.c_cc[VLNEXT]= 22; // ^V options.c_cc[VMIN]=1; options.c_cc[VTIME]=0; if(tcsetattr(fd, TCSANOW, &options) == -1) { perror("ERROR (port configuration setting) "); exit(EXIT_FAILURE); } int nodataCnt = 0; while(nodataCnt < 200) { result = read(fd, buffer, sizeof(buffer) - 1); // read fail if(result == -1) { perror("ERROR (read process) "); exit(EXIT_FAILURE); } // no data if(!result) { printf("no data\n"); nodataCnt++; } // data received if(result > 0) { buffer[result] = 0; printf("string : %s number : %d\n", buffer, result); } } tcsetattr(fd, TCSANOW, &storedOptions); close(fd); return EXIT_SUCCESS; }



