|
|
|
@ -470,8 +470,10 @@ class ProtocolManage(object):
|
|
|
|
if self._profibusConnected:
|
|
|
|
if self._profibusConnected:
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
|
|
|
|
print(f"正在连接Profibus...")
|
|
|
|
self.profibusManager.connect()
|
|
|
|
self.profibusManager.connect()
|
|
|
|
self._profibusConnected = True
|
|
|
|
self._profibusConnected = True
|
|
|
|
|
|
|
|
print(f"Profibus连接成功")
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
print(f"连接PROFIBUS失败: {e}")
|
|
|
|
print(f"连接PROFIBUS失败: {e}")
|
|
|
|
self._profibusConnected = False
|
|
|
|
self._profibusConnected = False
|
|
|
|
@ -485,40 +487,53 @@ class ProtocolManage(object):
|
|
|
|
if not force and (now - self._profibusLastUpdate) < 0.5:
|
|
|
|
if not force and (now - self._profibusLastUpdate) < 0.5:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
if not self._ensureProfibusConnected():
|
|
|
|
if not self._ensureProfibusConnected():
|
|
|
|
|
|
|
|
print(f"Profibus连接失败,跳过区域更新")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
with self.profibusLock:
|
|
|
|
with self.profibusLock:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
|
|
|
|
print(f"正在读取Profibus区域数据...")
|
|
|
|
self.profibusManager.readAreas()
|
|
|
|
self.profibusManager.readAreas()
|
|
|
|
self._profibusLastUpdate = now
|
|
|
|
self._profibusLastUpdate = now
|
|
|
|
|
|
|
|
print(f"Profibus区域数据读取完成")
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
print(f"读取PROFIBUS区域失败: {e}")
|
|
|
|
print(f"读取PROFIBUS区域失败: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
def _writeProfibusVariable(self, info, rawValue):
|
|
|
|
def _writeProfibusVariable(self, info, rawValue):
|
|
|
|
if not self.profibusManager:
|
|
|
|
if not self.profibusManager:
|
|
|
|
|
|
|
|
print(f"Profibus管理器未初始化")
|
|
|
|
return False, None
|
|
|
|
return False, None
|
|
|
|
if not self._ensureProfibusConnected():
|
|
|
|
if not self._ensureProfibusConnected():
|
|
|
|
|
|
|
|
print(f"Profibus连接失败")
|
|
|
|
return False, None
|
|
|
|
return False, None
|
|
|
|
device = self.profibusManager.getDevice(info['deviceName'])
|
|
|
|
device = self.profibusManager.getDevice(info['deviceName'])
|
|
|
|
if not device:
|
|
|
|
if not device:
|
|
|
|
|
|
|
|
print(f"找不到设备: {info['deviceName']}")
|
|
|
|
return False, None
|
|
|
|
return False, None
|
|
|
|
areaIndex = info['areaIndex']
|
|
|
|
areaIndex = info['areaIndex']
|
|
|
|
if areaIndex >= len(device.areas):
|
|
|
|
if areaIndex >= len(device.areas):
|
|
|
|
|
|
|
|
print(f"区域索引超出范围: {areaIndex} >= {len(device.areas)}")
|
|
|
|
return False, None
|
|
|
|
return False, None
|
|
|
|
area = device.areas[areaIndex]
|
|
|
|
area = device.areas[areaIndex]
|
|
|
|
|
|
|
|
print(f"写入设备 {info['deviceName']} 区域 {areaIndex} 类型 {area.type} 值 {rawValue}")
|
|
|
|
|
|
|
|
|
|
|
|
if area.type in ['AI', 'AO']:
|
|
|
|
if area.type in ['AI', 'AO']:
|
|
|
|
analogValue, qualityList = self._formatAnalogValue(rawValue)
|
|
|
|
analogValue, qualityList = self._formatAnalogValue(rawValue)
|
|
|
|
if analogValue is None:
|
|
|
|
if analogValue is None:
|
|
|
|
|
|
|
|
print(f"模拟量值格式化失败")
|
|
|
|
return False, None
|
|
|
|
return False, None
|
|
|
|
valuesToWrite = [analogValue]
|
|
|
|
valuesToWrite = [analogValue]
|
|
|
|
normalizedValue = analogValue
|
|
|
|
normalizedValue = analogValue
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
valuesToWrite = self._formatDiscreteValues(area, rawValue)
|
|
|
|
valuesToWrite = self._formatDiscreteValues(area, rawValue)
|
|
|
|
if valuesToWrite is None:
|
|
|
|
if valuesToWrite is None:
|
|
|
|
|
|
|
|
print(f"离散量值格式化失败")
|
|
|
|
return False, None
|
|
|
|
return False, None
|
|
|
|
qualityList = None
|
|
|
|
qualityList = None
|
|
|
|
normalizedValue = valuesToWrite[:]
|
|
|
|
normalizedValue = valuesToWrite[:]
|
|
|
|
|
|
|
|
|
|
|
|
with self.profibusLock:
|
|
|
|
with self.profibusLock:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
|
|
|
|
print(f"调用writeAreas写入数据: {valuesToWrite}")
|
|
|
|
if qualityList is not None:
|
|
|
|
if qualityList is not None:
|
|
|
|
self.profibusManager.writeAreas(
|
|
|
|
self.profibusManager.writeAreas(
|
|
|
|
deviceName=info['deviceName'],
|
|
|
|
deviceName=info['deviceName'],
|
|
|
|
@ -532,6 +547,7 @@ class ProtocolManage(object):
|
|
|
|
areaIndex=areaIndex,
|
|
|
|
areaIndex=areaIndex,
|
|
|
|
values=valuesToWrite
|
|
|
|
values=valuesToWrite
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
print(f"writeAreas调用成功,更新本地缓存")
|
|
|
|
if area.type in ['AI', 'AO']:
|
|
|
|
if area.type in ['AI', 'AO']:
|
|
|
|
area.currentValue = [normalizedValue]
|
|
|
|
area.currentValue = [normalizedValue]
|
|
|
|
if qualityList:
|
|
|
|
if qualityList:
|
|
|
|
@ -612,15 +628,20 @@ class ProtocolManage(object):
|
|
|
|
|
|
|
|
|
|
|
|
def _readProfibusVariable(self, info):
|
|
|
|
def _readProfibusVariable(self, info):
|
|
|
|
if not self.profibusManager:
|
|
|
|
if not self.profibusManager:
|
|
|
|
|
|
|
|
print(f"Profibus管理器未初始化")
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
self._updateProfibusAreas()
|
|
|
|
self._updateProfibusAreas()
|
|
|
|
device = self.profibusManager.getDevice(info['deviceName'])
|
|
|
|
device = self.profibusManager.getDevice(info['deviceName'])
|
|
|
|
if not device:
|
|
|
|
if not device:
|
|
|
|
|
|
|
|
print(f"找不到设备: {info['deviceName']}")
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
areaIndex = info['areaIndex']
|
|
|
|
areaIndex = info['areaIndex']
|
|
|
|
if areaIndex >= len(device.areas):
|
|
|
|
if areaIndex >= len(device.areas):
|
|
|
|
|
|
|
|
print(f"区域索引超出范围: {areaIndex} >= {len(device.areas)}")
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
area = device.areas[areaIndex]
|
|
|
|
area = device.areas[areaIndex]
|
|
|
|
|
|
|
|
print(f"读取设备 {info['deviceName']} 区域 {areaIndex} 类型 {area.type} 当前值: {getattr(area, 'currentValue', None)}")
|
|
|
|
|
|
|
|
|
|
|
|
if area.type in ['AI', 'AO']:
|
|
|
|
if area.type in ['AI', 'AO']:
|
|
|
|
if isinstance(area.currentValue, list) and area.currentValue:
|
|
|
|
if isinstance(area.currentValue, list) and area.currentValue:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
|