|
|
|
@ -7,6 +7,25 @@ import struct
|
|
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
# from protocol.ModBus.ByteOrder import *
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
class SearchSlaveThread(threading.Thread):
|
|
|
|
|
def __init__(self, callback, master):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.stopEvent = threading.Event()
|
|
|
|
|
self.callback = callback
|
|
|
|
|
self.master = master
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
for address in range(1, 126):
|
|
|
|
|
if not self.stopEvent.is_set():
|
|
|
|
|
self.callback(address, self.master.judgeSlave(address))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
|
self.stopEvent.set()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DPV1Master():
|
|
|
|
@ -43,31 +62,12 @@ class DPV1Master():
|
|
|
|
|
return all(x == 0 for x in tup)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def searchMaster(self, callback = None):
|
|
|
|
|
def search():
|
|
|
|
|
for address in range(1, 126):
|
|
|
|
|
hexAddress = address.to_bytes(1, byteorder='little')
|
|
|
|
|
searchByteStream = b'\x01' + hexAddress + b'\x01\x00\xF0\x00'
|
|
|
|
|
searchDate = struct.unpack('>hhh', searchByteStream)
|
|
|
|
|
self.writeMultipleRegister(1, 750, searchDate)
|
|
|
|
|
time.sleep(0.3)
|
|
|
|
|
dir = self.readHoldingRegisters(1, 750, 6)
|
|
|
|
|
if not self.areAllZeros(dir) and dir != 'error':
|
|
|
|
|
callback(address, True)
|
|
|
|
|
else:
|
|
|
|
|
callback(address, False)
|
|
|
|
|
self.writeMultipleRegister(1, 750, self.resetData)
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
# print(address, dir)
|
|
|
|
|
# if callable(callback):
|
|
|
|
|
if not self.master:
|
|
|
|
|
callback(125, False)
|
|
|
|
|
return
|
|
|
|
|
self.searchMasterThread = threading.Thread(target=search)
|
|
|
|
|
self.searchMasterThread.start()
|
|
|
|
|
def searchSlave(self, callback = None):
|
|
|
|
|
self.searchSlaveThread = SearchSlaveThread(callback = callback, master = self)
|
|
|
|
|
self.searchSlaveThread.start()
|
|
|
|
|
|
|
|
|
|
def closeThread(self):
|
|
|
|
|
self.searchMasterThread.join()
|
|
|
|
|
self.searchSlaveThread.stop()
|
|
|
|
|
|
|
|
|
|
def readParm(self, address, slot, index, length, callback = None):
|
|
|
|
|
hexAddress = address.to_bytes(1, byteorder='little')
|
|
|
|
@ -85,6 +85,19 @@ class DPV1Master():
|
|
|
|
|
return
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def judgeSlave(self, address):
|
|
|
|
|
hexAddress = address.to_bytes(1, byteorder='little')
|
|
|
|
|
searchByteStream = b'\x01' + hexAddress + b'\x01\x00\xF0\x00'
|
|
|
|
|
searchDate = struct.unpack('>hhh', searchByteStream)
|
|
|
|
|
self.writeMultipleRegister(1, 750, searchDate)
|
|
|
|
|
time.sleep(0.3)
|
|
|
|
|
dir = self.readHoldingRegisters(1, 750, 6)
|
|
|
|
|
self.writeMultipleRegister(1, 750, self.resetData)
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
if not self.areAllZeros(dir) and dir != 'error':
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def readInputRegisters(self, slaveId, startAddress, varNums, order = 'ABCD'):
|
|
|
|
|