commit 05f9e0a5b9f1a9f34b598f39d33d40958cd05cbd Author: lipengpeng Date: Wed Jul 23 18:42:07 2025 +0800 连接串口功能 diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..90b6d53 --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "widget.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + + return a.exec(); +} diff --git a/modbus.pro b/modbus.pro new file mode 100644 index 0000000..1eccdc5 --- /dev/null +++ b/modbus.pro @@ -0,0 +1,21 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2025-07-23T11:43:43 +# +#------------------------------------------------- + +QT += core gui +QT += serialport + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = modbus +TEMPLATE = app + + +SOURCES += main.cpp\ + widget.cpp + +HEADERS += widget.h + +FORMS += widget.ui diff --git a/modbus.pro.user b/modbus.pro.user new file mode 100644 index 0000000..d691afe --- /dev/null +++ b/modbus.pro.user @@ -0,0 +1,318 @@ + + + + + + EnvironmentId + {1084213a-f698-4faa-95c4-704b6a0911a5} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.6.3 MinGW 32bit + Desktop Qt 5.6.3 MinGW 32bit + qt.563.win32_mingw49_kit + 0 + 0 + 0 + + D:/work/modbus/lipengpeng/build-modbus-Desktop_Qt_5_6_3_MinGW_32bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + 构建 + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + 清理 + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + D:/work/modbus/lipengpeng/build-modbus-Desktop_Qt_5_6_3_MinGW_32bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + 构建 + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + 清理 + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + D:/work/modbus/lipengpeng/build-modbus-Desktop_Qt_5_6_3_MinGW_32bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + 构建 + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + 清理 + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + 部署 + + ProjectExplorer.BuildSteps.Deploy + + 1 + 在本地部署 + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + modbus + + Qt4ProjectManager.Qt4RunConfiguration:D:/work/modbus/lipengpeng/modbus/modbus.pro + true + + modbus.pro + false + + D:/work/modbus/lipengpeng/build-modbus-Desktop_Qt_5_6_3_MinGW_32bit-Debug + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/widget.cpp b/widget.cpp new file mode 100644 index 0000000..783d31e --- /dev/null +++ b/widget.cpp @@ -0,0 +1,90 @@ +#include "widget.h" +#include "ui_widget.h" +#include +#include +#include +#include + +Widget::Widget(QWidget *parent) : + QWidget(parent), + ui(new Ui::Widget) +{ + ui->setupUi(this); + serialPort = new QSerialPort(this); + + connect(serialPort,&QSerialPort::readyRead,this,&Widget::on_SerialData_ReadyToRead); + + ui->comboBox_baudRate->setCurrentIndex(3); + ui->comboBox_dataBit->setCurrentIndex(3); + ui->comboBox_xiaoyan->setCurrentIndex(2); + + QList serialList = QSerialPortInfo::availablePorts(); + for(QSerialPortInfo serialInfo : serialList) + { + ui->comboBox_serialNum->addItem(serialInfo.portName()); + } +} + +Widget::~Widget() +{ + delete ui; +} + +void Widget::on_btnConnect_clicked() +{ + if (ui->btnConnect->text() == "连接") + { + //配置串口号 + serialPort->setPortName(ui->comboBox_serialNum->currentText()); + //配置波特率 + serialPort->setBaudRate(ui->comboBox_baudRate->currentText().toInt()); + //配置数据位 + serialPort->setDataBits(QSerialPort::DataBits(ui->comboBox_dataBit->currentText().toInt())); + //配置校验位 + switch (ui->comboBox_xiaoyan->currentIndex()) + { + case 0: + serialPort->setParity(QSerialPort::NoParity); + break; + case 1: + serialPort->setParity(QSerialPort::OddParity); + break; + case 2: + serialPort->setParity(QSerialPort::EvenParity); + } + //配置停止位 + serialPort->setStopBits(QSerialPort::StopBits(ui->comboBox_stopBit->currentText().toInt())); + //打开串口 + if (serialPort->open(QIODevice::ReadWrite)) + { + qDebug() << "Serial open success"; + ui->btnConnect->setText("断开"); + } + else + { + qDebug() << "error"; + } + } + else + { + serialPort->close(); + ui->btnConnect->setText("连接"); + } + +} + +void Widget::on_pushWrite_clicked() +{ + const char *sendData = ui->lineEdit->text().toStdString().c_str(); + serialPort->write(sendData); + qDebug() << "SenOk" <readAll(); + QString hexData = revMessage.toHex().toUpper(); + qDebug() << hexData; + ui->textEdit->append(hexData); + +} diff --git a/widget.h b/widget.h new file mode 100644 index 0000000..7d769b1 --- /dev/null +++ b/widget.h @@ -0,0 +1,31 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include +#include + +namespace Ui { +class Widget; +} + +class Widget : public QWidget +{ + Q_OBJECT + +public: + explicit Widget(QWidget *parent = 0); + ~Widget(); + +private slots: + void on_btnConnect_clicked(); + + void on_pushWrite_clicked(); + + void on_SerialData_ReadyToRead(); + +private: + Ui::Widget *ui; + QSerialPort *serialPort; +}; + +#endif // WIDGET_H diff --git a/widget.ui b/widget.ui new file mode 100644 index 0000000..5a3130d --- /dev/null +++ b/widget.ui @@ -0,0 +1,261 @@ + + + Widget + + + + 0 + 0 + 787 + 543 + + + + Widget + + + + + 31 + 31 + 30 + 16 + + + + 串口 + + + + + + 68 + 31 + 87 + 21 + + + + + + + 162 + 31 + 45 + 16 + + + + 波特率 + + + + + + 308 + 31 + 45 + 16 + + + + 数据位 + + + + + + 214 + 31 + 87 + 21 + + + + + 1200 + + + + + 2400 + + + + + 4800 + + + + + 9600 + + + + + 19200 + + + + + 38400 + + + + + + + 360 + 31 + 61 + 21 + + + + + 5 + + + + + 6 + + + + + 7 + + + + + 8 + + + + + NONE + + + + + + + 480 + 30 + 93 + 28 + + + + 连接 + + + + + + 80 + 70 + 91 + 21 + + + + + 无校验(N) + + + + + 奇校验(O) + + + + + 偶校验(E) + + + + + + + 30 + 70 + 51 + 21 + + + + 校验位 + + + + + + 198 + 70 + 45 + 16 + + + + 停止位 + + + + + + 250 + 70 + 61 + 21 + + + + + 1 + + + + + 2 + + + + + + + 20 + 150 + 231 + 21 + + + + + + + 270 + 150 + 93 + 28 + + + + 发送 + + + + + + 20 + 180 + 251 + 151 + + + + + + + +