代码拉取完成,页面将自动刷新
#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();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。