转载请注明: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. c++string类的简单介绍

    #include "iostream" #include "string" using namespace std; /*@author:浅滩 *family: ...

  2. TP5 分页类,自定义样式

    结合X-admin 后台框架 在做项目,为了保持分页风格的一致,所以需要自定义 一个分页类. 一.在项目的 extend 目录,创建 cus 目录 二.创建 Page 分页类,代码如下 <?ph ...

  3. 51nod 1321 收集点心(最小割)

    给出一种最小割的方法. 设\(num1[i]\),\(num2[i]\)为第i种形状的点心的两种口味的数量 设\(type[i]\),\(type[i]\)为第i种形状的点心的两种口味 假设\(num ...

  4. luogu P1592 互质(欧拉函数)

    题意 (n<=106,k<=108) 题解 一开始以为是搜索. 但想想不对,翻了一眼题解发现是欧拉函数. 因为 gcd(a,b)=gcd(a,a+b) 所以和n互质的数应该是类似a1,a2 ...

  5. codeforces 914 D Bash and a Tough Math Puzzle

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #i ...

  6. tree编译

    没有tree命令,就需要下载源代码 [root@fyc tree-1.7.0]#cd /opt/src [root@fyc tree-1.7.0]# wget ftp://mama.indstate. ...

  7. LINUX命令LS -AL 解析

    LINUX命令LS -AL 解析 linux命令ls -al 解析 ls是“list”的意思,与早期dos的命令dir功能类似.参数-al则表示列出所有的文件,包括隐藏文件,就是文件前面第一个字符为. ...

  8. 洛谷 P1088 火星人 (全排列)

    直接调用next_permutation即可,向前的话可以调用prev_permutation #include<cstdio> #include<cctype> #inclu ...

  9. [javase学习笔记]-6.7 封装

    这一节我们学习面向对象中的第一个特性,封装(encapsulation) 封装:是指隐藏对象的发生和实现细节,仅对外提供公共訪问方式. 那么什么是隐藏对象的实现细节? 我们来举例学习. 比方我们来定义 ...

  10. 深入理解Android之Java虚拟机Dalvik

    一.背景 这个选题非常大,但并非一開始就有这么高大上的追求. 最初之时,仅仅是源于对Xposed的好奇.Xposed差点儿是定制ROM的神器软件技术架构或者说方法了. 它究竟是怎么实现呢?我本意就是想 ...