From aa29723c64e7bcc414dc5f1653665dc6c5ade1af Mon Sep 17 00:00:00 2001 From: "ADMINISTRATOR\\Administrator" <951937200@qq.com> Date: Tue, 12 Sep 2023 02:24:25 +0800 Subject: [PATCH] 0912 --- UI/AreaTabWidget.py | 27 +++++++++++------ UI/MainWindow.py | 6 ++-- model/ProjectModel/AreaManage.py | 2 +- model/ProjectModel/DeviceManage.py | 47 +++++++++++++++++------------- protocol/ModBus/ByteOrder.py | 16 +++++----- protocol/ModBus/TCPMaster.py | 3 +- 6 files changed, 60 insertions(+), 41 deletions(-) diff --git a/UI/AreaTabWidget.py b/UI/AreaTabWidget.py index 7fc44ce..e0f1f31 100644 --- a/UI/AreaTabWidget.py +++ b/UI/AreaTabWidget.py @@ -137,7 +137,7 @@ class AreaTabWidget(QMainWindow): areaLayout.addWidget(areaLabel, 0, 0) areaLayout.addWidget(areaLabel2, 0, 1) areaLayout.addItem(self.horizontalSpacer, 0, 4) - widgetLists.append([areaLabel2, areaLayout]) + widgetLists.append([areaLabel2, areaLayout, areaLabel]) elif '主站' in deviceName and dataType == 'AO' or ('从站' in deviceName and dataType == 'AI'): areaLineEdit = QLineEdit('0') @@ -148,7 +148,7 @@ class AreaTabWidget(QMainWindow): areaLayout.addWidget(editbtn, 0, 3) areaLayout.addItem(self.horizontalSpacer, 0, 4) editbtn.clicked.connect(lambda checked, btn=editbtn: self.wirteValue(btn)) - widgetLists.append([areaLabel2, areaLineEdit, editbtn, areaLayout]) + widgetLists.append([areaLabel2, areaLineEdit, editbtn, areaLayout, areaLabel]) self.widgetList.append(widgetLists) else: @@ -161,14 +161,14 @@ class AreaTabWidget(QMainWindow): areaLabel2 = QLabel('0') areaLayout.addWidget(areaLabel, i // 2, i % 2) areaLayout.addWidget(areaLabel2, i // 2, i % 2 + 1) - widgetLists.append([areaLabel2, areaLayout]) + widgetLists.append([areaLabel2, areaLayout, areaLabel]) else: areaLabel = QLabel(dataType + str(i + 1) + ": " + byteLineEdit + 'Byte') areaLabel2 = QLabel('0') areaLayout.addWidget(areaLabel, i // 2, i % 2 + 2) areaLayout.addWidget(areaLabel2, i // 2, i % 2 + 3) - widgetLists.append([areaLabel2, areaLayout]) + widgetLists.append([areaLabel2, areaLayout, areaLabel]) self.widgetList.append(widgetLists) if '主站' in deviceName and dataType == 'DO' or ( '从站' in deviceName and dataType == 'DI'): @@ -184,7 +184,7 @@ class AreaTabWidget(QMainWindow): areaLayout.addWidget(areaLineEdit, i // 2, i % 2 + 2) areaLayout.addWidget(editbtn, i // 2, i % 2 + 3) editbtn.clicked.connect(lambda checked, btn=editbtn: self.wirteValue(btn)) - widgetLists.append([areaLabel2, areaLineEdit, editbtn]) + widgetLists.append([areaLabel2, areaLineEdit, editbtn, areaLabel]) else: areaLabel = QLabel(dataType + str(i + 1) + ": " + byteLineEdit + 'Byte') areaLabel2 = QLabel('0') @@ -197,7 +197,7 @@ class AreaTabWidget(QMainWindow): areaLayout.addWidget(editbtn, i // 2, i % 2 + 7) editbtn.clicked.connect(lambda checked, btn=editbtn: self.wirteValue(btn)) - widgetLists.append([areaLabel2, areaLineEdit, editbtn]) + widgetLists.append([areaLabel2, areaLineEdit, editbtn, areaLabel]) self.widgetList.append(widgetLists) areaLayout.addItem(self.verticalSpacer, int(byteLineEdit) * 8 ,0) @@ -208,13 +208,16 @@ class AreaTabWidget(QMainWindow): del self.widgetList[index - 1] if init: + # print(222) + # self.mainwindow.devicesManange.getDevice(deviceName).addArea(type = dataType, bytes = int(byteLineEdit), nums = 1) + # self.mainwindow.devicesManange.recalculateAddress() return DevicesManange.updataAreas(dataType, order, byteLineEdit, deviceName, index) self.mainwindow.devicesManange.getDevice(deviceName).editArea(index = index - 1, type = dataType, order = order, bytes = int(byteLineEdit)) self.mainwindow.devicesManange.recalculateAddress() else: DevicesManange.addAreas(dataType, order, byteLineEdit, deviceName) - self.mainwindow.devicesManange.getDevice(deviceName).addArea(type = dataType, bytes = int(byteLineEdit), nums = 1) + self.mainwindow.devicesManange.getDevice(deviceName).addArea(type = dataType, bytes = int(byteLineEdit), order = order, nums = 1) self.mainwindow.devicesManange.recalculateAddress() @@ -229,7 +232,7 @@ class AreaTabWidget(QMainWindow): deviceName = self.areaTabWidget.parent().parent().parent().parent().parent().windowTitle() # index = self.areaTabWidget.currentIndex() valueList = [] - + didoValueList = [] if len(self.widgetList) > 0: for widgetLists in self.widgetList: for widgetList in widgetLists: @@ -237,7 +240,13 @@ class AreaTabWidget(QMainWindow): widgetList[0].setText(widgetList[1].text()) if isinstance(widgetList[1], QLineEdit): - valueList.append(float(widgetList[1].text())) + dataType = widgetList[-1].text()[0:2] + # if dataType in ['DI', "DO"]: + didoValueList.append(float(widgetList[1].text())) + + valueList.append(didoValueList) + didoValueList = [] + # print(valueList) self.mainwindow.devicesManange.writeAreas(deviceName = deviceName, values = valueList) diff --git a/UI/MainWindow.py b/UI/MainWindow.py index 7defaac..3e8973f 100644 --- a/UI/MainWindow.py +++ b/UI/MainWindow.py @@ -117,7 +117,9 @@ class MainWindow(QMainWindow): for area in areas: dataType, order = self.tran(area["type"], area["order"]) channelBytes = area["bytes"] - self.devicesManange.getDevice(deviceName = devices[0]).addArea(type = area["type"], order = order, bytes = int(channelBytes), nums = 1) + dev = self.devicesManange.getDevice(deviceName = devices[0]) + + dev.addArea(type = area["type"], order = area["order"], bytes = int(channelBytes), nums = 1) self.devicesManange.recalculateAddress() areaTabWidget.initAreaTab(dataType, order, channelBytes) @@ -169,8 +171,8 @@ class AreaQMdiSubWindow(QMdiSubWindow): self.parentWindow = parentWindow def closeEvent(self, event): self.parentWindow.subWindows.remove(self) - DeviceDB.deleteDevice(deviceName = self.windowTitle()) self.parentWindow.devicesManange.delDevice(deviceName = self.windowTitle()) + DeviceDB.deleteDevice(deviceName = self.windowTitle()) diff --git a/model/ProjectModel/AreaManage.py b/model/ProjectModel/AreaManage.py index 170a8ea..c863656 100644 --- a/model/ProjectModel/AreaManage.py +++ b/model/ProjectModel/AreaManage.py @@ -20,7 +20,7 @@ class Area(): addressList = [] order = 'ABCD' nums = None - forceValue = 0 + forceValue = [0] def __init__(self): pass diff --git a/model/ProjectModel/DeviceManage.py b/model/ProjectModel/DeviceManage.py index ef55b62..ca0b03f 100644 --- a/model/ProjectModel/DeviceManage.py +++ b/model/ProjectModel/DeviceManage.py @@ -4,13 +4,12 @@ from utils.DBModels.DeviceModels import DeviceDB from model.ProjectModel.AreaManage import Area import numpy as np from protocol.ModBus.ByteOrder import * +from protocol.ModBus.TCPMaster import * import struct #从站 "AI" "DI"可强制 #主站 "AO" "DO"可强制 class Device(): - inputAreas = [] - outputAreas = [] inputStartAddress = None inputEndAddress = None outputStartAddress = None @@ -19,7 +18,8 @@ class Device(): masterOrSlave = None deviceName = None def __init__(self): - pass + self.inputAreas = [] + self.outputAreas = [] def addArea(self, type, nums, bytes, order = 'ABCD'): area = Area() @@ -41,7 +41,9 @@ class Device(): self.outputEndAddress = area.endAddress area.addressList = np.arange(area.startAddress, area.endAddress + 1, area.bytes).tolist() self.outputAreas.append(area) - print(area.addressList, area.startAddress, self.inputEndAddress) + # print(self.inputAreas) + + # print(area.addressList, area.startAddress, self.inputEndAddress) def delArea(self, index, type): if type in ["DI", "AI"]: @@ -129,6 +131,8 @@ class DevicesManange(): self.dpSlaveDevices = collections.OrderedDict() # 有序字典 (OrderedDict) self.paMasterDevices = collections.OrderedDict() self.paSlaveDevices = collections.OrderedDict() + self.dpSlaveModbus = TcpMaster(host = '192.168.2.10', port = 502) + self.paSlaveModbus = TcpMaster(host = '192.168.4.10', port = 502) @@ -137,7 +141,6 @@ class DevicesManange(): device.type = proType device.masterOrSlave = masterSlaveModel device.deviceName = deviceName - masterSlaveModel = masterSlaveModel if proType == "DP" and masterSlaveModel == "主站": curProDict = self.dpMasterDevices @@ -152,8 +155,8 @@ class DevicesManange(): device.inputStartAddress = 0 device.outputStartAddress = 0 else: - device.inputStartAddress = curProDict.values[-1].inputEndAddress + 1 - device.outputStartAddress = curProDict.values[-1].outputEndAddress + 1 + device.inputStartAddress = list(curProDict.values())[-1].inputEndAddress + 1 + device.outputStartAddress = list(curProDict.values())[-1].outputEndAddress + 1 curProDict[deviceName] = device def initDevices(self): @@ -168,9 +171,10 @@ class DevicesManange(): def recalculateAddress(self): for devicesDict in [self.paMasterDevices, self.paSlaveDevices, self.dpMasterDevices, self.dpSlaveDevices]: for index, (deviceName, device) in enumerate(devicesDict.items()): - device.startAddress = 0 if index == 0 else previousDevice.endAddress + 1 + device.inputStartAddress = 0 if index == 0 else list(devicesDict.values())[index - 1].inputEndAddress + 1 + device.outputStartAddress = 0 if index == 0 else list(devicesDict.values())[index - 1].outputEndAddress + 1 device.recalculateAddress() - previousDevice = device + # previousDevice = device def getDevice(self, deviceName): @@ -187,30 +191,33 @@ class DevicesManange(): elif device.type == "DP" and device.masterOrSlave == "从站": curProDict = self.dpSlaveDevices areas = device.inputAreas + modbusM = self.dpSlaveModbus elif device.type == "PA" and device.masterOrSlave == "主站": areas = device.outputAreas curProDict = self.paMasterDevices elif device.type == "PA" and device.masterOrSlave == "从站": areas = device.inputAreas curProDict = self.paSlaveDevices - + modbusM = self.paSlaveModbus for area, value in zip(areas, values): area.forceValue = value - for device in curProDict.values(): forceAreas = device.outputAreas if device.masterOrSlave == "主站" else device.inputAreas for area in forceAreas: + # print(area.type) if area.type in ["AI", "AO"]: - byte = floatToBytes([area.forceValue], area.bytes, order = area.order) + byte = floatToBytes(area.forceValue, area.bytes, order = area.order) elif area.type in ["DI", "DO"]: - byte = coilsToBytes([int(x) for x in [area.forceValue]], area.bytes) - order = '<'if order.byte in ['ABCD', 'DCBA'] else '>' - else: - if len(bytes) % 2 != 0: - bytes += struct.pack('b', 0) - - values = struct.pack('') - print(len(bytes)) + if device.type == "PA" and device.masterOrSlave == "从站" and len(area.forceValue) == 16: + area.forceValue = area.forceValue[8:] + area.forceValue[:8] + byte = coilsToBytes([int(x) for x in area.forceValue], area.bytes) + bytes += byte + else: + if len(bytes) % 2 != 0: + bytes += struct.pack('B', 0) + values = struct.unpack('!' + 'H' * int(len(bytes) / 2), bytes) + modbusM.writeMultipleRegister(slaveId = 1, address = 0, outputValue = values) + # print(struct.unpack('>f', struct.pack('!HH', *values[:2]))) @classmethod def addAreas(self, type, order, bytes, deviceName): diff --git a/protocol/ModBus/ByteOrder.py b/protocol/ModBus/ByteOrder.py index 15e1d2a..a7df5ac 100644 --- a/protocol/ModBus/ByteOrder.py +++ b/protocol/ModBus/ByteOrder.py @@ -2,13 +2,13 @@ import struct def reorderBytes(byteStream, format): if format == 'ABCD': - return byteStream - elif format == 'DCBA': return byteStream[::-1] + elif format == 'DCBA': + return byteStream elif format == 'BADC': - return bytes([byteStream[1], byteStream[0], byteStream[3], byteStream[2]]) - elif format == 'CDAB': return bytes([byteStream[2], byteStream[3], byteStream[0], byteStream[1]]) + elif format == 'CDAB': + return bytes([byteStream[1], byteStream[0], byteStream[3], byteStream[2]]) else: raise ValueError("Invalid format") @@ -59,11 +59,11 @@ def floatToBytes(values, length, order): valuesNums = len(values) # 将values转换为字节流 valueByte = struct.pack(f"!{'f' * valuesNums}", *values) - valueByte = reorderBytes(valueByte, format = order) - print(ABCDToFloat(struct.unpack('!HH', valueByte))) + valueByte = reorderBytes(valueByte, format = order) + struct.pack('B' * (length - valuesNums * 4), *[0] * (length - valuesNums * 4)) # {'B' * (length - 4 * valuesNums) *[0] * (length - 4 * valuesNums) + # print(valueByte) # 返回转换后的字节流 - # return valueByte + return valueByte def coilsToBytes(values, length): decimalNumbers = [] @@ -77,6 +77,6 @@ def coilsToBytes(values, length): return valueByte # print(coilsToBytes([1] * 2, 3)) -floatToBytes([3.14], 5, 'ABCD') +# floatToBytes([3.14], 5, 'ABCD') diff --git a/protocol/ModBus/TCPMaster.py b/protocol/ModBus/TCPMaster.py index 90a000a..b5f71f7 100644 --- a/protocol/ModBus/TCPMaster.py +++ b/protocol/ModBus/TCPMaster.py @@ -20,8 +20,9 @@ class TcpMaster(): def writeMultipleRegister(self, slaveId, address, outputValue): try: - self.master.execute(slaveId, cst.WRITE_MULTIPLE_REGISTERS, starting_address = address, output_value=valueByte) + self.master.execute(slaveId, cst.WRITE_MULTIPLE_REGISTERS, starting_address = address, output_value=outputValue) except Exception as e: + print(e) return 'error'