Quellcode durchsuchen

能够进行通过Modbus协议与从站进行通信

master
鹏鹏 李 vor 2 Tagen
Ursprung
Commit
6a48be6484
4 geänderte Dateien mit 71 neuen und 5 gelöschten Zeilen
  1. +1
    -1
      editor.pro.user
  2. +26
    -2
      plc.cpp
  3. +6
    -0
      plc.h
  4. +38
    -2
      plc.ui

+ 1
- 1
editor.pro.user Datei anzeigen

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2025-08-07T20:13:11. -->
<!-- Written by QtCreator 4.11.1, 2025-08-08T13:01:33. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>


+ 26
- 2
plc.cpp Datei anzeigen

@@ -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();
}
}

+ 6
- 0
plc.h Datei anzeigen

@@ -30,6 +30,12 @@ private slots:
void onListwidgetCurrenttextchanged(const QString &currentText);
void btnInsertClicked();

void on_pushButton_clicked();

void on_pushButton_2_clicked();

void updateConnectionStatus(bool connection);

private:
Ui::PLC *ui;
QGraphicsScene *plc_scene_;


+ 38
- 2
plc.ui Datei anzeigen

@@ -19,7 +19,7 @@
<x>10</x>
<y>30</y>
<width>171</width>
<height>451</height>
<height>401</height>
</rect>
</property>
</widget>
@@ -29,7 +29,7 @@
<x>200</x>
<y>30</y>
<width>761</width>
<height>451</height>
<height>401</height>
</rect>
</property>
<property name="dragMode">
@@ -49,6 +49,42 @@
<string>插入</string>
</property>
</widget>
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>10</x>
<y>440</y>
<width>951</width>
<height>87</height>
</rect>
</property>
</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>
<customwidgets>
<customwidget>


Laden…
Abbrechen
Speichern