转载请注明:http://blog.csdn.net/wang_zheng_kai
导航制导与控制实验室 2014年11月10日
好久没有写博客了,先从一个小小的程序開始一段新的历程吧。
近期的项目主要还是用的的是linux系统,这篇文章主要介绍怎样从设置、读取BD+gps模块(um220),实际上主要是对串口(UART)的操作。
/* * gps.c * * um220 test * * Author: Wang Zhengkai <449811900@qq.com> * */ #include <stdio.h> #include <termios.h> #include <strings.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h>
/*我用的是ubuntu的电脑測试的,使用的串口是ttyS0*/ #define DEV_NODE "/dev/ttyS0" #define MAX_PACKET_SIZE 1024
</pre><pre code_snippet_id="478189" snippet_file_name="blog_20140930_2_1496439" name="code" class="objc">/************************************************
******* Initialize serial port options***********
************************************************/
static void setTermios(struct termios * pNewtio, int uBaudRate) { bzero(pNewtio, sizeof(struct termios)); /* clear struct for new port settings */ //8N1 pNewtio->c_cflag = uBaudRate | CS8 | CREAD | CLOCAL; pNewtio->c_iflag = IGNPAR; pNewtio->c_oflag = 0; pNewtio->c_lflag = 0; //non ICANON } /*************************************************
**设置um220串口的波特率9600。并刷新使其马上生效***
**************************************************/
void um220_uart_init(int ttyFd,struct termios *oldtio,struct termios *newtio) { tcgetattr(ttyFd, oldtio); /* save current serial port settings */ setTermios(newtio, B9600); tcflush(ttyFd, TCIFLUSH); tcsetattr(ttyFd, TCSANOW, newtio); } /**************************************************
*************Analysis Data of um220****************
***************************************************/ void parseData(char *buf) { int nQ, nN, nB, nC; char cX, cY, cM1, cM2; float fTime, fX, fY, fP, fH, fB, fD; if (buf == NULL) { printf("error: Can't get buf! "); return; } sscanf(buf,"$GNGGA,%f,%f,%c,%f,%c,%d,%02d,%f,%f,%c,%f,%c,%f,%04d%02x",&fTime,&fX,&cX,&fY,&cY,&nQ,&nN,&fP,&fH,&cM1,&fB,&cM2, &fD, &nB, &nC); printf("x: %c %f, y: %c %f, h %f, satellite: %d ",cX, fX, cY, fY, fH, nN);
/*cX:N or S;fX:纬度;cY:E or W;fY:经度;fH:height;nN:卫星个数*/ } int main(void) { int nb,command; int um220_fd = -1; char newbuf[MAX_PACKET_SIZE]; char msg[20],*ret=NULL; struct termios oldtio, newtio; /*Open Um220 Module*/ if ((um220_fd = open(DEV_NODE, O_RDWR)) < 0) { printf("error: Can't open serial port %s! ", DEV_NODE); return -1; } /*Init Uart for Um220*/ um220_uart_init(um220_fd,&oldtio,&newtio); /*Set Um220 options*/ printf("Please select modules of um220 "); printf("1.BD module "); printf("2.GPS module "); printf("3.BD+GPS module "); if(scanf("%d",&command) != 1) { printf("error:input is wrong! "); } switch(command) { case 1: memset(msg, 0, sizeof(msg)); strcpy(msg,"$cfgsys,h01"); if(write(um220_fd,msg,sizeof(msg)) < 0) printf("Failed to set BD modules! "); break; case 2: memset(msg, 0, sizeof(msg)); strcpy(msg,"$cfgsys,h10"); if(write(um220_fd,msg,sizeof(msg)) < 0) printf("Failed to set GPS modules! "); break; case 3: memset(msg, 0, sizeof(msg)); strcpy(msg,"$cfgsys,h11"); if(write(um220_fd,msg,sizeof(msg)) < 0) printf("Failed to set BD+GPS modules! "); break; default: printf("Can't identify command,set BD+GPS modules! "); memset(msg, 0, sizeof(msg)); strcpy(msg,"$cfgsys,h11"); if(write(um220_fd,msg,sizeof(msg)) < 0) printf("Failed to set BD+GPS modules! "); } for(;;) { /*Read Data from Um220*/ memset(newbuf, 0, 1024); nb = read(um220_fd, newbuf, MAX_PACKET_SIZE); if (nb == -1) { perror("read uart error"); return -1; } if ((ret=strstr(newbuf, "$GNGGA")) != NULL) { /*Analysis Data*/ parseData(ret); } sleep(1); } /*Recover Settings Of Serial Port*/ tcsetattr(um220_fd,TCSANOW,&oldtio); /*Close Um220_fd*/ close(um220_fd);
return 0; }