1.IDE:QTCreator
2.实验
3.记录
(1)在widget.h中加入必要文件,并且定义一个类指针
(2)如果有类的成员不知道怎么写,可以通过以下途径搜索
(2)设置串口数据
void Widget::on_open_pb_clicked() //当打开串口按下时
{QSerialPort::BaudRate buadrate; // ::为作用域,定义波特率QSerialPort::DataBits databits; //定义数据位QSerialPort::StopBits stopbits; //定义停止位QSerialPort::Parity checkbits; //定义校验位
/******************波特率赋值******************************/if(ui->baudrate_cb->currentText()=="115200")buadrate=QSerialPort::Baud115200;else if(ui->baudrate_cb->currentText()=="9600")buadrate=QSerialPort::Baud9600;else if(ui->baudrate_cb->currentText()=="4800")buadrate=QSerialPort::Baud4800;
/******************数据位赋值*******************************/if(ui->data_cb->currentText()=="8")databits=QSerialPort::Data8;else if(ui->data_cb->currentText()=="7")databits=QSerialPort::Data7;else if(ui->data_cb->currentText()=="6")databits=QSerialPort::Data6;else if(ui->data_cb->currentText()=="5")databits=QSerialPort::Data5;
/*******************停止位赋值******************************/if(ui->stop_cb->currentText()=="1")stopbits=QSerialPort::OneStop;else if(ui->stop_cb->currentText()=="1.5")stopbits=QSerialPort::OneAndHalfStop;else if(ui->stop_cb->currentText()=="2")stopbits=QSerialPort::TwoStop;
/******************校验位赋值******************************/checkbits=QSerialPort::NoParity; //因为强制选择无校验位
/******************设置串口*******************************/serialport->setPortName(ui->com_cb->currentText()); //设置串口名serialport->setBaudRate(buadrate); //设置波特率serialport->setDataBits(databits); //设置数据位serialport->setStopBits(stopbits); //设置停止位serialport->setParity(checkbits); //设置校验位if(serialport->open(QIODevice::ReadWrite)==true) //串口打开成功QMessageBox::information(this,"提示","串口打开成功");elseQMessageBox::critical(this,"警告","串口打开失败");
}
(3)将串口打开与串口关闭通过槽函数关联按钮(QMessageBox用于提示)
void Widget::on_close_pb_clicked() //当关闭窗口按钮按下时
{serialport->close();QMessageBox::information(this,"提示","串口已经关闭");
}
(4)关联准备读函数(函数部分widget前应该有void)
/*************************准备读函数*************************/
void Widget::SerialPort_ReadyRead_Slot()
{QString receive_buff; //定义一个数组,存放接收到的内容receive_buff=QString(serialport->readAll());ui->receive_line->appendPlainText(receive_buff); //将接收到的内容在接收区显示出来
}
(5)发送函数关联
void Widget::on_send_pb_clicked()
{serialport->write(ui->send_line->text().toLocal8Bit().data()); //将发送区内容转换为8bit发送出去
}
(6)清空函数关联
void Widget::on_clear_pb_clicked() //当清空按钮按下时
{ui->receive_line->clear();
}
(7)运行效果
4.代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include "QSerialPort" //加入文件QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();QSerialPort *serialport; //定义一个QserialPort类的指针
private slots:void on_open_pb_clicked();void on_close_pb_clicked();void SerialPort_ReadyRead_Slot(); //准备读信号函数声明void on_send_pb_clicked();void on_clear_pb_clicked();private:Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "QSerialPortInfo" //引入必要的serial文件
#include "QMessageBox" //消息提示
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QStringList serialNamePort; //定义一个数组serialport = new QSerialPort(this); //串口指针赋值给serialportconnect(serialport,SIGNAL(readyRead()),this,SLOT(SerialPort_ReadyRead_Slot())); //关联准备读信号foreach (const QSerialPortInfo &info ,QSerialPortInfo::availablePorts()) { //自动搜索当前串口serialNamePort<<info.portName(); //将返回值加载入数组}ui->com_cb->addItems(serialNamePort); //将搜索的串口加入UI界面
}Widget::~Widget()
{delete ui;
}
/*************************准备读函数*************************/
void Widget::SerialPort_ReadyRead_Slot()
{QString receive_buff; //定义一个数组,存放接收到的内容receive_buff=QString(serialport->readAll()); //将接收到的数据转换为QString类型ui->receive_line->appendPlainText(receive_buff); //将接收到的内容在接收区显示出来
}void Widget::on_open_pb_clicked() //当打开串口按下时
{QSerialPort::BaudRate buadrate; // ::为作用域,定义波特率QSerialPort::DataBits databits; //定义数据位QSerialPort::StopBits stopbits; //定义停止位QSerialPort::Parity checkbits; //定义校验位
/******************波特率赋值******************************/if(ui->baudrate_cb->currentText()=="115200")buadrate=QSerialPort::Baud115200;else if(ui->baudrate_cb->currentText()=="9600")buadrate=QSerialPort::Baud9600;else if(ui->baudrate_cb->currentText()=="4800")buadrate=QSerialPort::Baud4800;
/******************数据位赋值*******************************/if(ui->data_cb->currentText()=="8")databits=QSerialPort::Data8;else if(ui->data_cb->currentText()=="7")databits=QSerialPort::Data7;else if(ui->data_cb->currentText()=="6")databits=QSerialPort::Data6;else if(ui->data_cb->currentText()=="5")databits=QSerialPort::Data5;
/*******************停止位赋值******************************/if(ui->stop_cb->currentText()=="1")stopbits=QSerialPort::OneStop;else if(ui->stop_cb->currentText()=="1.5")stopbits=QSerialPort::OneAndHalfStop;else if(ui->stop_cb->currentText()=="2")stopbits=QSerialPort::TwoStop;
/******************校验位赋值******************************/checkbits=QSerialPort::NoParity; //因为强制选择无校验位
/******************设置串口*******************************/serialport->setPortName(ui->com_cb->currentText()); //设置串口名serialport->setBaudRate(buadrate); //设置波特率serialport->setDataBits(databits); //设置数据位serialport->setStopBits(stopbits); //设置停止位serialport->setParity(checkbits); //设置校验位if(serialport->open(QIODevice::ReadWrite)==true) //串口打开成功QMessageBox::information(this,"提示","串口打开成功");elseQMessageBox::critical(this,"警告","串口打开失败");
}void Widget::on_close_pb_clicked() //当关闭窗口按钮按下时
{serialport->close();QMessageBox::information(this,"提示","串口已经关闭");
}void Widget::on_send_pb_clicked() //当发送按钮按下时
{serialport->write(ui->send_line->text().toLocal8Bit().data()); //将发送区内容转换为8bit发送出去
}void Widget::on_clear_pb_clicked() //当清空按钮按下时
{ui->receive_line->clear();
}
pro文件(只加入了serialport,以及自动加入的图标文件)
QT += core gui serialportgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetRESOURCES += \tubiao.qrc