-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
104 lines (85 loc) · 3.74 KB
/
main.py
File metadata and controls
104 lines (85 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# main.py - GUI 主程序
import sys
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel, QSpinBox, QPushButton, QStatusBar
)
from PyQt5.QtCore import Qt
from plot_widget import GasPlotWidget, TempPlotWidget, RhPlotWidget
from modbus_comm import ModbusPoller
class SensorGUI(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Gas Sensor Monitor")
self.resize(1400, 900)
# === 数据变量 ===
self.time_counter = 0
self.update_interval = 2 # 与 Modbus 轮询周期匹配
# 主容器
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QVBoxLayout(central_widget)
# 标题
title_label = QLabel("Gas Sensor Monitor")
title_label.setAlignment(Qt.AlignCenter)
title_label.setStyleSheet("font-size: 20px; font-weight: bold; color: #333;")
main_layout.addWidget(title_label)
# 参数设置行
ctrl_layout = QHBoxLayout()
ctrl_layout.addWidget(QLabel("Sampling Interval (s):"))
self.rate_spinbox = QSpinBox()
self.rate_spinbox.setRange(1, 3600)
self.rate_spinbox.setValue(2)
self.set_rate_button = QPushButton("Set")
ctrl_layout.addWidget(self.rate_spinbox)
ctrl_layout.addWidget(self.set_rate_button)
ctrl_layout.addStretch()
main_layout.addLayout(ctrl_layout)
# === 温湿度图表 ===
temp_rh_layout = QHBoxLayout()
self.temp_plot = TempPlotWidget()
self.rh_plot = RhPlotWidget()
temp_rh_layout.addWidget(self.temp_plot, stretch=1)
temp_rh_layout.addWidget(self.rh_plot, stretch=1)
main_layout.addLayout(temp_rh_layout, stretch=1)
# === 气体图表(3x2)===
gas_layout = QGridLayout()
gas_layout.setSpacing(10)
self.gas_plots = {}
gases = ['O2', 'CO2', 'CH4', 'H2', 'C2H4', 'CH3OH']
colors = ['b', 'g', 'r', 'c', 'm', 'y']
units = ['%VOL', 'ppm', 'ppm', 'ppm', 'ppm', '%VOL']
for i, (gas, color, unit) in enumerate(zip(gases, colors, units)):
row = i // 2
col = i % 2
plot = GasPlotWidget(gas, unit, color)
self.gas_plots[gas] = plot
gas_layout.addWidget(plot, row, col)
main_layout.addLayout(gas_layout, stretch=2)
# 状态栏
self.statusbar = QStatusBar()
self.setStatusBar(self.statusbar)
# === 启动 Modbus 线程 ===
self.modbus_thread = ModbusPoller(port='COM3')
self.modbus_thread.data_received.connect(self.update_plots)
self.modbus_thread.status_updated.connect(self.statusbar.showMessage)
self.modbus_thread.error_occurred.connect(lambda msg: self.statusbar.showMessage(f"ERROR: {msg}", 3000))
self.modbus_thread.start()
def update_plots(self, gas_name, concentration, temperature, humidity):
"""接收 Modbus 数据并更新图表"""
if gas_name in self.gas_plots:
self.gas_plots[gas_name].update_plot(self.time_counter, concentration)
# 假设所有设备温湿度一致,每收到一次数据就更新
self.temp_plot.update_plot(self.time_counter, temperature)
self.rh_plot.update_plot(self.time_counter, humidity)
# 更新时间
self.time_counter += self.update_interval / 3 # 因为每个设备读3次,平均时间步长
def closeEvent(self, event):
"""窗口关闭时停止线程"""
self.modbus_thread.stop()
self.modbus_thread.wait(1000) # 等待1秒
event.accept()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = SensorGUI()
window.show()
sys.exit(app.exec_())