文章目录
- 前言
- 具体方法
- 总结
前言
前一篇文章有讲GPS的NMEA-0813协议报文具体解析,但是前提是需要先读取串口获取报文,此篇主要讲如何获取串口的数据,即读取串口。
具体方法
头文件:
#ifndef GPSPositionWidget_H #define GPSPositionWidget_H #includeQT_BEGIN_NAMESPACE namespace Ui {class CGPSPositionWidget;} QT_END_NAMESPACE class QSerialPort; class CGPSPositionWidget : public QWidget { Q_OBJECT public: CGPSPositionWidget(QWidget *parent = 0); ~CGPSPositionWidget(); public: void Initialize(); bool SetPara(); public slots: void SlotPushButton_Switch(); void SlotSerialRead(); private: Ui::CGPSPositionWidget* ui; QSerialPort *m_pserial; }; #endif // GPSPositionWidget_H
源文件:
#include "GPSPositionWidget.h" #include "ui_GPSPositionWidget.h" #include#include #include "qdebug.h" #include "AcBaseWidget/Include/dlgmessagebox.h" CGPSPositionWidget::CGPSPositionWidget(QWidget *parent) , m_pserial(NULL) { ui = new Ui::CGPSPositionWidget(); ui->setupUi(panel); setTitle(ConvertCharArrayToQString("GPS定位")); connect(ui->switchButton,SIGNAL(clicked()),this,SLOT(SlotPushButton_Switch())); Initialize(); } CGPSPositionWidget::~CGPSPositionWidget() { delete ui; ui = nullptr; } void CGPSPositionWidget::Initialize() { ui->portComboBox->clear(); ui->baudComboBox->clear(); ui->dataBitsComboBox->clear(); ui->stopBitsComboBox->clear(); ui->parityComboBox->clear(); ui->flowControlComboBox->clear(); ui->modeComboBox->clear(); //状态栏清空 ui->statusLabel->clear(); //获取计算机上所有可用串口并添加到comBox中 QList infos = QSerialPortInfo::availablePorts(); if(infos.isEmpty()) { ui->portComboBox->addItem(ConvertCharArrayToQString("无串口")); return; } else { m_pserial = new QSerialPort; } foreach (QSerialPortInfo info, infos) { ui->portComboBox->addItem(info.portName()); qDebug() << info.portName(); } //设置三种波特率 QStringList strBaud; strBaud< baudComboBox->addItems(strBaud); ui->baudComboBox->setCurrentIndex(1); //设置四种数据位 QStringList strData; strData< dataBitsComboBox->addItems(strData); ui->dataBitsComboBox->setCurrentIndex(3); //设置三种停止位 QStringList strStop; strStop< stopBitsComboBox->addItems(strStop); ui->stopBitsComboBox->setCurrentIndex(0); //设置五种校验位 QStringList strParity; strParity< parityComboBox->addItems(strParity); ui->parityComboBox->setCurrentIndex(0); //设置四种流控 QStringList strFlowControl; strFlowControl< flowControlComboBox->addItems(strFlowControl); ui->flowControlComboBox->setCurrentIndex(0); //设置三种定位模式 QStringList strMode; strMode< modeComboBox->addItems(strMode); ui->modeComboBox->setCurrentIndex(0); } bool CGPSPositionWidget::SetPara() { if (m_pserial == NULL) { ui->statusLabel->setText(ConvertCharArrayToQString("无串口!")); return false; } //设置串口名字 m_pserial->setPortName(ui->portComboBox->currentText()); //设置波特率 m_pserial->setBaudRate(ui->baudComboBox->currentText().toInt()); //设置数据位 switch (ui->flowControlComboBox->currentIndex()) { case 0: { m_pserial->setDataBits(QSerialPort::Data5); } break; case 1: { m_pserial->setDataBits(QSerialPort::Data6); } break; case 2: { m_pserial->setDataBits(QSerialPort::Data7); } break; case 3: { m_pserial->setDataBits(QSerialPort::Data8); } break; default: { m_pserial->setDataBits(QSerialPort::Data8); } break; } //设置停止位 switch (ui->flowControlComboBox->currentIndex()) { case 0: { m_pserial->setStopBits(QSerialPort::OneStop); } break; case 1: { m_pserial->setStopBits(QSerialPort::OneAndHalfStop); } break; case 2: { m_pserial->setStopBits(QSerialPort::TwoStop); } break; default: { m_pserial->setStopBits(QSerialPort::OneStop); } break; } //设置奇偶校验位 switch (ui->parityComboBox->currentIndex()) { case 0: { m_pserial->setParity(QSerialPort::NoParity); } break; case 1: { m_pserial->setParity(QSerialPort::OddParity); } break; case 2: { m_pserial->setParity(QSerialPort::EvenParity); } break; case 3: { m_pserial->setParity(QSerialPort::MarkParity); } break; case 4: { m_pserial->setParity(QSerialPort::SpaceParity); } break; default: { m_pserial->setParity(QSerialPort::NoParity); } break; } //设置流控 switch (ui->flowControlComboBox->currentIndex()) { case 0: { m_pserial->setFlowControl(QSerialPort::NoFlowControl); } break; case 1: { m_pserial->setFlowControl(QSerialPort::HardwareControl); } break; case 2: { m_pserial->setFlowControl(QSerialPort::SoftwareControl); } break; default: { m_pserial->setFlowControl(QSerialPort::NoFlowControl); } break; } return true; } void CGPSPositionWidget::SlotPushButton_Switch() { if (ui->switchButton->text() == ConvertCharArrayToQString("打开串口")) { //状态栏清空 ui->statusLabel->clear(); //设置参数 if (!SetPara()) { return; } //打开串口 if (m_pserial->open(QIODevice::ReadWrite)) { //ui->modeComboBox->setEditable(false); m_pGPSMapEventHandler->CreatePLineLayer(); connect(m_pGPSMapEventHandler, &CGPSMapEventHandler::SigCurrent2DObjectDestroyed, this, &CGPSPositionWidget::SlotCurrent2DObjectDestroyed); ui->switchButton->setText(ConvertCharArrayToQString("关闭串口")); //检测点和轨迹显隐状态 SlotCheckBox_Show(); if (ui->showTraCheckBox->isChecked()) { m_pGPSMapEventHandler->SetTraVisible(true); } else { m_pGPSMapEventHandler->SetTraVisible(false); } //SlotCheckBox_ShowTra(); //信号与槽函数关联 connect(m_pserial,SIGNAL(readyRead()),this,SLOT(SlotSerialRead())); //连接槽 } else { DLgMessageBox dlgMsg(this); dlgMsg.SetMessage(ConvertCharArrayToQString("串口打开失败!"), MS_Information); dlgMsg.exec(); } } else { //关闭串口 m_pserial->close(); //恢复设置功能 ui->switchButton->setText(ConvertCharArrayToQString("打开串口")); ui->timelineEdit->clear(); ui->latlineEdit->clear(); ui->lonlineEdit->clear(); ui->elevationlineEdit->clear(); //m_pGPSMapEventHandler->SetTraVisible(false); //ui->showTraCheckBox->setChecked(false); //SlotCheckBox_ShowTra(); //ui->saveTraCheckBox->setEnabled(true); ui->statusLabel->setText(ConvertCharArrayToQString("串口关闭")); } } void CGPSPositionWidget::SlotSerialRead() { QByteArray qa = m_pserial->readAll(); GPSParse(qa); ui->statusLabel->setText(ConvertCharArrayToQString("读取中...")); }
总结
具体效果如下:
注:只提供了读取相关代码。



