|
|
|
@ -2,6 +2,8 @@ from utils.DBModels.ProtocolModel import (
|
|
|
|
ModbusTcpMasterVar, ModbusTcpSlaveVar, ModbusRtuMasterVar, ModbusRtuSlaveVar,
|
|
|
|
ModbusTcpMasterVar, ModbusTcpSlaveVar, ModbusRtuMasterVar, ModbusRtuSlaveVar,
|
|
|
|
HartVar, TcRtdVar, AnalogVar, HartSimulateVar
|
|
|
|
HartVar, TcRtdVar, AnalogVar, HartSimulateVar
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
from model.ProjectModel.GlobalConfigManager import GlobalConfigManager
|
|
|
|
|
|
|
|
from protocol.HartRtuSlaveManager import HartRtuSlaveManager
|
|
|
|
|
|
|
|
|
|
|
|
from protocol.TCP.TCPVarManage import *
|
|
|
|
from protocol.TCP.TCPVarManage import *
|
|
|
|
from protocol.TCP.TemToMv import temToMv
|
|
|
|
from protocol.TCP.TemToMv import temToMv
|
|
|
|
@ -15,18 +17,21 @@ import time
|
|
|
|
class ProtocolManage(object):
|
|
|
|
class ProtocolManage(object):
|
|
|
|
"""通讯变量查找类,用于根据变量名在数据库模型中检索变量信息"""
|
|
|
|
"""通讯变量查找类,用于根据变量名在数据库模型中检索变量信息"""
|
|
|
|
|
|
|
|
|
|
|
|
# 支持的模型类列表
|
|
|
|
|
|
|
|
MODEL_CLASSES = [
|
|
|
|
|
|
|
|
ModbusTcpMasterVar, ModbusTcpSlaveVar,
|
|
|
|
|
|
|
|
ModbusRtuMasterVar, ModbusRtuSlaveVar,
|
|
|
|
|
|
|
|
HartVar, TcRtdVar, AnalogVar, HartSimulateVar
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
# self.tcpVarManager = TCPVarManager('192.168.1.50', 5055)
|
|
|
|
# 根据全局配置动态获取启用的模型类
|
|
|
|
self.tcpVarManager = TCPVarManager('127.0.0.1', 8000)
|
|
|
|
self.MODEL_CLASSES = GlobalConfigManager.getEnabledModelClasses()
|
|
|
|
self.writeTC = [0] * 8
|
|
|
|
|
|
|
|
self.writeRTD = [0] * 8
|
|
|
|
# 根据配置决定是否初始化TCPVarManager(仅当启用IO或TCRTD模块时)
|
|
|
|
|
|
|
|
if GlobalConfigManager.needsTcpVarManager():
|
|
|
|
|
|
|
|
# self.tcpVarManager = TCPVarManager('192.168.1.50', 5055)
|
|
|
|
|
|
|
|
self.tcpVarManager = TCPVarManager('127.0.0.1', 8000)
|
|
|
|
|
|
|
|
self.writeTC = [0] * 8
|
|
|
|
|
|
|
|
self.writeRTD = [0] * 8
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.tcpVarManager = None
|
|
|
|
|
|
|
|
self.writeTC = None
|
|
|
|
|
|
|
|
self.writeRTD = None
|
|
|
|
|
|
|
|
|
|
|
|
self.RpcClient = None
|
|
|
|
self.RpcClient = None
|
|
|
|
self.RpcServer = None
|
|
|
|
self.RpcServer = None
|
|
|
|
self.varInfoCache = {} # 保持驼峰命名
|
|
|
|
self.varInfoCache = {} # 保持驼峰命名
|
|
|
|
@ -37,6 +42,16 @@ class ProtocolManage(object):
|
|
|
|
self.modbusManager = ModbusManager()
|
|
|
|
self.modbusManager = ModbusManager()
|
|
|
|
# self.modbusManager.setVariableCache(self.variableValueCache, None, self.varInfoCache)
|
|
|
|
# self.modbusManager.setVariableCache(self.variableValueCache, None, self.varInfoCache)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# HART模拟RTU从站管理器
|
|
|
|
|
|
|
|
self.hartRtuSlaveManager = None
|
|
|
|
|
|
|
|
if GlobalConfigManager.isModuleEnabled('hartSimulateModule'):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
self.hartRtuSlaveManager = HartRtuSlaveManager()
|
|
|
|
|
|
|
|
print("HART模拟RTU从站管理器已初始化")
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
print(f"初始化HART模拟RTU从站管理器失败: {e}")
|
|
|
|
|
|
|
|
self.hartRtuSlaveManager = None
|
|
|
|
|
|
|
|
|
|
|
|
self.refreshVarCache()
|
|
|
|
self.refreshVarCache()
|
|
|
|
self.cacheLock = threading.Lock()
|
|
|
|
self.cacheLock = threading.Lock()
|
|
|
|
# 设置 Modbus 管理器的缓存锁
|
|
|
|
# 设置 Modbus 管理器的缓存锁
|
|
|
|
@ -53,6 +68,8 @@ class ProtocolManage(object):
|
|
|
|
def refreshVarCache(self):
|
|
|
|
def refreshVarCache(self):
|
|
|
|
"""重新加载所有变量信息到缓存(可选实现)"""
|
|
|
|
"""重新加载所有变量信息到缓存(可选实现)"""
|
|
|
|
self.varInfoCache.clear()
|
|
|
|
self.varInfoCache.clear()
|
|
|
|
|
|
|
|
# 重新获取最新的启用模型类列表
|
|
|
|
|
|
|
|
self.MODEL_CLASSES = GlobalConfigManager.getEnabledModelClasses()
|
|
|
|
for modelClass in self.MODEL_CLASSES:
|
|
|
|
for modelClass in self.MODEL_CLASSES:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
for varInstance in modelClass.select():
|
|
|
|
for varInstance in modelClass.select():
|
|
|
|
@ -175,6 +192,10 @@ class ProtocolManage(object):
|
|
|
|
|
|
|
|
|
|
|
|
# 温度/RTD变量处理
|
|
|
|
# 温度/RTD变量处理
|
|
|
|
elif modelType == 'TcRtdVar':
|
|
|
|
elif modelType == 'TcRtdVar':
|
|
|
|
|
|
|
|
if not self.tcpVarManager:
|
|
|
|
|
|
|
|
print("TCPVarManager未初始化,无法处理TcRtdVar变量")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
channel = int(info['channelNumber']) - 1
|
|
|
|
channel = int(info['channelNumber']) - 1
|
|
|
|
varType = info['varType']
|
|
|
|
varType = info['varType']
|
|
|
|
compensationVar = float(info['compensationVar'])
|
|
|
|
compensationVar = float(info['compensationVar'])
|
|
|
|
@ -195,6 +216,10 @@ class ProtocolManage(object):
|
|
|
|
|
|
|
|
|
|
|
|
# 模拟量变量处理
|
|
|
|
# 模拟量变量处理
|
|
|
|
elif modelType == 'AnalogVar':
|
|
|
|
elif modelType == 'AnalogVar':
|
|
|
|
|
|
|
|
if not self.tcpVarManager:
|
|
|
|
|
|
|
|
print("TCPVarManager未初始化,无法处理AnalogVar变量")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
channel = int(info['channelNumber']) - 1
|
|
|
|
channel = int(info['channelNumber']) - 1
|
|
|
|
varType = info['varType']
|
|
|
|
varType = info['varType']
|
|
|
|
varModel = info['varModel']
|
|
|
|
varModel = info['varModel']
|
|
|
|
@ -206,7 +231,27 @@ class ProtocolManage(object):
|
|
|
|
|
|
|
|
|
|
|
|
# HART模拟变量处理
|
|
|
|
# HART模拟变量处理
|
|
|
|
elif modelType == 'HartSimulateVar':
|
|
|
|
elif modelType == 'HartSimulateVar':
|
|
|
|
pass
|
|
|
|
if not self.hartRtuSlaveManager:
|
|
|
|
|
|
|
|
print("HART RTU从站管理器未初始化,无法处理HartSimulateVar变量")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 根据变量名确定是主变量还是动态变量
|
|
|
|
|
|
|
|
varName = variableName.lower()
|
|
|
|
|
|
|
|
if 'primary' in varName or '主变量' in variableName:
|
|
|
|
|
|
|
|
success = self.hartRtuSlaveManager.writeVariable('primaryVariable', float(value))
|
|
|
|
|
|
|
|
elif 'dynamic1' in varName or '动态变量1' in variableName:
|
|
|
|
|
|
|
|
success = self.hartRtuSlaveManager.writeVariable('dynamicVariable1', float(value))
|
|
|
|
|
|
|
|
elif 'dynamic2' in varName or '动态变量2' in variableName:
|
|
|
|
|
|
|
|
success = self.hartRtuSlaveManager.writeVariable('dynamicVariable2', float(value))
|
|
|
|
|
|
|
|
elif 'dynamic3' in varName or '动态变量3' in variableName:
|
|
|
|
|
|
|
|
success = self.hartRtuSlaveManager.writeVariable('dynamicVariable3', float(value))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# 默认写入主变量
|
|
|
|
|
|
|
|
success = self.hartRtuSlaveManager.writeVariable('primaryVariable', float(value))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
|
|
|
|
print(f"HART模拟变量 {variableName} 写入失败")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
if self.RpcClient:
|
|
|
|
if self.RpcClient:
|
|
|
|
self.RpcClient.setVarContent(variableName, value, info['min'], info['max'], info['varType'])
|
|
|
|
self.RpcClient.setVarContent(variableName, value, info['min'], info['max'], info['varType'])
|
|
|
|
@ -286,6 +331,9 @@ class ProtocolManage(object):
|
|
|
|
elif modelType == 'HartVar':
|
|
|
|
elif modelType == 'HartVar':
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
elif modelType == 'TcRtdVar':
|
|
|
|
elif modelType == 'TcRtdVar':
|
|
|
|
|
|
|
|
if not self.tcpVarManager:
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
channel = int(info['channelNumber']) - 1
|
|
|
|
channel = int(info['channelNumber']) - 1
|
|
|
|
varType = info['varType']
|
|
|
|
varType = info['varType']
|
|
|
|
varModel = info['varModel']
|
|
|
|
varModel = info['varModel']
|
|
|
|
@ -297,10 +345,13 @@ class ProtocolManage(object):
|
|
|
|
value = self.tcpVarManager.simTCData[channel]
|
|
|
|
value = self.tcpVarManager.simTCData[channel]
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
if varType == 'PT100':
|
|
|
|
if varType == 'PT100':
|
|
|
|
value = self.writeRTD[channel]
|
|
|
|
value = self.writeRTD[channel] if self.writeRTD else 0
|
|
|
|
elif varType in ['R', 'S', 'B', 'J', 'T', 'E', 'K', 'N', 'C', 'A']:
|
|
|
|
elif varType in ['R', 'S', 'B', 'J', 'T', 'E', 'K', 'N', 'C', 'A']:
|
|
|
|
value = self.writeTC[channel]
|
|
|
|
value = self.writeTC[channel] if self.writeTC else 0
|
|
|
|
elif modelType == 'AnalogVar':
|
|
|
|
elif modelType == 'AnalogVar':
|
|
|
|
|
|
|
|
if not self.tcpVarManager:
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
channel = int(info['channelNumber']) - 1
|
|
|
|
channel = int(info['channelNumber']) - 1
|
|
|
|
varType = info['varType']
|
|
|
|
varType = info['varType']
|
|
|
|
varModel = info['varModel']
|
|
|
|
varModel = info['varModel']
|
|
|
|
@ -309,7 +360,22 @@ class ProtocolManage(object):
|
|
|
|
if varType in ['AI', 'AO']:
|
|
|
|
if varType in ['AI', 'AO']:
|
|
|
|
value = self.getRealAI(value, info['max'], info['min'])
|
|
|
|
value = self.getRealAI(value, info['max'], info['min'])
|
|
|
|
elif modelType == 'HartSimulateVar':
|
|
|
|
elif modelType == 'HartSimulateVar':
|
|
|
|
pass
|
|
|
|
if not self.hartRtuSlaveManager:
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 根据变量名确定是主变量还是动态变量
|
|
|
|
|
|
|
|
varName = variableName.lower()
|
|
|
|
|
|
|
|
if 'primary' in varName or '主变量' in variableName:
|
|
|
|
|
|
|
|
value = self.hartRtuSlaveManager.readVariable('primaryVariable')
|
|
|
|
|
|
|
|
elif 'dynamic1' in varName or '动态变量1' in variableName:
|
|
|
|
|
|
|
|
value = self.hartRtuSlaveManager.readVariable('dynamicVariable1')
|
|
|
|
|
|
|
|
elif 'dynamic2' in varName or '动态变量2' in variableName:
|
|
|
|
|
|
|
|
value = self.hartRtuSlaveManager.readVariable('dynamicVariable2')
|
|
|
|
|
|
|
|
elif 'dynamic3' in varName or '动态变量3' in variableName:
|
|
|
|
|
|
|
|
value = self.hartRtuSlaveManager.readVariable('dynamicVariable3')
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# 默认读取主变量
|
|
|
|
|
|
|
|
value = self.hartRtuSlaveManager.readVariable('primaryVariable')
|
|
|
|
if value is not None and value != 'error':
|
|
|
|
if value is not None and value != 'error':
|
|
|
|
if self.RpcClient:
|
|
|
|
if self.RpcClient:
|
|
|
|
self.RpcClient.setVarContent(variableName, value, info['min'], info['max'], info['varType'])
|
|
|
|
self.RpcClient.setVarContent(variableName, value, info['min'], info['max'], info['varType'])
|
|
|
|
@ -328,12 +394,18 @@ class ProtocolManage(object):
|
|
|
|
return self._readVariableValueOriginal(variableName)
|
|
|
|
return self._readVariableValueOriginal(variableName)
|
|
|
|
|
|
|
|
|
|
|
|
def recvDeltaT(self):
|
|
|
|
def recvDeltaT(self):
|
|
|
|
return self.tcpVarManager.recvDeltaT()
|
|
|
|
if self.tcpVarManager:
|
|
|
|
|
|
|
|
return self.tcpVarManager.recvDeltaT()
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
def shutdown(self):
|
|
|
|
self.tcpVarManager.shutdown()
|
|
|
|
if self.tcpVarManager:
|
|
|
|
|
|
|
|
self.tcpVarManager.shutdown()
|
|
|
|
# 关闭所有Modbus通讯
|
|
|
|
# 关闭所有Modbus通讯
|
|
|
|
self.modbusManager.stopAllModbus()
|
|
|
|
self.modbusManager.stopAllModbus()
|
|
|
|
|
|
|
|
# 关闭HART RTU从站
|
|
|
|
|
|
|
|
if self.hartRtuSlaveManager:
|
|
|
|
|
|
|
|
self.hartRtuSlaveManager.stopSlave()
|
|
|
|
self.closeClient()
|
|
|
|
self.closeClient()
|
|
|
|
self.closeServer()
|
|
|
|
self.closeServer()
|
|
|
|
# 关闭后台读取线程
|
|
|
|
# 关闭后台读取线程
|
|
|
|
@ -436,3 +508,28 @@ class ProtocolManage(object):
|
|
|
|
def clearModbusMessages(self):
|
|
|
|
def clearModbusMessages(self):
|
|
|
|
"""清空 Modbus 报文记录"""
|
|
|
|
"""清空 Modbus 报文记录"""
|
|
|
|
self.modbusManager.clearMessages()
|
|
|
|
self.modbusManager.clearMessages()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ==================== HART模拟RTU从站管理方法 ====================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def startHartRtuSlave(self):
|
|
|
|
|
|
|
|
"""启动HART RTU从站"""
|
|
|
|
|
|
|
|
if not self.hartRtuSlaveManager:
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
return self.hartRtuSlaveManager.startSlave()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def stopHartRtuSlave(self):
|
|
|
|
|
|
|
|
"""停止HART RTU从站"""
|
|
|
|
|
|
|
|
if self.hartRtuSlaveManager:
|
|
|
|
|
|
|
|
self.hartRtuSlaveManager.stopSlave()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def writeHartVariable(self, variableName: str, value: float) -> bool:
|
|
|
|
|
|
|
|
"""写入HART变量值"""
|
|
|
|
|
|
|
|
if not self.hartRtuSlaveManager:
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
return self.hartRtuSlaveManager.writeVariable(variableName, value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def readHartVariable(self, variableName: str):
|
|
|
|
|
|
|
|
"""读取HART变量值"""
|
|
|
|
|
|
|
|
if not self.hartRtuSlaveManager:
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
return self.hartRtuSlaveManager.readVariable(variableName)
|