从串口设置、读取、并分析um220模块的数据
转载请注明: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模块的数据的更多相关文章
- nginx源码分析之模块初始化
在nginx启动过程中,模块的初始化是整个启动过程中的重要部分,而且了解了模块初始化的过程对应后面具体分析各个模块会有事半功倍的效果.在我看来,分析源码来了解模块的初始化是最直接不过的了,所以下面主要 ...
- 智能设备逆向工程之外部Flash读取与分析篇
智能设备逆向工程之外部Flash读取与分析篇 唐朝实验室 · 2015/10/19 11:19 author: rayxcp 0x00 前言 目前智能家居设备的种类很多,本文内容以某智能豆浆机为例完成 ...
- Linux串口设置及编程(转)
用户常见的数据通信的基本方式可分为并行通信和串行通信. 并行通信是指利用多条数据传输线将一个资料的各位同时传送.特点是传输速度快,适用于短距离通信,但要求传输速度较高的应用场合. 串行通信是指利用一条 ...
- Swoole 源码分析——基础模块之 Pipe 管道
前言 管道是进程间通信 IPC 的最基础的方式,管道有两种类型:命名管道和匿名管道,匿名管道专门用于具有血缘关系的进程之间,完成数据传递,命名管道可以用于任何两个进程之间.swoole 中的管道都是匿 ...
- Linux下打开串口设置
给出打开串口函数 int open_tty(char tty[]) { int fd; char tty_path[32]={0}; sprintf(tty_path,"/dev/%s&qu ...
- poll函数和串口设置
2015.1.24 今天星期六,多云,早晨8:17起床的,今天是来南京起床最迟的一天,因为昨晚睡得有点迟,今天又不用上课,整个人有点放松.收拾好来到教室,教室门没有开,胡明也到了,其他人还在宿舍睡觉, ...
- zedboard 中SDK 修改串口设置(波特率。。。。)
其实在zedboard SDK中不用初始化串口的也就是platform()可以不写 ,初始化在EDK导入SDK中就写好了 具体看bsp文件夹下面的汇编.但是如果我们想要在SDK中改变串口设置的话 ...
- nginx源码分析——http模块
源码:nginx 1.12.0 一.nginx http模块简介 由于nginx的性能优势,现在已经有越来越多的单位.个人采用nginx或者openresty. ...
- 串口接收端verilog代码分析
串口接收端verilog代码分析 `timescale 1ns / 1ps ////////////////////////////////////////////////////////////// ...
随机推荐
- POJ 1664 放苹果【DFS】
题意:给出n个苹果,m个盘子,问有多少种不同的苹果放置方法 可以把它抽象为把一个数n,拆分成a1,a2,a3,---,am,使得它们的和为n, 话说这一题是学习的ppt里面的,它的思路就是搜索 搜索条 ...
- swift语言点评二十一-协议
定义有什么,及哪些必须实现. A protocol defines a blueprint of methods, properties, and other requirements that su ...
- SQL Server查询死锁,杀死进程解决死锁
查询死锁进程和表 SELECT request_session_id AS spid , OBJECT_NAME(resource_associated_entity_id) AS 'table' F ...
- 企业微信H5支付返回不到自己指定的结果页面
项目:vue做的H5项目,嵌入到企业微信里 发现的问题:支付完成后的跳转页面 跳转不到自己指定的页面 问题在于:调支付接口时,redirectUrl: document.location.protoc ...
- tab栏切换
最简单的tab栏切换 html部分 <ul class="tab"> <li class="item">待支付(1)</li> ...
- eclipse历史版本下载地址
http://wiki.eclipse.org/Older_Versions_Of_Eclipse
- luogu P1495 曹冲养猪(中国剩余定理)
题意 题解 翻到了一个金句 就跟这句话说得一样,就是个裸题. 所以看模板呗. #include<iostream> #include<cstring> #include< ...
- PID的原理
来源:https://www.cnblogs.com/foxclever/p/8902029.html 在自动控制中,PID及其衍生出来的算法是应用最广的算法之一.各个做自动控制的厂家基本都有会实现这 ...
- idea 包的显示方式
idea 可以通过点击Project的导航栏里的小齿轮里面有一个 Flatten packages 选项,将其勾上.就可以得到跟eclipse一样的包的显示方式. 没有设置默认是这样的 2018-06 ...
- Objective-C的陷阱与缺陷
Objective-C是一个强大而且非常有用的语言,但是同样也是有一点危险的.这次主题是受到一篇有关C++陷阱的文章启发,来聊聊Objective-C和Cocoa中的陷阱. 简介 我将和Horstma ...