转载请注明: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!\n");
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\n",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!\n", DEV_NODE);
return -1;
} /*Init Uart for Um220*/
um220_uart_init(um220_fd,&oldtio,&newtio); /*Set Um220 options*/
printf("Please select modules of um220\n");
printf("1.BD module\n");
printf("2.GPS module\n");
printf("3.BD+GPS module\n");
if(scanf("%d",&command) != 1)
{
printf("error:input is wrong!\n");
}
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!\n");
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!\n");
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!\n");
break;
default:
printf("Can't identify command,set BD+GPS modules!\n");
memset(msg, 0, sizeof(msg));
strcpy(msg,"$cfgsys,h11");
if(write(um220_fd,msg,sizeof(msg)) < 0)
printf("Failed to set BD+GPS modules!\n");
} 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;
}

最新文章

  1. linux系统中查看系统位数(转载)
  2. BZOJ 1142: [POI2009]Tab
  3. php大力力 [019节]php分页类的学习
  4. UVA 11427 Expect the Expected (期望)
  5. Android init.rc文件格式解析
  6. 常用Linux运维命令
  7. Lua 第一个应用程序 Hello World
  8. Android调用系统的Activity、ContentProvider、Service、Broadcast Receiver
  9. 一維條碼 EAN13 的編碼方式
  10. golang中的reflect包用法
  11. cxf整合spring错误为:cvc-complex-type.2.4.c
  12. 【★】RSA-什么是不对称加密算法?
  13. Java零碎知识点
  14. go源文件中是否有main函数
  15. 解决WebMagic抓HTTPS时出现SSLException
  16. ImCash是骗局吗?到底是真是假?
  17. mybatis基础(中)
  18. 吴恩达机器学习笔记44-核函数(Kernels)
  19. How Tomcat works — 一、怎样阅读源码
  20. 模拟银行ATM系统(基础版)

热门文章

  1. The Structure of an App-ios应用架构-MVC
  2. Nginx域名配置文件bak
  3. Node_进阶_6
  4. iOS面试总结(待完善)
  5. [TJOI2015]弦论(后缀数组or后缀自动机)
  6. [HDU5686]2016&quot;百度之星&quot; - 资格赛 Problem B
  7. BZOJ 1951 [SDOI2010]古代猪文 (组合数学+欧拉降幂+中国剩余定理)
  8. CSS 子元素选择器
  9. JAVA jsp page指令的属性 errorPage 和isErrorPage
  10. C#打开或者创建一个文件,然后向其末尾写入数据的方法