QT 序列槽調試工具
- 第一節 虛拟序列槽工具安裝
- 第二節 QT建立一個基于QWidget的項目
- 第三節 UI界面設計
- 第三節 項目頭檔案widget.h
- 第四節 項目實作檔案widget.cpp
- 第五節 main函數
- 第六節 編譯結果重點
- 第七節 使用QT打包程式,不安裝QT的電腦可使用
第一節 虛拟序列槽工具安裝
-----------------------------------------下載下傳所需工具---------------------------------------------------------------------
連結:https://pan.baidu.com/s/1QkT36S4EnH2HEAhZ1TZ8ug?pwd=ilqg
提取碼:ilqg
-------------------------------------首先點選.exe檔案-----------------------------------------------------------------------
------------------------------------安裝在指定目錄等下要用--------------------------------------------------
将vspdctl.dll替換掉你安裝檔案的所在目錄,不然出現錯誤,就掉頭發了哦
-------------------------------------打開軟體看看有啥---------------------------------------------------=
注意
:請記住這裡的操作,後面還要用到
-------------------------------------------通過上面的操作,出現以下界面,說明設定成功---------------------
點選電腦—管理—裝置管理器–端口–可以看到成功了
第二節 QT建立一個基于QWidget的項目
項目.pro添加以下資訊
# 添加序列槽
QT += serialport
第三節 UI界面設計
這裡不多說,隻要是按鈕都要轉到槽,具體槽函數請看源代碼
第三節 項目頭檔案widget.h
使用兩個序列槽對象實作
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSerialPort> //提供通路序列槽得到功能
#include <QSerialPortInfo> //提供系統中存在的序列槽資訊
#include <QMessageBox> //資訊彈出框
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
//使用枚舉儲存波特率
typedef enum
{
B1200 =1200,
B9600 = 9600,
B115200 = 115200,
B921600 = 921600
}Baud;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
//接收序列槽資料
void SerialPortReadyRead(); //比作COM6
void Serial2PortReadyRead(); //比作COM7
private slots:
void on_PB_receive_clicked();
void on_PB_send_clicked();
void on_PB_detectserial_clicked();
void on_PB_openserial_clicked();
void on_comboBox_b_currentIndexChanged(int index);
void on_PB_openserial_2_clicked();
void on_PB_send_2_clicked();
private:
Ui::Widget *ui;
QSerialPort serial; //序列槽1
QSerialPort serial2; //序列槽2
};
#endif // WIDGET_H
-------------------------------------------------------------------------------------------------------------------------------------
第四節 項目實作檔案widget.cpp
----------------------實作信号與槽,信号為緩沖區有資料時,槽函數作為接收資料------------------------------------
//連接配接信号與槽
QObject::connect(&serial, SIGNAL(readyRead()),this, SLOT(SerialPortReadyRead()));
QObject::connect(&serial2, SIGNAL(readyRead()),this, SLOT(Serial2PortReadyRead()));
------------------------------------------------序列槽對象一次性讀取全部----------------------------------------------------------
//序列槽2接收序列槽1資料
void Widget::SerialPortReadyRead()
{
qDebug()<<"===============有資料==================";
//從緩沖區讀取檔案
QByteArray buffer = serial.readAll();
//擷取界面已經讀取的資料
QString recv = ui->plainTextEdit_2->toPlainText();
recv +=QString(buffer);
//顯示全部
ui->plainTextEdit_2->appendPlainText(recv);
}
//序列槽1接收序列槽2資料
void Widget::Serial2PortReadyRead()
{
qDebug()<<"===============有資料2==================";
//從緩沖區讀取檔案
QByteArray buffer = serial2.readAll();
//擷取界面已經讀取的資料
QString recv = ui->plainTextEdit->toPlainText();
recv +=QString(buffer);
//顯示全部
ui->plainTextEdit->appendPlainText(recv);
}
--------------------------------------------------------------向序列槽寫入資料------------------------------------------------------
//作為序列槽1
void Widget::on_PB_send_clicked()
{
//向序列槽寫入資料
QByteArray data = ui->plainTextEdit_2->toPlainText().toUtf8();
qDebug()<<"發送資料 data = "<<data;
serial.write(data);
if(!serial.isWritable())
qDebug()<<"寫入失敗";
}
//作為序列槽2
void Widget::on_PB_send_2_clicked()
{
//向序列槽2寫入資料
QByteArray data = ui->plainTextEdit->toPlainText().toUtf8();
qDebug()<<"發送資料 data2 = "<<data;
serial2.write(data);
if(!serial2.isWritable())
qDebug()<<"寫入失敗";
}
-----------------------------------------配置序列槽并打開--------------------------------------------------------------------
void Widget::on_PB_openserial_clicked()
{
if(ui->PB_openserial->text() == QString("打開序列槽1"))
{
//設定序列槽名
serial.setPortName(ui->comboBox_id->currentText());
//設定波特率
serial.setBaudRate(ui->comboBox_b->currentText().toUInt());
//設定資料位
switch (ui->comboBox_data->currentIndex())
{
case 8:serial.setDataBits(QSerialPort::Data8);break;
default:break;
}
//設定奇偶校驗 無
switch (ui->comboBox_crc->currentIndex())
{
case 0:serial.setParity(QSerialPort::NoParity);break;
default:break;
}
//設定停止位
switch (ui->comboBox_stop->currentIndex())
{
case 1:serial.setStopBits(QSerialPort::OneStop);break;
default:break;
}
//設定流控 無
serial.setFlowControl(QSerialPort::NoFlowControl);
//打開序列槽
if(!serial.open(QIODevice::ReadWrite))
{
QMessageBox::about(NULL,"提示","序列槽1打開失敗");
}
if(serial.isOpen())
QMessageBox::about(NULL,"提示","序列槽1打開成功");
//下拉菜單控件失能
ui->comboBox_b->setEnabled(false);
ui->comboBox_id->setEnabled(false);
ui->comboBox_crc->setEnabled(false);
ui->comboBox_data->setEnabled(false);
ui->comboBox_stop->setEnabled(false);
ui->PB_openserial->setText(tr("關閉序列槽1"));
//發送按鍵使能
ui->PB_send->setEnabled(true);
}
else
{
//關閉序列槽
serial.close();
//下拉使能
ui->comboBox_b->setEnabled(true);
ui->comboBox_id->setEnabled(true);
ui->comboBox_crc->setEnabled(true);
ui->comboBox_data->setEnabled(true);
ui->comboBox_stop->setEnabled(true);
ui->PB_openserial->setText(tr("打開序列槽1"));
//發送失能
ui->PB_send->setEnabled(false);
}
}
//擷取波特率索引
void Widget::on_comboBox_b_currentIndexChanged(int index)
{
if(ui->comboBox_b->itemText(index).toUInt() == B9600)
{
qDebug()<<"itemText = "<<ui->comboBox_b->itemText(index);
ui->comboBox_b->setCurrentIndex(index);
}
else if(ui->comboBox_b->itemText(index).toUInt() == B115200)
{
qDebug()<<"itemText = "<<ui->comboBox_b->itemText(index);
ui->comboBox_b->setCurrentIndex(index);
}
}
void Widget::on_PB_openserial_2_clicked()
{
if(ui->PB_openserial_2->text() == QString("打開序列槽2"))
{
//設定序列槽名
serial2.setPortName(ui->comboBox_id_2->currentText());
//設定波特率
serial2.setBaudRate(ui->comboBox_b->currentText().toUInt());
//設定資料位
switch (ui->comboBox_data->currentIndex())
{
case 8:serial2.setDataBits(QSerialPort::Data8);break;
default:break;
}
//設定奇偶校驗 無
switch (ui->comboBox_crc->currentIndex())
{
case 0:serial2.setParity(QSerialPort::NoParity);break;
default:break;
}
//設定停止位
switch (ui->comboBox_stop->currentIndex())
{
case 1:serial2.setStopBits(QSerialPort::OneStop);break;
default:break;
}
//設定流控 無
serial2.setFlowControl(QSerialPort::NoFlowControl);
//打開序列槽
if(!serial2.open(QIODevice::ReadWrite))
{
QMessageBox::about(NULL,"提示","序列槽2打開失敗");
}
if(serial2.isOpen())
QMessageBox::about(NULL,"提示","序列槽2打開成功");
//下拉菜單控件失能
ui->comboBox_b->setEnabled(false);
ui->comboBox_id_2->setEnabled(false);
ui->comboBox_crc->setEnabled(false);
ui->comboBox_data->setEnabled(false);
ui->comboBox_stop->setEnabled(false);
ui->PB_openserial_2->setText(tr("關閉序列槽2"));
//發送按鍵使能
ui->PB_send_2->setEnabled(true);
}
else
{
//關閉序列槽
serial2.close();
//下拉使能
ui->comboBox_b->setEnabled(true);
ui->comboBox_id_2->setEnabled(true);
ui->comboBox_crc->setEnabled(true);
ui->comboBox_data->setEnabled(true);
ui->comboBox_stop->setEnabled(true);
ui->PB_openserial_2->setText(tr("打開序列槽2"));
//發送失能
ui->PB_send_2->setEnabled(false);
}
}
-----------------------------------------------------------------源碼------------------------------------------------------------
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//連接配接信号與槽
QObject::connect(&serial, SIGNAL(readyRead()),this, SLOT(SerialPortReadyRead()));
QObject::connect(&serial2, SIGNAL(readyRead()),this, SLOT(Serial2PortReadyRead()));
//發送按鈕不使能
ui->PB_send->setDisabled(true);
ui->PB_send_2->setDisabled(true);
}
Widget::~Widget()
{
delete ui;
}
//序列槽2接收序列槽1資料
void Widget::SerialPortReadyRead()
{
qDebug()<<"===============有資料==================";
//從緩沖區讀取檔案
QByteArray buffer = serial.readAll();
//擷取界面已經讀取的資料
QString recv = ui->plainTextEdit_2->toPlainText();
recv +=QString(buffer);
//顯示全部
ui->plainTextEdit_2->appendPlainText(recv);
}
//序列槽1接收序列槽2資料
void Widget::Serial2PortReadyRead()
{
qDebug()<<"===============有資料2==================";
//從緩沖區讀取檔案
QByteArray buffer = serial2.readAll();
//擷取界面已經讀取的資料
QString recv = ui->plainTextEdit->toPlainText();
recv +=QString(buffer);
//顯示全部
ui->plainTextEdit->appendPlainText(recv);
}
void Widget::on_PB_receive_clicked()
{
//清空接收框資料
ui->plainTextEdit->clear();
ui->plainTextEdit_2->clear();
}
//作為序列槽1
void Widget::on_PB_send_clicked()
{
//向序列槽寫入資料
QByteArray data = ui->plainTextEdit_2->toPlainText().toUtf8();
qDebug()<<"發送資料 data = "<<data;
serial.write(data);
if(!serial.isWritable())
qDebug()<<"寫入失敗";
}
//作為序列槽2
void Widget::on_PB_send_2_clicked()
{
//向序列槽2寫入資料
QByteArray data = ui->plainTextEdit->toPlainText().toUtf8();
qDebug()<<"發送資料 data2 = "<<data;
serial2.write(data);
if(!serial2.isWritable())
qDebug()<<"寫入失敗";
}
//檢測序列槽
void Widget::on_PB_detectserial_clicked()
{
//清空序列槽id
ui->comboBox_id->clear();
ui->comboBox_id_2->clear();
//通過這個類查找可用序列槽1
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
qDebug()<<"info.portName() = "<<info.portName();
ui->comboBox_id->addItem(info.portName());
}
//通過這個類查找可用序列槽2
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
qDebug()<<"info.portName() = "<<info.portName();
ui->comboBox_id_2->addItem(info.portName());
}
}
void Widget::on_PB_openserial_clicked()
{
if(ui->PB_openserial->text() == QString("打開序列槽1"))
{
//設定序列槽名
serial.setPortName(ui->comboBox_id->currentText());
//設定波特率
serial.setBaudRate(ui->comboBox_b->currentText().toUInt());
//設定資料位
switch (ui->comboBox_data->currentIndex())
{
case 8:serial.setDataBits(QSerialPort::Data8);break;
default:break;
}
//設定奇偶校驗 無
switch (ui->comboBox_crc->currentIndex())
{
case 0:serial.setParity(QSerialPort::NoParity);break;
default:break;
}
//設定停止位
switch (ui->comboBox_stop->currentIndex())
{
case 1:serial.setStopBits(QSerialPort::OneStop);break;
default:break;
}
//設定流控 無
serial.setFlowControl(QSerialPort::NoFlowControl);
//打開序列槽
if(!serial.open(QIODevice::ReadWrite))
{
QMessageBox::about(NULL,"提示","序列槽1打開失敗");
}
if(serial.isOpen())
QMessageBox::about(NULL,"提示","序列槽1打開成功");
//下拉菜單控件失能
ui->comboBox_b->setEnabled(false);
ui->comboBox_id->setEnabled(false);
ui->comboBox_crc->setEnabled(false);
ui->comboBox_data->setEnabled(false);
ui->comboBox_stop->setEnabled(false);
ui->PB_openserial->setText(tr("關閉序列槽1"));
//發送按鍵使能
ui->PB_send->setEnabled(true);
}
else
{
//關閉序列槽
serial.close();
//下拉使能
ui->comboBox_b->setEnabled(true);
ui->comboBox_id->setEnabled(true);
ui->comboBox_crc->setEnabled(true);
ui->comboBox_data->setEnabled(true);
ui->comboBox_stop->setEnabled(true);
ui->PB_openserial->setText(tr("打開序列槽1"));
//發送失能
ui->PB_send->setEnabled(false);
}
}
//擷取波特率索引
void Widget::on_comboBox_b_currentIndexChanged(int index)
{
if(ui->comboBox_b->itemText(index).toUInt() == B9600)
{
qDebug()<<"itemText = "<<ui->comboBox_b->itemText(index);
ui->comboBox_b->setCurrentIndex(index);
}
else if(ui->comboBox_b->itemText(index).toUInt() == B115200)
{
qDebug()<<"itemText = "<<ui->comboBox_b->itemText(index);
ui->comboBox_b->setCurrentIndex(index);
}
}
void Widget::on_PB_openserial_2_clicked()
{
if(ui->PB_openserial_2->text() == QString("打開序列槽2"))
{
//設定序列槽名
serial2.setPortName(ui->comboBox_id_2->currentText());
//設定波特率
serial2.setBaudRate(ui->comboBox_b->currentText().toUInt());
//設定資料位
switch (ui->comboBox_data->currentIndex())
{
case 8:serial2.setDataBits(QSerialPort::Data8);break;
default:break;
}
//設定奇偶校驗 無
switch (ui->comboBox_crc->currentIndex())
{
case 0:serial2.setParity(QSerialPort::NoParity);break;
default:break;
}
//設定停止位
switch (ui->comboBox_stop->currentIndex())
{
case 1:serial2.setStopBits(QSerialPort::OneStop);break;
default:break;
}
//設定流控 無
serial2.setFlowControl(QSerialPort::NoFlowControl);
//打開序列槽
if(!serial2.open(QIODevice::ReadWrite))
{
QMessageBox::about(NULL,"提示","序列槽2打開失敗");
}
if(serial2.isOpen())
QMessageBox::about(NULL,"提示","序列槽2打開成功");
//下拉菜單控件失能
ui->comboBox_b->setEnabled(false);
ui->comboBox_id_2->setEnabled(false);
ui->comboBox_crc->setEnabled(false);
ui->comboBox_data->setEnabled(false);
ui->comboBox_stop->setEnabled(false);
ui->PB_openserial_2->setText(tr("關閉序列槽2"));
//發送按鍵使能
ui->PB_send_2->setEnabled(true);
}
else
{
//關閉序列槽
serial2.close();
//下拉使能
ui->comboBox_b->setEnabled(true);
ui->comboBox_id_2->setEnabled(true);
ui->comboBox_crc->setEnabled(true);
ui->comboBox_data->setEnabled(true);
ui->comboBox_stop->setEnabled(true);
ui->PB_openserial_2->setText(tr("打開序列槽2"));
//發送失能
ui->PB_send_2->setEnabled(false);
}
}
第五節 main函數
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
第六節 編譯結果重點
--------------------------------不想編寫兩個序列槽的,安裝的序列槽助手裡面有一個工具----------------------------------
可以看到,可以實作所需的功能
第七節 使用QT打包程式,不安裝QT的電腦可使用
第一步QT編譯使用如下
第二部,執行上述以後,去以下目錄複制.exe檔案到一個建立的檔案夾
以管理者身份打開cmd,進入的這個建立的檔案夾
windeployqt.exe 你的.exe檔案名
由于沒有一台未安裝QT的電腦作為模拟,有些遺憾
為了提高成功率打開如下:
cd D:\Qt\Qt5.14.2\Tools\QtCreator\bin
進入以上目錄,執行如下指令
D:\Qt\Qt5.14.2\5.14.2\mingw73_64>windeployqt.exe C:\Users\ll\Desktop\QT序列槽工具\QSerialPort.exe
-------------------------------------本次任務over-----------------------------------------------------------