Qt - 讀取GPS數據

1.GPS型號爲ublox(EVK-M8L),配有USB接口,Qt版本5.7app

2.實現步驟:函數

(1)實現串口通訊ui

  採用Qt5.7 內置的串口操做類QSerialPortQSerialPortInfo,經過QSerialPortInfo提供的函數availablePorts(),可枚舉出當前計算機中可用的com口。使用該類需在pro文件中添加:this

QT += serialportspa

(2)篩選感興趣的信號,解析code

  GPRMC數據包基本上包含經緯度、航向角、時間等經常使用的信號。blog

3.效果圖接口

4.源碼ci

4.1 頭文件get

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QSerialPort>
 6 #include <QSerialPortInfo>
 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<QDebug>
  4 #include <QList>
  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<QSerialPortInfo>  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()<<GPSBuffer.size();
 57 
 58     if(GPSBuffer.contains("$GNRMC") )
 59     {
 60 
 61         QList<QByteArray> gpsByteArrays = GPSBuffer.split(',');
 62         int count = gpsByteArrays.count();
 63 
 64     int  gpsLat_1 = static_cast<int>(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<int>(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<QSerialPortInfo> 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 }
相關文章
相關標籤/搜索