新增模拟硬件测试功能
This commit is contained in:
@@ -4,15 +4,21 @@
|
||||
纯业务逻辑,无 UI 依赖。通过回调与 UI 层通信。
|
||||
"""
|
||||
|
||||
import time
|
||||
from PcControl import MT2AM8Client
|
||||
import os
|
||||
import time
|
||||
from PcControl import MT2AM8Client
|
||||
from core.simulated_device import SimulatedDevice
|
||||
|
||||
|
||||
class ConnectionManager:
|
||||
"""管理 MT2-AM8 模块连接的生命周期"""
|
||||
|
||||
def __init__(self):
|
||||
self.modbus_client = None # MT2AM8Client (TCP 读压力/流量,控电机)
|
||||
def __init__(self, simulation=None):
|
||||
self.modbus_client = None # MT2AM8Client (TCP 读压力/流量,控电机)
|
||||
if simulation is None:
|
||||
simulation = os.environ.get("REINLOOP_SIMULATION", "").strip().lower() \
|
||||
in {"1", "true", "yes", "on"}
|
||||
self.simulation = bool(simulation)
|
||||
self._pressure_addr = 0 # 压力传感器模拟量通道地址
|
||||
self._flowmeter_addr = None # 流量计模拟量通道地址(None=使用手动输入)
|
||||
self._motor_addr = 0 # 电机模拟量输出通道地址
|
||||
@@ -61,19 +67,28 @@ class ConnectionManager:
|
||||
self._motor_addr = motor_addr
|
||||
self._flowmeter_addr = flowmeter_addr
|
||||
|
||||
# 创建 MT2-AM8 客户端
|
||||
self.modbus_client = MT2AM8Client(
|
||||
host=tcp_ip,
|
||||
port=tcp_port,
|
||||
pressure_range=pressure_range,
|
||||
flow_range=flow_range,
|
||||
)
|
||||
|
||||
if not self.modbus_client.connect():
|
||||
self.log(f"连接 MT2-AM8 模块失败: {tcp_ip}:{tcp_port}")
|
||||
return False
|
||||
|
||||
self.log(f"成功连接到 MT2-AM8 模块: {tcp_ip}:{tcp_port}")
|
||||
if self.simulation:
|
||||
self.modbus_client = SimulatedDevice(
|
||||
pressure_range=pressure_range,
|
||||
flow_range=flow_range,
|
||||
)
|
||||
if not self.modbus_client.connect():
|
||||
return False
|
||||
self.log("模拟设备已连接(未访问真实 TCP/串口硬件)")
|
||||
else:
|
||||
# 真实硬件路径:保留原有 MT2-AM8 Modbus TCP 实现。
|
||||
self.modbus_client = MT2AM8Client(
|
||||
host=tcp_ip,
|
||||
port=tcp_port,
|
||||
pressure_range=pressure_range,
|
||||
flow_range=flow_range,
|
||||
)
|
||||
|
||||
if not self.modbus_client.connect():
|
||||
self.log(f"连接 MT2-AM8 模块失败: {tcp_ip}:{tcp_port}")
|
||||
return False
|
||||
|
||||
self.log(f"成功连接到 MT2-AM8 模块: {tcp_ip}:{tcp_port}")
|
||||
self.log(f"压力地址: {pressure_addr}, 电机地址: {motor_addr}, 流量计地址: {flowmeter_addr}")
|
||||
|
||||
if self._on_status_change:
|
||||
|
||||
Reference in New Issue
Block a user