前情概述
在之前的文章中我们以及完成了串口调试助手页面的制作,
同时在打开串口按键的槽函数中实现串口的打开功能
本章节将注重于实现在串口打开后数据的收发问题以及一系列优化
本章流程
准备工作
1.在头文件中定义以下变量
private slots:
void on_btnCloseorOpenSerial_clicked();
void on_btnSendContext_clicked();//发送按键的槽函数
void on_SerialData_readyToRed();//接收信号的槽函数用于数据接收
private:
Ui::Widget *ui;
QSerialPort *serialPort; //创建QSrialPort型指针
int writeCntTotal; //记录发送量
int ReadCntTotal; //记录接受量
QString lastSend; //判断发送的数据与上一次的数据是否相同
bool serialStatus; //判断串口是否打开的状态
2.在构造函数中初始化变量,将信号与槽连接
connect(serialPort, &QSerialPort::readyRead, this, &Widget::on_SerialData_readyToRed);
serialStatus = false;
writeCntTotal = 0;
ReadCntTotal = 0;
serialPort = new QSerialPort(this); //实例化
发送按键功能
void Widget::on_btnSendContext_clicked()
{
int writeCnt = 0;
//获取lineEdit中要发送的内容
const char* sendData = ui->lineEditSendContext->text().toStdString().c_str();
//返回值为发送数量,若发送失败则返回-1
writeCnt = serialPort->write(sendData);
//若发送成功
if(writeCnt != -1)
{
writeCntTotal = writeCntTotal + writeCnt;
qDebug()<<"send success"<<sendData;
ui->labelSendStatus->setText("Send OK!");
ui->labelSendcnt->setNum(writeCntTotal);
if(strcmp(sendData, lastSend.toStdString().c_str())) //比较字符串指针与QString
{
ui->textEditRecord->append(sendData);
lastSend = QString(sendData); //转换
}
}
//若发送失败
else
{
qDebug()<<"send fail";
QMessageBox msgBox;
msgBox.setText("串口未打开");
msgBox.setWindowTitle("Error");
msgBox.exec();
}
}
接收数据功能
void Widget::on_SerialData_readyToRed()
{
QString revMessage = serialPort->readAll();
if(revMessage != NULL)
{
qDebug() << "GetMessage" << revMessage;
ui->textEditRev->append(revMessage);
ReadCntTotal = ReadCntTotal + revMessage.size();
ui->labelRevcnt->setNum(ReadCntTotal);
}
}
发送按键优化(优化内容见流程图)
void Widget::on_btnCloseorOpenSerial_clicked()
{
if(serialStatus == false)
{
//1选择端口号
serialPort->setPortName(ui->comboBox_serialNum->currentText());
//2配置波特率
serialPort->setBaudRate(ui->comboBox_baudrate->currentText().toInt());
//3配置数据位
serialPort->setDataBits(QSerialPort::DataBits(ui->comboBox_databit->currentText().toInt()));
//4配置校验位
switch (ui->comboBox_checkbit->currentIndex())
{
case 0:
serialPort->setParity(QSerialPort::NoParity);
break;
case 1:
serialPort->setParity(QSerialPort::EvenParity);
break;
case 2:
serialPort->setParity(QSerialPort::MarkParity);
break;
case 3:
serialPort->setParity(QSerialPort::OddParity);
break;
case 4:
serialPort->setParity(QSerialPort::SpaceParity);
break;
default:
serialPort->setParity(QSerialPort::UnknownParity);
break;
}
//5配置停止位
serialPort->setStopBits(QSerialPort::StopBits(ui->comboBox_stopbit->currentText().toInt()));
//6流控
if(ui->comboBox_fileCon->currentText() == "None")
{
serialPort->setFlowControl(QSerialPort::FlowControl(0));
}
//7打开串口
if(serialPort->open(QIODevice::ReadWrite))
{
qDebug()<<ui->comboBox_serialNum->currentText()<<"open success";
ui->comboBox_databit->setEnabled(false);
ui->comboBox_fileCon->setEnabled(false);
ui->comboBox_stopbit->setEnabled(false);
ui->comboBox_baudrate->setEnabled(false);
ui->comboBox_checkbit->setEnabled(false);
ui->comboBox_serialNum->setEnabled(false);
ui->btnCloseorOpenSerial->setText("关闭串口");
serialStatus = true;
}
else
{
QMessageBox msgBox;
msgBox.setText("打开失败,串口可能被占用");
msgBox.setWindowTitle("串口错误");
msgBox.exec();
}
}
else
{
serialPort->close(); //关闭串口
ui->comboBox_databit->setEnabled(true);
ui->comboBox_fileCon->setEnabled(true);
ui->comboBox_stopbit->setEnabled(true);
ui->comboBox_baudrate->setEnabled(true);
ui->comboBox_checkbit->setEnabled(true);
ui->comboBox_serialNum->setEnabled(true);
ui->btnCloseorOpenSerial->setText("打开串口");
serialStatus = false;
}
}
标签:setEnabled,QSerialPort,QT,comboBox,第一战,serialPort,ui,串口
From: https://blog.csdn.net/qq_39465744/article/details/142964053