#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("小源串口调试助手");
this->setWindowIcon(QIcon(":/123.jpg"));
serialPort=new QSerialPort(this);
//扫描本机的串口,并添加到下拉框
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()){
initPortStringList.append(info.portName());
}
ui->comboBox->addItems(initPortStringList);
//定时更新串口
timer=new QTimer(this);
connect(timer,&QTimer::timeout,this,&Widget::UpdatePort);
timer->start(1000);
//读取数据
connect(serialPort,SIGNAL(readyRead()),this,SLOT(readData()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked(bool checked)
{
if(checked==true){
//设置要打开的串口的名字
serialPort->setPortName(ui->comboBox->currentText());
//设置波特率
serialPort->setBaudRate(ui->comboBox_2->currentText().toInt());
//设置停止位
serialPort->setStopBits(QSerialPort::StopBits( ui->comboBox_3->currentText().toInt()));
//设置数据位
serialPort->setDataBits(QSerialPort::DataBits(ui->comboBox_4->currentText().toInt()));
//设置校验位
switch (ui->comboBox_5->currentIndex()) {
case 0:
serialPort->setParity(QSerialPort::NoParity);
break;
case 1:
serialPort->setParity(QSerialPort::EvenParity);
break;
case 2:
serialPort->setParity(QSerialPort::OddParity);
break;
case 3:
serialPort->setParity(QSerialPort::SpaceParity);
break;
case 4:
serialPort->setParity(QSerialPort::MarkParity);
break;
default:
break;
}
//设置流控
serialPort->setFlowControl(QSerialPort::NoFlowControl);
if(!serialPort->open(QIODevice::ReadWrite)){
QMessageBox::about(this,"错误","串口打开失败可能被占用!");
return;
}
ui->comboBox->setEnabled(false);
ui->comboBox_2->setEnabled(false);
ui->comboBox_3->setEnabled(false);
ui->comboBox_4->setEnabled(false);
ui->comboBox_5->setEnabled(false);
ui->pushButton->setText("关闭串口");
} else{
//关闭串口
serialPort->close();
ui->comboBox->setEnabled(true);
ui->comboBox_2->setEnabled(true);
ui->comboBox_3->setEnabled(true);
ui->comboBox_4->setEnabled(true);
ui->comboBox_5->setEnabled(true);
ui->pushButton->setText("打开串口");
}
}
void Widget::readData()
{
//接收数据
ui->textBrowser->insertPlainText( QString::fromUtf8( serialPort->readAll()));
}
void Widget::on_pushButton_2_clicked()
{
//发送数据
serialPort->write(ui->textEdit->toPlainText().toUtf8());
ui->textEdit->clear();
}
void Widget::on_pushButton_3_clicked()
{
//清除数据
ui->textEdit->clear();
}
//更新串口
void Widget::UpdatePort()
{
QStringList newportList;
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()){
newportList<<info.portName();
}
if(newportList.size()>initPortStringList.size())
{
initPortStringList=newportList;
ui->comboBox->clear();
ui->comboBox->addItems(initPortStringList);
qDebug()<<"更新串口";
}else if(newportList.size()<initPortStringList.size()){
initPortStringList=newportList;
ui->comboBox->clear();
ui->comboBox->addItems(initPortStringList);
qDebug()<<"更新串口2";
on_pushButton_clicked(false);
}
}
//清除接收窗口
void Widget::on_pushButton_4_clicked()
{
ui->textBrowser->clear();
}
//自动滚动
void Widget::on_textBrowser_textChanged()
{
ui->textBrowser->moveCursor(QTextCursor::End);
}
没有合适的资源?快使用搜索试试~ 我知道了~
QT源码-串口助手123456
共56个文件
qm:29个
dll:17个
cpp:2个
需积分: 5 0 下载量 108 浏览量
2022-11-13
19:24:48
上传
评论
收藏 42.85MB ZIP 举报
温馨提示
QT源码-串口助手123456
资源推荐
资源详情
资源评论
收起资源包目录
61.zip (56个子文件)
serialport
iconengines
qsvgicon.dll 67KB
libgcc_s_seh-1.dll 74KB
61_boxed.exe 55.11MB
Qt6Core.dll 6.18MB
Qt6SerialPort.dll 116KB
imageformats
qjpeg.dll 463KB
qico.dll 47KB
qgif.dll 47KB
qsvg.dll 39KB
61.exe 90KB
platforms
qwindows.dll 1002KB
libwinpthread-1.dll 52KB
libstdc++-6.dll 1.87MB
D3Dcompiler_47.dll 3.98MB
opengl32sw.dll 19.68MB
translations
qt_fi.qm 176KB
qt_fr.qm 162KB
qt_gd.qm 185KB
qt_ru.qm 201KB
qt_ko.qm 155KB
qt_hr.qm 146KB
qt_ca.qm 205KB
qt_pt_BR.qm 204KB
qt_pl.qm 161KB
qt_tr.qm 190KB
qt_de.qm 211KB
qt_nn.qm 192KB
qt_bg.qm 161KB
qt_uk.qm 157KB
qt_nl.qm 198KB
qt_en.qm 33B
qt_lv.qm 150KB
qt_hu.qm 157KB
qt_sk.qm 123KB
qt_cs.qm 171KB
qt_zh_CN.qm 135KB
qt_fa.qm 145KB
qt_ja.qm 129KB
qt_da.qm 177KB
qt_zh_TW.qm 123KB
qt_ar.qm 156KB
qt_he.qm 135KB
qt_es.qm 164KB
qt_it.qm 157KB
Qt6Gui.dll 9.15MB
styles
qwindowsvistastyle.dll 177KB
Qt6Widgets.dll 6.32MB
Qt6Svg.dll 350KB
widget.h 768B
widget.ui 8KB
main.cpp 177B
61.pro 646B
Image.qrc 91B
123.jpg 37KB
widget.cpp 4KB
61.pro.user 19KB
共 56 条
- 1
资源评论
源小帅
- 粉丝: 26
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于FPGA深度学习的SPI 驱动 HC595 点亮数码管
- 2023-2024学年教材费结算明细(公示) -V2.xls
- 129197954513812WPS.apk
- 1_2.cpp
- logxianshidddd
- 细胞的奇迹:吃出来的免疫力(美亚畅销书!作者的TED演讲播放量超300万次!《谷物大脑》作者、《三联生活周刊》推荐!吃下对的食物,改善你的... (Z-Library).azw3
- 智能手机产品拆解,使用mindmaster打开
- 11111111111111
- 统信系统ARM64依赖openssl-1.0.2版本的libssl.so.10和libcrypto.so.10文件
- 奥比中光python sdk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功