Use USB to serial port to read GPS data in Linux

Source: Internet
Author: User

 

//----------------------------------------------------

// Author: lanyang123456

// Date: 2012-02-28

//----------------------------------------------------

Kernel version 2.6.18, 2.6.32, or 3.1.10

Linux kernel source code with USB to serial port driver

The directory is drivers/USB/serial,

Generally, the system compiles the USB-to-serial driver into a kernel module and automatically loads it into the kernel when necessary.

Insert the gps device (USB Interface) into the computer, the system automatically identifies the device, converts the USB to the serial port, and outputs information.

Command Line view, # dmesg

[2, 909.159038] USB 2-4: new full speed USB device number 5 using ohci_hcd
[909.338158] USB 2-4: new USB device found, idvendor = 067b, idproduct = 2303
[2, 909.338166] USB 2-4: new USB device strings: MFR = 0, Product = 0, serialnumber = 0
[2, 909.558764] USB serial support registered for pl2303
[909.559656] pl2303 2-. 0: pl2303 converter Detected
[2, 909.594064] USB 2-4: pl2303 converter now attached to ttyusb0
[2, 909.594866] usbcore: registered new interface driver pl2303
[2, 909.594872] pl2303: prolific pl2303 USB to serial adaptor driver

 

 

# Lsmod can view the newly added module pl2303 (the gps device is a pl2303 chip, and more than 70% of GPS devices use the prolific pl2303 driver .)

 

View directory/dev

Run the LS-L/dev/ttyusb0 command.

CrW-RW ---- 1 root dialout 188-0 ...... /Dev/ttyusb0

 

 

Linux Kernel 2.6.18

Read GPS data directly through ttyusb0

The procedure is as follows:

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <sys/types. h> # include <sys/STAT. h> # include <fcntl. h> # include <unistd. h> # include <termios. h> # include <string. h> int set_opt (int fd, int nspeed, int nbits, char nevent, int nstop) {struct termios newtio, oldtio; If (tcgetattr (FD, & oldtio )! = 0) {perror ("setupserial 1"); Return-1;} bzero (& newtio, sizeof (newtio); newtio. c_cflag | = clocal | cread; newtio. c_cflag & = ~ Csize; Switch (nbits) {Case 7: newtio. c_cflag | = cs7; break; case 8: newtio. c_cflag | = cs8; break;} switch (nevent) {Case 'O': newtio. c_cflag | = parenb; newtio. c_cflag | = parodd; newtio. c_iflag | = (inpck | istrip); break; Case 'E': newtio. c_iflag | = (inpck | istrip); newtio. c_cflag | = parenb; newtio. c_cflag & = ~ Parodd; break; Case 'N': newtio. c_cflag & = ~ Parenb; break;} switch (nspeed) {Case 2400: cfsetispeed (& newtio, b2400); cfsetospeed (& newtio, b2400); break; Case 4800: cfsetispeed (& newtio, b4800); cfsetospeed (& newtio, b4800); break; Case 9600: cfsetispeed (& newtio, b9600); cfsetospeed (& newtio, b9600); break; Case 115200: cfsetispeed (& newtio, b115200); cfsetospeed (& newtio, b115200); break; Case 460800: cfsetispeed (& newtio, b460800); cfsetospeed (& newtio, b460800); brea K; default: cfsetispeed (& newtio, b9600); cfsetospeed (& newtio, b9600); break;} If (nstop = 1) newtio. c_cflag & = ~ Cstopb; else if (nstop = 2) newtio. c_cflag | = cstopb; newtio. c_cc [vtime] = 0; // important newtio. c_cc [Vmin] = 100; // The minimum returned value is tcflush (FD, tciflush); If (tcsetattr (FD, tcsanow, & newtio ))! = 0) {perror ("Com set error"); Return-1 ;}// printf ("set done! \ N \ r "); Return 0;} int main (void) {int fd1, nset1, nread; char Buf [1, 1024]; fd1 = open ("/dev/ttyusb0", o_rdwr); // open the serial port if (fd1 =-1) Exit (1); nset1 = set_opt (fd1, 4800, 8, 'n', 1); // set the serial port attribute if (nset1 =-1) Exit (1); While (1) {memset (BUF ); nread = read (fd1, Buf, 1024); // read the serial port if (nread> 0) {printf ("\ n GPS datalen = % d \ n", nread ); buf [nread] = '\ 0'; printf ("GPS % s \ n", Buf); // output read data} Sleep (2); // sleep, wait for more data} Close (fd1); Return 0 ;}

 

The result of one read is as follows:

GPS datalen = 395

GPS $ gpgga, 000531.979, 0000.0000, N, 00000.0000, E, 50.0, 0.0, 0.0, 0.0, 0000, * 76

$ Gpgsa, A, 1, 50.0, 50.0, 50.0*05

$ Gpgsv, 00,000, 00 * 4A

$ Uplmc, 000531.979, V, 0000.0000, N, 00000.0000, E, 270621, * 14

$ Gpvtg, T, M, N, K * 4E

$ Gpgga, 000532.979, 0000.0000, N, 00000.0000, E, 50.0, 0.0, 0.0, 0.0, M, 0000*75

$ Uplmc, 000532.979, V, 0000.0000, N, 00000.0000, E, 270621, * 17

$ Gpvtg, T, M, N, K * 4E

 

 

When the kernel version is 2.6.32

Run the program. The read data length is 0.

 

Under 3.1.10

When the program is running, the program stops at the open function and cannot run down.

I once suspected that there was a problem with the USB-to-serial port driver. I thought it was not a driver problem. The newer the kernel version, the better the driver. Therefore, the parameters of serial port operation functions in applications are considered.

 

Solution:

Change the OPEN function

Open ("/dev/ttyusb0", o_rdwr | o_nonblock );

The program runs properly.

 

 

Refer:

Http://hi.baidu.com/%CD%FE%B5%C4%C9%FA%BB%EE/blog/item/5cae0e2749c7c71b8a82a135.html

 

Http://linux.chinaunix.net/techdoc/develop/2007/11/15/972357.shtml

 

 

Indicate the source for reprinting. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.