使用QtSerialPort实现串口收发数据,具体代码如下:
- 头文件:
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
#ifndef SERIALPORTTOOLVIEW_H
#define SERIALPORTTOOLVIEW_H
#include <QObject>
#include <QFrame>
#include <QtSerialPort/QtSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QTextBrowser>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
class SerialPortToolView : public QFrame
{
Q_OBJECT
public:
SerialPortToolView(QWidget *parent = nullptr);
void setupUI();
private:
void openSerialPort();
void closeSerialPort();
void clearBtnClicked();
void closeBtnClicked();
void sendCmdData();
void readyRead();
protected:
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
private:
QTextBrowser *m_textBrowser = Q_NULLPTR;
QSerialPort *m_serialPort = Q_NULLPTR;
QLineEdit *m_lineEdit = Q_NULLPTR;
QComboBox *m_combSerialPortName= Q_NULLPTR;
QPushButton *m_pSerialPortOpen = Q_NULLPTR;
QPushButton *m_pSerialPortClose = Q_NULLPTR;
QPoint m_beginDrag = QPoint(-1,-1);
};
#endif // SERIALPORTTOOLVIEW_H
- cpp文件
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
#include "SerialPortToolView.h"
#include <QDebug>
#include <QHBoxLayout>
#include <QListView>
#include <QMouseEvent>
#include <QDesktopWidget>
#include <QApplication>
SerialPortToolView::SerialPortToolView(QWidget *parent)
:QFrame(parent)
{
// this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
this->setFixedSize(1150,700);
}
void SerialPortToolView::setupUI()
{
if (m_serialPort == Q_NULLPTR)
{
m_serialPort = new QSerialPort(this);
QObject::connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortToolView::readyRead);
}
QString strBtnStyleSheet = QString("QPushButton#serialPortBtn{font-size:22px;}"
"QPushButton#serialPortBtn:disabled{color:#999999;}");
if (m_combSerialPortName == Q_NULLPTR)
{
m_combSerialPortName = new QComboBox();
m_combSerialPortName->setView(new QListView());
m_combSerialPortName->setEditable(false);
m_combSerialPortName->setObjectName("comBoxPortName");
m_combSerialPortName->setFixedSize(110,50);
QString strComStyleSheet = QString("QComboBox#comBoxPortName{font-family:Yahei;font-size:20px;}"
"QComboBox QAbstractItemView{font-size:20px;}"
"QComboBox QAbstractItemView::item{height:50px;}"
"QComboBox#comBoxPortName::down-arrow{height:30px;width:20px;}"
"QComboBox#comBoxPortName::down-down{width:40px;}");
m_combSerialPortName->setStyleSheet(strComStyleSheet);
}
m_combSerialPortName->clear();
foreach(const QSerialPortInfo &portinfo,QSerialPortInfo::availablePorts())
{
m_combSerialPortName->addItem(portinfo.portName());
}
if (m_pSerialPortOpen == Q_NULLPTR)
{
m_pSerialPortOpen = new QPushButton();
m_pSerialPortOpen->setObjectName("serialPortBtn");
m_pSerialPortOpen->setText("打开串口");
m_pSerialPortOpen->setFixedSize(120,50);
m_pSerialPortOpen->setStyleSheet(strBtnStyleSheet);
connect(m_pSerialPortOpen, &QPushButton::clicked,this, &SerialPortToolView::openSerialPort);
}
if (m_pSerialPortClose == Q_NULLPTR)
{
m_pSerialPortClose = new QPushButton();
m_pSerialPortClose->setObjectName("serialPortBtn");
m_pSerialPortClose->setText("关闭串口");
m_pSerialPortClose->setFixedSize(120,50);
m_pSerialPortClose->setStyleSheet(strBtnStyleSheet);
connect(m_pSerialPortClose, &QPushButton::clicked,this, &SerialPortToolView::closeSerialPort);
}
m_pSerialPortOpen->setDisabled(false);
m_pSerialPortClose->setDisabled(true);
if (m_lineEdit == Q_NULLPTR)
{
m_lineEdit = new QLineEdit();
m_lineEdit->setObjectName("sendLineEdit");
m_lineEdit->setPlaceholderText("请输入发送内容:");
m_lineEdit->setFixedSize(400,60);
m_lineEdit->setStyleSheet(QString("QLineEdit#sendLineEdit{font-size:20px;}"));
}
QPushButton *pSendDataBtn = new QPushButton();
pSendDataBtn->setObjectName("serialPortBtn");
pSendDataBtn->setText("发送");
pSendDataBtn->setFixedSize(100,50);
pSendDataBtn->setStyleSheet(strBtnStyleSheet);
connect(pSendDataBtn, &QPushButton::clicked,this, &SerialPortToolView::sendCmdData);
QPushButton *pClearDataBtn = new QPushButton();
pClearDataBtn->setObjectName("serialPortBtn");
pClearDataBtn->setText("清空");
pClearDataBtn->setFixedSize(100,50);
pClearDataBtn->setStyleSheet(strBtnStyleSheet);
connect(pClearDataBtn, &QPushButton::clicked,this, &SerialPortToolView::clearBtnClicked);
QPushButton *pCloseBtn = new QPushButton();
pCloseBtn->setObjectName("serialPortBtn");
pCloseBtn->setText("关闭");
pCloseBtn->setFixedSize(100,50);
pCloseBtn->setStyleSheet(strBtnStyleSheet);
connect(pCloseBtn, &QPushButton::clicked,this, &SerialPortToolView::closeBtnClicked);
QHBoxLayout *serialPortLyt = new QHBoxLayout();
serialPortLyt->setMargin(0);
serialPortLyt->setSpacing(15);
serialPortLyt->setAlignment(Qt::AlignCenter);
serialPortLyt->addWidget(m_combSerialPortName,0,Qt::AlignCenter);
serialPortLyt->addWidget(m_pSerialPortOpen,0,Qt::AlignCenter);
serialPortLyt->addWidget(m_pSerialPortClose,0,Qt::AlignCenter);
serialPortLyt->addWidget(m_lineEdit,0,Qt::AlignCenter);
serialPortLyt->addWidget(pSendDataBtn,0,Qt::AlignCenter);
serialPortLyt->addWidget(pClearDataBtn,0,Qt::AlignCenter);
serialPortLyt->addWidget(pCloseBtn,0,Qt::AlignCenter);
if (m_textBrowser == Q_NULLPTR)
{
m_textBrowser = new QTextBrowser();
}
QVBoxLayout *mainLyt = new QVBoxLayout();
mainLyt->setMargin(0);
mainLyt->setSpacing(20);
mainLyt->addLayout(serialPortLyt);
mainLyt->addWidget(m_textBrowser);
this->setLayout(mainLyt);
}
void SerialPortToolView::openSerialPort()
{
if (m_combSerialPortName == Q_NULLPTR)
{
return;
}
m_serialPort->setPortName(m_combSerialPortName->currentText());
if (!m_serialPort->open(QIODevice::ReadWrite))
{
qDebug() << "打开串口失败" << m_serialPort->portName() << m_combSerialPortName->currentText();
return;
}
m_pSerialPortOpen->setDisabled(true);
m_pSerialPortClose->setDisabled(false);
m_combSerialPortName->setDisabled(true);
m_serialPort->setBaudRate(QSerialPort::Baud19200);
m_serialPort->setDataBits(QSerialPort::Data8);
m_serialPort->setParity(QSerialPort::NoParity);
m_serialPort->setStopBits(QSerialPort::OneStop);
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);
}
void SerialPortToolView::closeSerialPort()
{
if (m_serialPort != Q_NULLPTR)
{
m_serialPort->close();
}
m_pSerialPortOpen->setDisabled(false);
m_pSerialPortClose->setDisabled(true);
m_combSerialPortName->setDisabled(false);
}
void SerialPortToolView::clearBtnClicked()
{
if (m_textBrowser)
{
m_textBrowser->clear();
}
}
void SerialPortToolView::closeBtnClicked()
{
if (m_serialPort != Q_NULLPTR)
{
m_serialPort->close();
}
m_pSerialPortOpen->setDisabled(false);
m_pSerialPortClose->setDisabled(true);
m_combSerialPortName->setDisabled(false);
this->close();
}
void SerialPortToolView::sendCmdData()
{
if (m_serialPort == Q_NULLPTR || m_lineEdit == Q_NULLPTR)
{
return;
}
QString strText = m_lineEdit->text().replace(" ","");
QByteArray arSend = QByteArray::fromHex(strText.toUtf8());
m_serialPort->write(arSend);
m_serialPort->flush();
}
void SerialPortToolView::readyRead()
{
if (m_serialPort == Q_NULLPTR || m_textBrowser == Q_NULLPTR)
{
return;
}
m_textBrowser->append(m_serialPort->readAll().toHex());
}
void SerialPortToolView::mousePressEvent(QMouseEvent *event)
{
m_beginDrag = event->pos();
return QFrame::mousePressEvent(event);
}
void SerialPortToolView::mouseMoveEvent(QMouseEvent *event)
{
QDesktopWidget* desktop = QApplication::desktop();
QRect windowRect(desktop->availableGeometry());
QRect widgetRect(this->geometry());
QPoint point(event->globalPos() - m_beginDrag);
//以下是防止窗口拖出可见范围外
//左边
if (point.x() <= 0)
{
point = QPoint(0,point.y());
}
//右边
int y = windowRect.bottomRight().y() - this->size().height();
if (point.y() >= y && widgetRect.topLeft().y() >= y)
{
point = QPoint(point.x(),y);
}
//上边
if (point.y() <= 0)
{
point = QPoint(point.x(),0);
}
//下边
int x = windowRect.bottomRight().x() - this->size().width();
if (point.x() >= x && widgetRect.topLeft().x() >= x)
{
point = QPoint(x,point.y());
}
this->move(point);
return QFrame::mouseMoveEvent(event);
}
void SerialPortToolView::mouseReleaseEvent(QMouseEvent *event)
{
return QFrame::mouseReleaseEvent(event);
}
- 运行效果: