转载请注明: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;
}

从串口设置、读取、并分析um220模块的数据的更多相关文章

  1. nginx源码分析之模块初始化

    在nginx启动过程中,模块的初始化是整个启动过程中的重要部分,而且了解了模块初始化的过程对应后面具体分析各个模块会有事半功倍的效果.在我看来,分析源码来了解模块的初始化是最直接不过的了,所以下面主要 ...

  2. 智能设备逆向工程之外部Flash读取与分析篇

    智能设备逆向工程之外部Flash读取与分析篇 唐朝实验室 · 2015/10/19 11:19 author: rayxcp 0x00 前言 目前智能家居设备的种类很多,本文内容以某智能豆浆机为例完成 ...

  3. Linux串口设置及编程(转)

    用户常见的数据通信的基本方式可分为并行通信和串行通信. 并行通信是指利用多条数据传输线将一个资料的各位同时传送.特点是传输速度快,适用于短距离通信,但要求传输速度较高的应用场合. 串行通信是指利用一条 ...

  4. Swoole 源码分析——基础模块之 Pipe 管道

    前言 管道是进程间通信 IPC 的最基础的方式,管道有两种类型:命名管道和匿名管道,匿名管道专门用于具有血缘关系的进程之间,完成数据传递,命名管道可以用于任何两个进程之间.swoole 中的管道都是匿 ...

  5. Linux下打开串口设置

    给出打开串口函数 int open_tty(char tty[]) { int fd; char tty_path[32]={0}; sprintf(tty_path,"/dev/%s&qu ...

  6. poll函数和串口设置

    2015.1.24 今天星期六,多云,早晨8:17起床的,今天是来南京起床最迟的一天,因为昨晚睡得有点迟,今天又不用上课,整个人有点放松.收拾好来到教室,教室门没有开,胡明也到了,其他人还在宿舍睡觉, ...

  7. zedboard 中SDK 修改串口设置(波特率。。。。)

    其实在zedboard   SDK中不用初始化串口的也就是platform()可以不写 ,初始化在EDK导入SDK中就写好了  具体看bsp文件夹下面的汇编.但是如果我们想要在SDK中改变串口设置的话 ...

  8. nginx源码分析——http模块

         源码:nginx 1.12.0      一.nginx http模块简介           由于nginx的性能优势,现在已经有越来越多的单位.个人采用nginx或者openresty. ...

  9. 串口接收端verilog代码分析

    串口接收端verilog代码分析 `timescale 1ns / 1ps ////////////////////////////////////////////////////////////// ...

随机推荐

  1. shell-4.bash的变量:用户自定义变量

    目录 内容

  2. Python 曲线拟合

    #曲线拟合 fig = plt.figure() ax = fig.add_subplot(111)#将画布分割成1行1列,图像画在从左到右从上到下的第1块 ax.plot(Num,a,label=u ...

  3. vue循环遍历给div添加id

    html部分 <div class="img-preview" v-for="(img,i) of list" :key="img.imageK ...

  4. NodeJS学习笔记 (5)网络服务-http-req(ok)

    原文:https://github.com/chyingp/nodejs-learning-guide 自己敲代码: 概览 本文的重点会放在req这个对象上.前面已经提到,它其实是http.Incom ...

  5. (2016北京集训十)【xsy1528】azelso - 概率期望dp

    北京集训的题都是好题啊~~(于是我爆0了) 注意到一个重要的性质就是期望是线性的,也就是说每一段的期望步数可以直接加起来,那么dp求出每一段的期望就行了... 设$f_i$表示从$i$出发不回到$i$ ...

  6. 【BZOJ4176】Lucas的数论-杜教筛

    求$$\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}f(ij)$$,其中$f(x)$表示$x$的约数个数,$0\leq n\leq 10^9$,答案膜$10^9+ ...

  7. TP5 错误信息提示入坑指南

    查遍了百度,基本都是在 config.php 开启调试 然后还有一个错误信息提示 然后做完这些以后,很神奇的事情发生了! 那就是居然没有任何鬼用.依旧是提示页面错误!什么鬼信息都没有! 然后发现在  ...

  8. [Typescript] Promise based delay function using async / await

    Learn how to write a promise based delay function and then use it in async await to see how much it ...

  9. Android 获取麦克风的音量(分贝)

    基础知识 度量声音强度.大家最熟悉的单位就是分贝(decibel,缩写为dB).这是一个无纲量的相对单位.计算公式例如以下: 分子是測量值的声压,分母是參考值的声压(20微帕.人类所能听到的最小声压) ...

  10. JConsole远程监控Tomcat7

    下面技术应用于最优质的水果的鲜果篮 一.设置服务端: 1.增加Listener到conf/server.xml <Listener className="org.apache.cata ...