Linux下使用USB转串口读GPS数据

时间:2022-05-09 02:34:28
 

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

//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 

 

 

转载请注明出处。谢谢!