Qt - 读取GPS数据
1.GPS型号为ublox(EVK-M8L),配有USB接口,Qt版本5.7
2.实现步骤:
(1)实现串口通信
采用Qt5.7 内置的串口操作类QSerialPort和QSerialPortInfo,通过QSerialPortInfo提供的函数availablePorts(),可枚举出当前计算机中可用的com口。使用该类需在pro文件中添加:
QT += serialport
(2)筛选感兴趣的信号,解析
GPRMC数据包基本上包含经纬度、航向角、时间等常用的信号。
3.效果图

4.源码
4.1 头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QSerialPort>
#include <QSerialPortInfo> namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow(); public:
QSerialPort serial;//串口实例
void initSeialPort();//初始化串口函数
private:
void gpsParse(QByteArray GPSBuffer);//gps解析函数 private slots:
void serialRead(); void on_connectBtn_clicked(); void on_closeBtn_clicked(); private:
Ui::MainWindow *ui;
}; #endif // MAINWINDOW_H
4.2实现文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include <QList> MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList strBaud;
strBaud<<""<<""<<"";
ui->baudcomboBox->addItems(strBaud); initSeialPort();
} MainWindow::~MainWindow()
{
delete ui;
} void MainWindow::initSeialPort()
{
connect(&serial,SIGNAL(readyRead()),this,SLOT(serialRead())); //连接槽 //获取计算机上所有可用串口并添加到comboBox中
QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
if(infos.isEmpty())
{
ui->portcomboBox->addItem("无串口");
return;
}
foreach (QSerialPortInfo info, infos) {
ui->portcomboBox->addItem(info.portName());
qDebug() << info.portName();
} } void MainWindow::serialRead()
{
QByteArray qa = serial.readAll(); ui->textEdit->append(qa); gpsParse(qa); ui->statusLabel->setText("读取中...");
} void MainWindow::gpsParse(QByteArray GPSBuffer)
{ // qDebug()<<GPSBuffer.size(); if(GPSBuffer.contains("$GNRMC") )
{ QList<QByteArray> gpsByteArrays = GPSBuffer.split(',');
int count = gpsByteArrays.count(); int gpsLat_1 = static_cast<int>(gpsByteArrays.at().toDouble()/);
double gpsLat_2 = (gpsByteArrays.at().toDouble() - gpsLat_1 * )/;
double gpslat=gpsLat_1 + gpsLat_2; int gpsLong_1 = static_cast<int>(gpsByteArrays.at().toDouble()/);
double gpsLong_2 = (gpsByteArrays.at().toDouble()-gpsLong_1 * )/;
double gpsLong = gpsLong_1 + gpsLong_2; ui->timelineEdit->setText(gpsByteArrays.at());
ui->latlineEdit->setText(QString::number(gpslat,'g',));
ui->longlineEdit->setText(QString::number(gpsLong,'g',)); if(!gpsByteArrays.at().isEmpty())
ui->headlineEdit->setText(gpsByteArrays.at()); }
} void MainWindow::on_connectBtn_clicked()
{
QSerialPortInfo info;
QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
int i = ;
foreach (info, infos) {
if(info.portName() == ui->portcomboBox->currentText()) break;
i++;
} if(i != infos.size ()){//can find
ui->statusLabel->setText("串口打开成功"); serial.close();
serial.setPort(info);
serial.open(QIODevice::ReadWrite); //读写打开
switch (ui->baudcomboBox->currentIndex()) {
case :
serial.setBaudRate(QSerialPort::Baud4800);
break;
case :
serial.setBaudRate(QSerialPort::Baud9600);
qDebug()<<"";
break;
case :
serial.setBaudRate(QSerialPort::Baud115200);
qDebug()<<"";
default:
break;
} // serial.setBaudRate(QSerialPort::Baud9600); //波特率
// serial.setDataBits(QSerialPort::Data8); //数据位
// serial.setParity(QSerialPort::NoParity); //无奇偶校验
// serial.setStopBits(QSerialPort::OneStop); //无停止位
// serial.setFlowControl(QSerialPort::NoFlowControl); //无控制
}else{
serial.close(); ui->statusLabel->setText("串口打开失败");
}
} void MainWindow::on_closeBtn_clicked()
{
serial.close();
ui->statusLabel->setText("串口断开");
}
Qt - 读取GPS数据的更多相关文章
- GPS数据读取与处理
GPS数据读取与处理 GPS模块简介 SiRF芯片在2004年发布的最新的第三代芯片SiRFstar III(GSW 3.0/3.1),使得民用GPS芯片在性能方面登上了一个顶峰,灵敏度比以前的产品大 ...
- Qt读取JSON和XML数据
QJSON JSON(JavaScript Object Notation)是一个轻量级的数据交换格式; 可以将数据以name/value的形式任意组合; QJson 是一个基于Qt的库, 将JSON ...
- Qt通过odbc读取excel数据
传统的读取方式是通过Excel.Application,这种方式不仅操作繁琐,而且速度也不快. 通过odbc读取,可以使用select语句直接读取整个工作表,处理excel数据就跟数据库一样方便. 当 ...
- Qt监控Arduino开关状态(读取串口数据)
setup.ini配置文件内容 [General] #游戏所在主机IP GameIp1=192.168.1.151 GameIp2=192.168.1.152 GameIp3=192.168.1.15 ...
- 采集的GPS数据如何正确显示在arcgis和cad中
利用GPS定位卫星,在全球范围内实时进行定位.导航的系统,称为全球卫星定位系统,简称GPS.GPS是由美国国防部研制建立的一种具有全方位.全天候.全时段.高精度的卫星导航系统,能为全球用户提供低成本. ...
- 格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: GetLzdtArticleResult。InnerException 消息是“反序列化对象 属于类型 lzdt.DTO.Dtolzdt[] 时出现错误。读取 XML 数据时,超出最大
当遇到这个错误的时候郁闷了好长时间报错是字符串长度过大可是修改了MaxStringContentLength”属性的值却不起作用最后才发现还是因为配置文件配置的问题在服务端 格式化程序尝试对消息反序列 ...
- 使用 Qt 获取 UDP 数据并显示成图片(2)
本文首发于 BriFuture 的 个人博客 在我的前一篇文章 使用 Qt 获取 UDP 数据并显示成图片 中,我讲了如何用 Python 模拟发送数据,如何在 Qt 中高效的接收 UDP 数据包并将 ...
- java的poi技术读取Excel数据到MySQL
这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...
- Hive读取外表数据时跳过文件行首和行尾
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 有时候用hive读取外表数据时,比如csv这种类型的,需要跳过行首或者行尾一些和数据无关的或者自 ...
随机推荐
- [LeetCode] Repeated Substring Pattern 重复子字符串模式
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 在ASP.NET Core中实现一个Token base的身份认证
注:本文提到的代码示例下载地址> How to achieve a bearer token authentication and authorization in ASP.NET Core 在 ...
- WebAPI中无法获取Session对象的解决办法
在MVC的WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型 public override void Init() { PostAut ...
- grouping sets从属子句的运用
grouping sets主要是用来合并多个分组的结果. 对于员工目标业绩表'businessTarget': employeeId targetDate idealDistAmount 如果需要分别 ...
- table表框去掉相邻的间隔
HTML <table> <tr> <th>名称</th> <th>描述</th> </tr> <tr> ...
- Java学习笔记(四)
字符串 字符串应用主要分为String类操作与字符串生成器 在程序中频繁的进行附加字符串则使用字符串生成器StringBuilder 数组 概述 数组是具有相同数据类型的一组数据的集合 数组创建 先声 ...
- Mysql数据库 - 增删改
一. Create 1. 单条插入, sql格式: insert into (列名) values(列值); INSERT INTO test.tch_teacher ( Sex, BId, NO, ...
- java中关键字volatile的作用
用在多线程,同步变量. 线程为了提高效率,将某成员变量(如A)拷贝了一份(如B),线程中对A的访问其实访问的是B.只在某些动作时才进行A和B的同步.因此存在A和B不一致的情况.volatile就是用来 ...