Linux下使用USB轉串口讀GPS資料

來源:互聯網
上載者:User

 

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

//AUTHOR: lanyang123456

//DATE:2012-02-28

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

 核心版本2.6.18 或2.6.32 或 3.1.10

Linux核心源碼中內建USB轉串口驅動

目錄為drivers/usb/serial,

一般情況下,系統將USB轉串口驅動編譯成核心模組,需要時自動載入到核心中。

將GPS裝置(USB介面)插入到電腦中,系統自動識別裝置,將USB轉為串口,並輸出資訊。

命令列下查看,#dmesg

[  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
[  909.338166] usb 2-4: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[  909.558764] USB Serial support registered for pl2303
[  909.559656] pl2303 2-4:1.0: pl2303 converter detected
[  909.594064] usb 2-4: pl2303 converter now attached to ttyUSB0
[  909.594866] usbcore: registered new interface driver pl2303
[  909.594872] pl2303: Prolific PL2303 USB to serial adaptor driver

 

 

#lsmod 可以查看新增加了模組,pl2303(GPS裝置是pl2303晶片,大約多於70%的GPS裝置是使用Prolific  pl2303 driver這個驅動的。)

 

查看目錄 /dev

通過命令ls -l /dev/ttyUSB0

crw-rw---- 1 root dialout 188,0……/dev/ttyUSB0

 

 

核心2.6.18系統下

直接通過ttyUSB0讀GPS資料

程式如下

#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);break;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;//重要newtio.c_cc[VMIN] = 100;//返回的最小值  重要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[1024];fd1 = open("/dev/ttyUSB0", O_RDWR);//開啟串口if (fd1 == -1)exit(1);nset1 = set_opt(fd1,4800, 8, 'N', 1);//設定串口屬性if (nset1 == -1)exit(1);while(1){memset(buf,0,1024);nread = read(fd1, buf, 1024);//讀串口if (nread > 0){printf("\n GPS DATALen=%d\n",nread); buf[nread] = '\0';printf( "GPS %s\n", buf); //輸出所讀資料}sleep(2);//睡眠,等待資料多一點 }close(fd1);return 0;}

 

一次讀的結果如下

GPS DATALen=395

GPS $GPGGA,000531.979,0000.0000,N,00000.0000,E,0,00,50.0,0.0,M,0.0,M,0.0,0000*76

$GPGSA,A,1,,,,,,,,,,,,,50.0,50.0,50.0*05

$GPGSV,1,1,01,02,00,000,00*4A

$GPRMC,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,0,00,50.0,0.0,M,0.0,M,0.0,0000*75

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

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

 

 

當在核心版本為2.6.32

運行程式,讀到的資料長度為0

 

在3.1.10下

運行程式時,程式停在open函數這,不能往下運行

曾經懷疑是USB轉串口驅動有問題,自己一琢磨,不是驅動的問題,核心版本越新,驅動更完善。所以考慮應用程式中,對串口操作函數的參數問題。

 

解決辦法:

將open 函數改為

open(“/dev/ttyUSB0”,O_RDWR|O_NONBLOCK);

程式運行即可正常。

 

 

參考:

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 

 

 

轉載請註明出處。謝謝!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.