《Linux下使用USB转串口读GPS数据.doc》由会员分享,可在线阅读,更多相关《Linux下使用USB转串口读GPS数据.doc(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、/-/AUTHOR: lanyang/DATE:2012-02-28/-内核版本2.6.18 或2.6.32 或 3.1.10Linux内核源码中自带USB转串口驱动目录为drivers/usb/serial,一般情况下,系统将USB转串口驱动编译成内核模块,需要时自动加载到内核中。将GPS设备(USB接口)插入到计算机中,系统自动识别设备,将USB转为串口,并输出信息。命令行下查看,#dmesg 909. usb 2-4: new full speed USB device number 5 using ohci_hcd 909. usb 2-4: New USB device found,
2、 idVendor=067b, idProduct=2303 909. usb 2-4: New USB device strings: Mfr=0, Product=0, SerialNumber=0 909. USB Serial support registered for pl2303 909. pl2303 2-4:1.0: pl2303 converter detected 909. usb 2-4: pl2303 converter now attached to ttyUSB0 909. usbcore: registered new interface driver pl23
3、03 909. pl2303: Prolific PL2303 USB to serial adaptor driver#lsmod 可以查看新增加了模块,pl2303(GPS设备是pl2303芯片),大约多于70%的GPS设备是使用Prolific pl2303driver这个驱动的。查看目录 /dev通过命令ls -l /dev/ttyUSB0crw-rw- 1 root dialout 188,0/dev/ttyUSB0内核2.6.18系统下直接通过ttyUSB0读GPS数据程序如下/-#include#include#include#include#include#include#in
4、clude#include#includeint 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:newti
5、o.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 &= PAR
6、ENB;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 :cfsetispeed(&newtio, B);cfsetospeed(&newtio, B);brea
7、k;case :cfsetispeed(&newtio, B);cfsetospeed(&newtio, B);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_ccVTIME = 0;/重要newtio.c_ccVMIN = 100;/返回的最小值 重要tcflush(fd,TCIFLUSH);if(tc
8、setattr(fd,TCSANOW,&newtio)!=0)perror(com set error);return -1;/printf(set done!nr);return 0;int main(void)int fd1,nset1,nread;char buf1024;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 = r
9、ead(fd1, buf, 1024);/读串口if (nread 0)printf(n GPS DATALen=%dn,nread); bufnread = 0;printf( GPS %sn, buf); /输出所读数据sleep(2);/睡眠,等待数据多一点 close(fd1);return 0;一次读的结果如下GPS DATALen=395GPS $GPGGA,.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,00
10、0,00*4A$GPRMC,.979,V,0000.0000,N,00000.0000,E,*14$GPVTG,T,M,N,K*4E$GPGGA,.979,0000.0000,N,00000.0000,E,0,00,50.0,0.0,M,0.0,M,0.0,0000*75$GPRMC,.979,V,0000.0000,N,00000.0000,E,*17$GPVTG,T,M,N,K*4E/-当在内核版本为2.6.32运行程序,读到的数据长度为0在3.1.10下运行程序时,程序停在open函数这,不能往下运行曾经怀疑是USB转串口驱动有问题,自己一琢磨,不是驱动的问题,内核版本越新,驱动更完善。所以考虑应用程序中,对串口操作函数的参数问题。解决办法:将open 函数改为open(“/dev/ttyUSB0”,O_RDWR|O_NONBLOCK);程序运行即可正常。