@@ -1,6 +1,6 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||
<!DOCTYPE QtCreatorProject> | <!DOCTYPE QtCreatorProject> | ||||
<!-- Written by QtCreator 4.11.1, 2025-08-08T13:01:33. --> | |||||
<!-- Written by QtCreator 4.11.1, 2025-08-09T09:34:13. --> | |||||
<qtcreator> | <qtcreator> | ||||
<data> | <data> | ||||
<variable>EnvironmentId</variable> | <variable>EnvironmentId</variable> | ||||
@@ -19,8 +19,10 @@ MainWindow::MainWindow(QWidget *parent) | |||||
, ui(new Ui::MainWindow) | , ui(new Ui::MainWindow) | ||||
{ | { | ||||
ui->setupUi(this); | ui->setupUi(this); | ||||
plc_ = new PLC(ui->widget); | |||||
registerManager = new RegisterManager(this); | |||||
plc_ = new PLC(registerManager, ui->widget); | |||||
hmi_ = new HMI(ui->widget); | hmi_ = new HMI(ui->widget); | ||||
modbusManager = new ModbusManager(registerManager, this); | |||||
plc_->show(); | plc_->show(); | ||||
hmi_->hide(); | hmi_->hide(); | ||||
connect(ui->action_plc,&QAction::triggered,this,&MainWindow::plcChange); | connect(ui->action_plc,&QAction::triggered,this,&MainWindow::plcChange); | ||||
@@ -29,6 +31,11 @@ MainWindow::MainWindow(QWidget *parent) | |||||
connect(ui->action_new, &QAction::triggered, this, &MainWindow::newProject); | connect(ui->action_new, &QAction::triggered, this, &MainWindow::newProject); | ||||
connect(ui->action_save, &QAction::triggered, this, &MainWindow::saveProject); | connect(ui->action_save, &QAction::triggered, this, &MainWindow::saveProject); | ||||
connect(ui->action_open, &QAction::triggered, this, &MainWindow::openProject); | connect(ui->action_open, &QAction::triggered, this, &MainWindow::openProject); | ||||
connect(ui->action_connect, &QAction::triggered, this, &MainWindow::connection); | |||||
connect(ui->action_disconnect, &QAction::triggered, this, &MainWindow::disconnection); | |||||
connect(modbusManager, &ModbusManager::connectionStatusChanged, | |||||
plc_, &PLC::updateConnectionStatus); | |||||
plc_->applyProjectToScene(plcProject_); // 初始空 | plc_->applyProjectToScene(plcProject_); // 初始空 | ||||
@@ -133,3 +140,15 @@ void MainWindow::saveProject() | |||||
} | } | ||||
} | } | ||||
} | } | ||||
void MainWindow::connection() | |||||
{ | |||||
modbusManager->connectToDevice("COM1",QSerialPort::BaudRate(9600),QSerialPort::DataBits(8),QSerialPort::EvenParity,QSerialPort::StopBits(1)); | |||||
modbusManager->startSimulation(2000); | |||||
} | |||||
void MainWindow::disconnection() | |||||
{ | |||||
modbusManager->disconnectDevice(); | |||||
modbusManager->stopSimulation(); | |||||
} |
@@ -26,11 +26,15 @@ private slots: | |||||
void newProject(); | void newProject(); | ||||
void openProject(); | void openProject(); | ||||
void saveProject(); | void saveProject(); | ||||
void connection(); | |||||
void disconnection(); | |||||
private: | private: | ||||
Ui::MainWindow *ui; | Ui::MainWindow *ui; | ||||
RegisterManager* registerManager; | |||||
PLC *plc_; | PLC *plc_; | ||||
HMI *hmi_; | HMI *hmi_; | ||||
ModbusManager* modbusManager; | |||||
Project plcProject_; | Project plcProject_; | ||||
Project hmiProject_; | Project hmiProject_; | ||||
QString plcFilePath_; | QString plcFilePath_; | ||||
@@ -49,8 +49,16 @@ | |||||
<addaction name="action_plc"/> | <addaction name="action_plc"/> | ||||
<addaction name="action_hmi"/> | <addaction name="action_hmi"/> | ||||
</widget> | </widget> | ||||
<widget class="QMenu" name="menu_3"> | |||||
<property name="title"> | |||||
<string>仿真</string> | |||||
</property> | |||||
<addaction name="action_connect"/> | |||||
<addaction name="action_disconnect"/> | |||||
</widget> | |||||
<addaction name="menu"/> | <addaction name="menu"/> | ||||
<addaction name="menu_2"/> | <addaction name="menu_2"/> | ||||
<addaction name="menu_3"/> | |||||
</widget> | </widget> | ||||
<widget class="QStatusBar" name="statusbar"/> | <widget class="QStatusBar" name="statusbar"/> | ||||
<action name="action_new"> | <action name="action_new"> | ||||
@@ -78,6 +86,16 @@ | |||||
<string>HMI</string> | <string>HMI</string> | ||||
</property> | </property> | ||||
</action> | </action> | ||||
<action name="action_connect"> | |||||
<property name="text"> | |||||
<string>connect</string> | |||||
</property> | |||||
</action> | |||||
<action name="action_disconnect"> | |||||
<property name="text"> | |||||
<string>disconnect</string> | |||||
</property> | |||||
</action> | |||||
</widget> | </widget> | ||||
<resources/> | <resources/> | ||||
<connections/> | <connections/> | ||||
@@ -12,13 +12,13 @@ | |||||
#include <QMessageBox> | #include <QMessageBox> | ||||
#include "creatitem.h" | #include "creatitem.h" | ||||
PLC::PLC(QWidget *parent) : | |||||
PLC::PLC(RegisterManager *regManager, QWidget *parent) : | |||||
QWidget(parent), | QWidget(parent), | ||||
ui(new Ui::PLC) | |||||
ui(new Ui::PLC), | |||||
registerManager(regManager) | |||||
{ | { | ||||
ui->setupUi(this); | ui->setupUi(this); | ||||
registerManager = new RegisterManager(this); | |||||
modbusManager = new ModbusManager(registerManager, this); | |||||
// modbusManager = new ModbusManager(registerManager, this); | |||||
/* 1. 场景 */ | /* 1. 场景 */ | ||||
plc_scene_ = new QGraphicsScene(this); | plc_scene_ = new QGraphicsScene(this); | ||||
ui->graphicsView->setScene(plc_scene_); | ui->graphicsView->setScene(plc_scene_); | ||||
@@ -35,8 +35,8 @@ PLC::PLC(QWidget *parent) : | |||||
createComponents(); | createComponents(); | ||||
connect(ui->listWidget,&QListWidget::currentTextChanged,this,&PLC::onListwidgetCurrenttextchanged); | connect(ui->listWidget,&QListWidget::currentTextChanged,this,&PLC::onListwidgetCurrenttextchanged); | ||||
connect(ui->btn_insert,&QPushButton::clicked,this,&PLC::btnInsertClicked); | 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, | // connect(modbusManager, &ModbusManager::errorOccurred, | ||||
// this, &PLC::handleModbusError); | // this, &PLC::handleModbusError); | ||||
connect(ui->graphicsView, &MyGraphicsView::itemBoundToRegister, | connect(ui->graphicsView, &MyGraphicsView::itemBoundToRegister, | ||||
@@ -235,26 +235,14 @@ void PLC::btnInsertClicked() | |||||
ui->graphicsView->scene()->addItem(c2); | 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) | void PLC::updateConnectionStatus(bool connection) | ||||
{ | { | ||||
if (connection) | if (connection) | ||||
{ | { | ||||
ui->textEdit->append("连接"); | ui->textEdit->append("连接"); | ||||
modbusManager->startSimulation(2000); | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
ui->textEdit->append("断开"); | ui->textEdit->append("断开"); | ||||
modbusManager->stopSimulation(); | |||||
} | } | ||||
} | } |
@@ -15,7 +15,7 @@ class PLC : public QWidget | |||||
Q_OBJECT | Q_OBJECT | ||||
public: | public: | ||||
explicit PLC(QWidget *parent = nullptr); | |||||
explicit PLC(RegisterManager* regManager, QWidget *parent = nullptr); | |||||
~PLC(); | ~PLC(); | ||||
void createComponents(); | void createComponents(); | ||||
@@ -26,20 +26,15 @@ public: | |||||
protected: | protected: | ||||
bool eventFilter(QObject *obj, QEvent *event) override; | bool eventFilter(QObject *obj, QEvent *event) override; | ||||
private slots: | |||||
public slots: | |||||
void onListwidgetCurrenttextchanged(const QString ¤tText); | void onListwidgetCurrenttextchanged(const QString ¤tText); | ||||
void btnInsertClicked(); | void btnInsertClicked(); | ||||
void on_pushButton_clicked(); | |||||
void on_pushButton_2_clicked(); | |||||
void updateConnectionStatus(bool connection); | void updateConnectionStatus(bool connection); | ||||
private: | private: | ||||
Ui::PLC *ui; | Ui::PLC *ui; | ||||
QGraphicsScene *plc_scene_; | QGraphicsScene *plc_scene_; | ||||
ModbusManager* modbusManager; | |||||
RegisterManager* registerManager; | RegisterManager* registerManager; | ||||
QString selectedComponentType; | QString selectedComponentType; | ||||
}; | }; | ||||
@@ -59,32 +59,6 @@ | |||||
</rect> | </rect> | ||||
</property> | </property> | ||||
</widget> | </widget> | ||||
<widget class="QPushButton" name="pushButton"> | |||||
<property name="geometry"> | |||||
<rect> | |||||
<x>120</x> | |||||
<y>0</y> | |||||
<width>93</width> | |||||
<height>28</height> | |||||
</rect> | |||||
</property> | |||||
<property name="text"> | |||||
<string>连接</string> | |||||
</property> | |||||
</widget> | |||||
<widget class="QPushButton" name="pushButton_2"> | |||||
<property name="geometry"> | |||||
<rect> | |||||
<x>230</x> | |||||
<y>0</y> | |||||
<width>93</width> | |||||
<height>28</height> | |||||
</rect> | |||||
</property> | |||||
<property name="text"> | |||||
<string>断开</string> | |||||
</property> | |||||
</widget> | |||||
</widget> | </widget> | ||||
<customwidgets> | <customwidgets> | ||||
<customwidget> | <customwidget> | ||||