1 Star 0 Fork 3

QHQ_QHQ/Qt 上位机

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
widget.cpp 2.69 KB
一键复制 编辑 原始数据 按行查看 历史
QHQ_QHQ 提交于 2021-10-31 01:02 . 上传代码源文件
#include "widget.h"
#include "ui_widget.h"
#include <QSerialPortInfo>
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QStringList serialNamePort;
serialPort = new QSerialPort(this);
connect(serialPort,SIGNAL(readyRead()),this,SLOT(serialPortReadyRead_slot()));
foreach (const QSerialPortInfo &info , QSerialPortInfo::availablePorts()){
serialNamePort<<info.portName();
}
ui->serialCb->addItems(serialNamePort);
}
Widget::~Widget()
{
delete ui;
}
void Widget::serialPortReadyRead_slot()
{
QString buf;
buf = QString(serialPort->readAll());
ui->recvEdit->appendPlainText(buf);
}
void Widget::on_openBt_clicked()
{
QSerialPort::BaudRate baudRate;
QSerialPort::DataBits dataBits;
QSerialPort::StopBits stopBits;
QSerialPort::Parity checkBits;
if(ui->baundrateCb->currentText() == "4800"){
baudRate = QSerialPort::Baud4800;
}
else if (ui->baundrateCb->currentText() == "9600") {
baudRate = QSerialPort::Baud9600;
}
else if (ui->baundrateCb->currentText() == "115200") {
baudRate = QSerialPort::Baud115200;
}
if(ui->dataCb->currentText() == "5"){
dataBits = QSerialPort::Data5;
}
else if (ui->dataCb->currentText() == "6") {
dataBits = QSerialPort::Data6;
}
else if (ui->dataCb->currentText() == "7") {
dataBits = QSerialPort::Data7;
}
else if (ui->dataCb->currentText() == "8") {
dataBits = QSerialPort::Data8;
}
if(ui->stopCb->currentText() == "1"){
stopBits = QSerialPort::OneStop;
}
else if (ui->stopCb->currentText() == "1.5") {
stopBits = QSerialPort::OneAndHalfStop;
}
else if (ui->stopCb->currentText() == "2") {
stopBits = QSerialPort::TwoStop;
}
if(ui->checkCb->currentText() == "none")
{
checkBits = QSerialPort::NoParity;
}
serialPort->setPortName(ui->serialCb->currentText());
serialPort->setBaudRate(baudRate);
serialPort->setDataBits(dataBits);
serialPort->setStopBits(stopBits);
serialPort->setParity(checkBits);
if(serialPort->open(QIODevice::ReadWrite) == true){
QMessageBox::information(this,"提示","成功");
}
else{
QMessageBox::critical(this,"提示","失败!");
}
}
void Widget::on_closeBt_clicked()
{
serialPort->close();
}
void Widget::on_sendBt_clicked()
{
serialPort->write(ui->sendEdit->text().toLocal8Bit().data());
ui->sendEdit->clear();
}
void Widget::on_clearBt_clicked()
{
ui->recvEdit->clear();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/qhq0210/qt-upper-computer.git
[email protected]:qhq0210/qt-upper-computer.git
qhq0210
qt-upper-computer
Qt 上位机
master

搜索帮助