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 头文件
1 #ifndef MAINWINDOW_H
2 #define MAINWINDOW_H
3
4 #include
5 #include
6 #include
7
8 namespace Ui {
9 class MainWindow;
10 }
11
12 class MainWindow : public QMainWindow
13 {
14 Q_OBJECT
15
16 public:
17 explicit MainWindow(QWidget *parent = 0);
18 ~MainWindow();
19
20 public:
21 QSerialPort serial;//串口实例
22 void initSeialPort();//初始化串口函数
23 private:
24 void gpsParse(QByteArray GPSBuffer);//gps解析函数
25
26
27 private slots:
28 void serialRead();
29
30 void on_connectBtn_clicked();
31
32 void on_closeBtn_clicked();
33
34 private:
35 Ui::MainWindow *ui;
36 };
37
38 #endif // MAINWINDOW_H
4.2实现文件
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include
4 #include
5
6 MainWindow::MainWindow(QWidget *parent) :
7 QMainWindow(parent),
8 ui(new Ui::MainWindow)
9 {
10 ui->setupUi(this);
11 QStringList strBaud;
12 strBaud<<"4800"<<"9600"<<"115200";
13 ui->baudcomboBox->addItems(strBaud);
14
15 initSeialPort();
16 }
17
18 MainWindow::~MainWindow()
19 {
20 delete ui;
21 }
22
23 void MainWindow::initSeialPort()
24 {
25 connect(&serial,SIGNAL(readyRead()),this,SLOT(serialRead())); //连接槽
26
27 //获取计算机上所有可用串口并添加到comboBox中
28 QList infos = QSerialPortInfo::availablePorts();
29 if(infos.isEmpty())
30 {
31 ui->portcomboBox->addItem("无串口");
32 return;
33 }
34 foreach (QSerialPortInfo info, infos) {
35 ui->portcomboBox->addItem(info.portName());
36 qDebug() << info.portName();
37 }
38
39 }
40
41
42 void MainWindow::serialRead()
43 {
44 QByteArray qa = serial.readAll();
45
46 ui->textEdit->append(qa);
47
48 gpsParse(qa);
49
50 ui->statusLabel->setText("读取中...");
51 }
52
53 void MainWindow::gpsParse(QByteArray GPSBuffer)
54 {
55
56 // qDebug()< gpsByteArrays = GPSBuffer.split(',');
62 int count = gpsByteArrays.count();
63
64 int gpsLat_1 = static_cast(gpsByteArrays.at(3).toDouble()/100);
65 double gpsLat_2 = (gpsByteArrays.at(3).toDouble() - gpsLat_1 * 100)/60;
66 double gpslat=gpsLat_1 + gpsLat_2;
67
68 int gpsLong_1 = static_cast(gpsByteArrays.at(5).toDouble()/100);
69 double gpsLong_2 = (gpsByteArrays.at(5).toDouble()-gpsLong_1 * 100)/60;
70 double gpsLong = gpsLong_1 + gpsLong_2;
71
72 ui->timelineEdit->setText(gpsByteArrays.at(1));
73 ui->latlineEdit->setText(QString::number(gpslat,'g',9));
74 ui->longlineEdit->setText(QString::number(gpsLong,'g',10));
75
76 if(!gpsByteArrays.at(8).isEmpty())
77 ui->headlineEdit->setText(gpsByteArrays.at(8));
78
79
80 }
81 }
82
83 void MainWindow::on_connectBtn_clicked()
84 {
85 QSerialPortInfo info;
86 QList infos = QSerialPortInfo::availablePorts();
87 int i = 0;
88 foreach (info, infos) {
89 if(info.portName() == ui->portcomboBox->currentText()) break;
90 i++;
91 }
92
93 if(i != infos.size ()){//can find
94 ui->statusLabel->setText("串口打开成功");
95
96 serial.close();
97 serial.setPort(info);
98 serial.open(QIODevice::ReadWrite); //读写打开
99 switch (ui->baudcomboBox->currentIndex()) {
100 case 0:
101 serial.setBaudRate(QSerialPort::Baud4800);
102 break;
103 case 1:
104 serial.setBaudRate(QSerialPort::Baud9600);
105 qDebug()<<"9600";
106 break;
107 case 2:
108 serial.setBaudRate(QSerialPort::Baud115200);
109 qDebug()<<"115200";
110 default:
111 break;
112 }
113
114 // serial.setBaudRate(QSerialPort::Baud9600); //波特率
115 // serial.setDataBits(QSerialPort::Data8); //数据位
116 // serial.setParity(QSerialPort::NoParity); //无奇偶校验
117 // serial.setStopBits(QSerialPort::OneStop); //无停止位
118 // serial.setFlowControl(QSerialPort::NoFlowControl); //无控制
119 }else{
120 serial.close();
121
122 ui->statusLabel->setText("串口打开失败");
123 }
124 }
125
126 void MainWindow::on_closeBtn_clicked()
127 {
128 serial.close();
129 ui->statusLabel->setText("串口断开");
130 }
页面更新:2024-03-01
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号