QT5 学习记录 QSerialPort 类实现串口调试工具
来源:互联网 发布:我赢职场java视频 编辑:程序博客网 时间:2024/06/05 00:50
1.
5.运行图
#-------------------------------------------------## Project created by QtCreator 2017-11-08T10:55:55##-------------------------------------------------QT += core guiQT += serialportgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = SerialPortToolTEMPLATE = appSOURCES += main.cpp\ serialporttool.cppHEADERS += serialporttool.hFORMS += serialporttool.ui
2.serialporttool.h
#ifndef SERIALPORTTOOL_H#define SERIALPORTTOOL_H#include <QWidget>#include <QMainWindow>#include <QTextEdit>#include <QLabel>#include <QComboBox>#include <QPushButton>#include <QtSerialPort/QSerialPort>#include <QtSerialPort/QSerialPortInfo>#include <QHBoxLayout>#include <QVBoxLayout>#include <QGridLayout>namespace Ui {class SerialPortTool;}class SerialPortTool : public QWidget{ Q_OBJECTpublic: explicit SerialPortTool(QWidget *parent = 0); ~SerialPortTool(); //layout QVBoxLayout * LeftLayout; QGridLayout * RightLayout; QHBoxLayout * MainLayout; QTextEdit * RecvText; QTextEdit * SendText; //port QSerialPort * serial ; //port name QLabel * PortNameLabel; QComboBox * PortNameComboBox; //baoud rate QLabel * BaudRateLabel; QComboBox * BaudRateComboBox; //date bit QLabel * DataBitLabel; QComboBox * DataBitComboBox; //check bit QLabel * CheckLabel; QComboBox * CheckComboBox; //Stop bit QLabel * StopLabel; QComboBox * StopComboBox; //open QPushButton * openButton; //send QPushButton * SendButton; //clear QPushButton * ClearButton;private: Ui::SerialPortTool *ui;private slots: void openSlotFun(); void sendSlotFun(); void clearSlotFun(); void readUartSlotFun();};#endif // SERIALPORTTOOL_H
3.serialporttool.cpp
#include "serialporttool.h"#include "ui_serialporttool.h"#include <QDebug>#include <QString>#include <QDialog>#include <QMessageBox>SerialPortTool::SerialPortTool(QWidget *parent) : QWidget(parent), ui(new Ui::SerialPortTool){ ui->setupUi(this); //布局 LeftLayout =new QVBoxLayout; RightLayout = new QGridLayout; MainLayout = new QHBoxLayout(this); //发送框和接受框 RecvText = new QTextEdit; SendText = new QTextEdit; LeftLayout->addWidget(RecvText); LeftLayout->addWidget(SendText); LeftLayout->setStretchFactor(RecvText,3); LeftLayout->setStretchFactor(SendText,1); //添加端口 PortNameLabel = new QLabel(tr("端 口:")); PortNameComboBox = new QComboBox; foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { QSerialPort mSerialPort; mSerialPort.setPort(info); PortNameComboBox->addItem(mSerialPort.portName()); } RightLayout->addWidget(PortNameLabel,0,0); RightLayout->addWidget(PortNameComboBox,0,1); //设置波特率 BaudRateLabel = new QLabel(tr("波特率:")); BaudRateComboBox = new QComboBox; BaudRateComboBox->addItem("115200"); BaudRateComboBox->addItem("9600"); RightLayout->addWidget(BaudRateLabel,1,0); RightLayout->addWidget(BaudRateComboBox,1,1); //数据位 DataBitLabel = new QLabel(tr("数据位:")); DataBitComboBox = new QComboBox; DataBitComboBox->addItem("8"); //DataBitComboBox->addItem("7"); RightLayout->addWidget(DataBitLabel,2,0); RightLayout->addWidget(DataBitComboBox,2,1); //校验位 CheckLabel = new QLabel("校验位:"); CheckComboBox = new QComboBox; CheckComboBox->addItem("无"); RightLayout->addWidget(CheckLabel,3,0); RightLayout->addWidget(CheckComboBox,3,1); //停止位 StopLabel = new QLabel("停止位:"); StopComboBox = new QComboBox; StopComboBox->addItem("1"); StopComboBox->addItem("2"); RightLayout->addWidget(StopLabel,4,0); RightLayout->addWidget(StopComboBox,4,1); //open openButton = new QPushButton; openButton->setText("open"); RightLayout->addWidget(openButton,5,0,1,2); //clear ClearButton = new QPushButton; ClearButton->setText("clear"); RightLayout->addWidget(ClearButton,6,0,1,2); //send SendButton = new QPushButton; SendButton->setText("send"); RightLayout->addWidget(SendButton,7,0,1,2); MainLayout->addLayout(LeftLayout); MainLayout->addLayout(RightLayout); //SLOT connect(openButton,SIGNAL(clicked()),this,SLOT(openSlotFun())); connect(SendButton,SIGNAL(clicked()),this,SLOT(sendSlotFun())); connect(ClearButton,SIGNAL(clicked()),this,SLOT(clearSlotFun()));}void SerialPortTool::openSlotFun(){ if(openButton->text() == tr("open")) { serial = new QSerialPort; //port name serial->setPortName(PortNameComboBox->currentText()); //open serial->open(QIODevice::ReadWrite); //baudrate serial->setBaudRate(SerialPortTool::BaudRateComboBox->currentText().toInt()); //data bit if(SerialPortTool::DataBitComboBox->currentText() == tr("8")) { serial->setDataBits(QSerialPort::Data8); } //check if(SerialPortTool::CheckComboBox->currentText() == tr("无")) { serial->setParity(QSerialPort::NoParity); } //stop switch(SerialPortTool::StopComboBox->currentText().toInt()) { case 1: serial->setStopBits(QSerialPort::OneStop); break; case 2: serial->setStopBits(QSerialPort::TwoStop); break; default: break; } //flowcontrol serial->setFlowControl(QSerialPort::NoFlowControl); // connect(serial,SIGNAL(readyRead()),this,SLOT(readUartSlotFun())); // openButton->setText("close"); } else { serial->clear(); serial->close(); serial->deleteLater(); openButton->setText("open"); }}void SerialPortTool::readUartSlotFun(){ QByteArray readData; readData = serial->readAll(); qDebug()<<readData; if(!readData.isEmpty()) { //方法1,会换行 //RecvText->append(readData); //方法2,不换行 RecvText->insertPlainText(readData); RecvText->moveCursor(QTextCursor::End); } readData.clear();}void SerialPortTool::clearSlotFun(){ RecvText->clear();}void SerialPortTool::sendSlotFun(){ //不加判断的话,如果没打开串口直接点发送按钮程序会死 if(openButton->text() == tr("close")) { serial->write(SendText->toPlainText().toLatin1()); } else { //没开串口,给个提示 QMessageBox::information(this,tr("info"),tr("Please open one serial port!")); }}SerialPortTool::~SerialPortTool(){ delete ui;}
4.main.cpp
#include "serialporttool.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); SerialPortTool w; w.setGeometry(300,200,600,400); w.show(); return a.exec();}
5.运行图
阅读全文
0 0
- QT5 学习记录 QSerialPort 类实现串口调试工具
- 初次使用QT5串口类QSerialPort
- 初次使用QT5串口类QSerialPort
- Qt 串口类QSerialPort 学习笔记
- Qt5.6实现的串口调试助手
- Qt 串口类QSerialPort 使用笔记
- Qt 串口类QSerialPort 使用笔记
- Qt之串口类QSerialPort使用笔记
- Qt 串口类QSerialPort 使用笔记
- Qt 串口类QSerialPort 使用笔记
- Qt 串口类QSerialPort 使用笔记
- Qt5.7 QSerialPort串口使用时readyRead()信号不产生解决方法
- QT5中QSerialPort串口通讯的时候,readyRead()信号不产生的解决方案
- 解决Qt5中的QSerialPort分多次接收串口数据的问题
- qt5实现串口通信
- Qt5实现串口通信
- Qt5实现串口通信
- QT 使用串口类出现“undefined reference to QSerialPort::QSerialPort(QObject*)错误
- 快速幂
- Apache Shiro简介
- 迷宫问题
- 使用C#开发数据库应用系统_使用ListView控件展示数据
- 我的秘密武器—Git
- QT5 学习记录 QSerialPort 类实现串口调试工具
- EXTJS6.0 饼图图例显示以及百分比显示
- LightOJ
- webservice-实现手机号码归属地的查询
- 回首Java——HashSet vs. TreeSet vs. LinkedHashSet
- Android精选完整源码之控件指示器视频压缩日历源码!
- 详细解释CNN卷积神经网络各层的参数和链接个数的计算
- php 有哪些数值是false
- ExtJs6学习(五)【ExtJs布局】