From 6a48be6484f8b4dfaded72f2b5038d8224f86a07 Mon Sep 17 00:00:00 2001 From: lipengpeng Date: Fri, 8 Aug 2025 14:02:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=BD=E5=A4=9F=E8=BF=9B=E8=A1=8C=E9=80=9A?= =?UTF-8?q?=E8=BF=87Modbus=E5=8D=8F=E8=AE=AE=E4=B8=8E=E4=BB=8E=E7=AB=99?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E9=80=9A=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- editor.pro.user | 2 +- plc.cpp | 28 ++++++++++++++++++++++++++-- plc.h | 6 ++++++ plc.ui | 40 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/editor.pro.user b/editor.pro.user index 6933c03..916a4d0 100644 --- a/editor.pro.user +++ b/editor.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/plc.cpp b/plc.cpp index 1818534..3c978dc 100644 --- a/plc.cpp +++ b/plc.cpp @@ -35,8 +35,8 @@ PLC::PLC(QWidget *parent) : createComponents(); connect(ui->listWidget,&QListWidget::currentTextChanged,this,&PLC::onListwidgetCurrenttextchanged); connect(ui->btn_insert,&QPushButton::clicked,this,&PLC::btnInsertClicked); -// connect(modbusManager, &ModbusManager::connectionStatusChanged, -// this, &PLC::updateConnectionStatus); + connect(modbusManager, &ModbusManager::connectionStatusChanged, + this, &PLC::updateConnectionStatus); // connect(modbusManager, &ModbusManager::errorOccurred, // this, &PLC::handleModbusError); connect(ui->graphicsView, &MyGraphicsView::itemBoundToRegister, @@ -233,3 +233,27 @@ void PLC::btnInsertClicked() Connection* c2 = new Connection(newItem, Item::Right, to, toType); ui->graphicsView->scene()->addItem(c2); } + +void PLC::on_pushButton_clicked() +{ + modbusManager->connectToDevice("COM1",QSerialPort::BaudRate(9600),QSerialPort::DataBits(8),QSerialPort::EvenParity,QSerialPort::StopBits(1)); +} + +void PLC::on_pushButton_2_clicked() +{ + modbusManager->disconnectDevice(); +} + +void PLC::updateConnectionStatus(bool connection) +{ + if (connection) + { + ui->textEdit->append("连接"); + modbusManager->startSimulation(2000); + } + else + { + ui->textEdit->append("断开"); + modbusManager->stopSimulation(); + } +} diff --git a/plc.h b/plc.h index 906f8c0..99c7f7e 100644 --- a/plc.h +++ b/plc.h @@ -30,6 +30,12 @@ private slots: void onListwidgetCurrenttextchanged(const QString ¤tText); void btnInsertClicked(); + void on_pushButton_clicked(); + + void on_pushButton_2_clicked(); + + void updateConnectionStatus(bool connection); + private: Ui::PLC *ui; QGraphicsScene *plc_scene_; diff --git a/plc.ui b/plc.ui index 33a7d94..c595c9f 100644 --- a/plc.ui +++ b/plc.ui @@ -19,7 +19,7 @@ 10 30 171 - 451 + 401 @@ -29,7 +29,7 @@ 200 30 761 - 451 + 401 @@ -49,6 +49,42 @@ 插入 + + + + 10 + 440 + 951 + 87 + + + + + + + 120 + 0 + 93 + 28 + + + + 连接 + + + + + + 230 + 0 + 93 + 28 + + + + 断开 + +