源码
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QSerialPort>
#include<QHBoxLayout>
#include<QDebug>
#include <QByteArray>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_serialPort = new QSerialPort();
this->setWindowTitle("QSerialPort");
m_portNameList = getPortNameList();
ui->comboBox->addItems(m_portNameList);
connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::openPort);
}
//------------------------------------------------------------------------------------
//快捷键设置
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return)
{
on_pushButton_8_clicked();//发送数据
}
}
MainWindow::~MainWindow()
{
if (m_serialPort->isOpen())
{
m_serialPort->close();
}
delete m_serialPort;
delete ui;
}
QStringList MainWindow::getPortNameList()
{
QStringList m_serialPortName;
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
{
m_serialPortName << info.portName();
qDebug()<<"serialPortName:"<<info.portName();
}
return m_serialPortName;
}
//-------------------------------------------------------------------------------
void MainWindow::openPort()
{
if(m_serialPort->isOpen())//如果串口已经打开了 先给他关闭了
{
m_serialPort->clear();
m_serialPort->close();
}
m_serialPort->setPortName(ui->comboBox->currentText());//当前选择的串口名字
if(!m_serialPort->open(QIODevice::ReadWrite))//用ReadWrite 的模式尝试打开串口
{
qDebug()<<"打开失败!";
ui->lineEdit->setText("串口打开失败!");
return;
}
qDebug()<<"串口打开成功!";
ui->lineEdit->setText("串口打开成功!");
//QSerialPort::Baud9600
m_serialPort->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);//设置波特率和读写方向
m_serialPort->setDataBits(QSerialPort::Data8); //数据位为8位
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制
m_serialPort->setParity(QSerialPort::NoParity); //无奇偶校验位
m_serialPort->setStopBits(QSerialPort::OneStop); //一位停止位
m_serialPort->open(QIODevice::ReadWrite);
//connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(receiveInfo()));
connect(m_serialPort,&QSerialPort::readyRead,this,&MainWindow::receiveInfo);
}
//解析接收到数据----------------------------------------------------------------------------------------
void MainWindow::receiveInfo()
{
QByteArray info = m_serialPort->readAll();
int tem[8];
int j=0;
QString strDisplay;
QString str = info.toHex().data();
for(int i = 0;i<str.length();i+=2)
{
QString st = str.mid(i,2);//返回一个字符串 从指定位置返回n个字符
strDisplay += st;
strDisplay += " ";//每个字节中间的空格
QString str_hex = st; //获取十六进制数
int dec = str_hex.toInt(0,16);
qDebug()<<"dec==:"<<dec;
tem[j]=dec;
j++;
}
if(tem[0]<100)
{
ui->lineEdit->setText(strDisplay);
ui->lineEdit_3->setText(strDisplay);
}else
{
QString s=QString::number(tem[0]);
ui->lineEdit_3->setText(s);
ui->lineEdit->setText(s);
ui->lineEdit_2->setText(s);
}
//qDebug()<<"temp==:"<<temp[0];
/*
if(temp[0]==101)
{ ui->lineEdit->setText("激光雷达启动成功!");}
if(temp[0]==102)
{ ui->lineEdit->setText("惯导启动成功!");}
if(temp[0]==103)
{ ui->lineEdit->setText("红外相机启动成功!");}
if(temp[0]==104)
{ ui->lineEdit->setText("正在记录ROS包...");}
if(temp[0]==105)
{ ui->lineEdit->setText("ROS包记录结束!");}
*/
}
//----------------------------------------------------------------------------------
void MainWindow::on_pushButton_2_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0]=101;//启动激光雷达
m_serialPort->write(temp);
ui->lineEdit->setText("正在启动激光雷达");
}
void MainWindow::on_pushButton_3_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0]=102;//启动惯导
m_serialPort->write(temp);
ui->lineEdit->setText("正在启动惯导");
}
void MainWindow::on_pushButton_4_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0]=99;//启动红外相机
m_serialPort->write(temp);
ui->lineEdit->setText("正在启动红外相机");
}
void MainWindow::on_pushButton_5_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0]=104;//开始记录一个ROS包
m_serialPort->write(temp);
ui->lineEdit->setText("正在记录ROS包");
}
void MainWindow::on_pushButton_6_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0] = 105;
/*
temp[1] = 29;
temp[2] = 29;
temp[3] = 29;
temp[4] = 29;
temp[5] = 29;
temp[6] = 29;
temp[7] = 29;
*/
m_serialPort->write(temp);
ui->lineEdit->setText("结束记录");
}
//---------------------------------------------------------------------------
void MainWindow::on_pushButton_8_clicked()
{
QString str=ui->lineEdit_2->text();//获取数据
int dec = str.toInt(0,10);
QByteArray temp;
temp.resize(1);
temp[0] = dec;
m_serialPort->write(temp);
ui->lineEdit->setText("正在发送数据..");
}
//-----------------------------------------------------------------------------
void MainWindow::on_pushButton_9_clicked()
{
QByteArray temp;
temp.resize(8);
temp[0] = 7;
temp[1] = 162;
temp[2] = 117;
temp[3] = 199;
temp[4] = 2;
temp[5] = 193;
temp[6] = 32;
temp[7] = 26;
m_serialPort->write(temp);
ui->lineEdit->setText("已发送坐标plane");
}
void MainWindow::on_pushButton_10_clicked()
{
QByteArray temp;
temp.resize(8);
temp[0] = 7;
temp[1] = 163;
temp[2] = 117;
temp[3] = 199;
temp[4] = 8;
temp[5] = 200;
temp[6] = 253;
temp[7] = 246;
m_serialPort->write(temp);
ui->lineEdit->setText("已发送坐标fire");
}
void MainWindow::on_pushButton_11_clicked()
{
QByteArray temp;
temp.resize(8);
temp[0] = 7;
temp[1] = 162;
temp[2] = 127;
temp[3] = 199;
temp[4] = 2;
temp[5] = 193;
temp[6] = 42;
temp[7] = 26;
m_serialPort->write(temp);
ui->lineEdit->setText("已发送坐标plane");
}
void MainWindow::on_pushButton_12_clicked()
{
QByteArray temp;
temp.resize(8);
temp[0] = 7;
temp[1] = 142;
temp[2] = 117;
temp[3] = 190;
temp[4] = 2;
temp[5] = 193;
temp[6] = 82;
temp[7] = 26;
m_serialPort->write(temp);
ui->lineEdit->setText("已发送坐标plane");
}
void MainWindow::on_pushButton_13_clicked()
{
QByteArray temp;
temp.resize(8);
temp[0] = 7;
temp[1] = 183;
temp[2] = 117;
temp[3] = 159;
temp[4] = 8;
temp[5] = 200;
temp[6] = 243;
temp[7] = 246;
m_serialPort->write(temp);
ui->lineEdit->setText("已发送坐标fire");
}
void MainWindow::on_pushButton_14_clicked()
{
QByteArray temp;
temp.resize(8);
temp[0] = 7;
temp[1] = 173;
temp[2] = 117;
temp[3] = 169;
temp[4] = 8;
temp[5] = 220;
temp[6] = 253;
temp[7] = 206;
m_serialPort->write(temp);
ui->lineEdit->setText("已发送坐标fire");
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QComboBox>
#include <QPushButton>
#include <QMainWindow>
#include<QKeyEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QStringList getPortNameList();//获取可用的串口列表
void openPort();//打开串口
public slots:
void receiveInfo();
protected:
//键盘事件
void keyPressEvent(QKeyEvent *event);
private slots:
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void on_pushButton_6_clicked();
void on_pushButton_8_clicked();
void on_pushButton_9_clicked();
void on_pushButton_10_clicked();
void on_pushButton_11_clicked();
void on_pushButton_12_clicked();
void on_pushButton_13_clicked();
void on_pushButton_14_clicked();
private:
Ui::MainWindow *ui;
QTimer *readTimer;
QSerialPort* m_serialPort; //串口类
QStringList m_portNameList;
};
#endif // MAINWINDOW_H
pro配置
QT += core gui serialport xml
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled_SerialPort
TEMPLATE = app
CONFIG += console c++11
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h \
FORMS += \
mainwindow.ui
DISTFILES += \
界面搭建
注意
ubuntu18,通过指令安装qt时使用qt串口容易出错:
QT Unknown module(s) in QT: serialport
解决:
sudo apt-get install libqt5serialport5
sudo apt-get install libqt5serialport5-dev