100 lines
3.5 KiB
Python
Raw Normal View History

import _modbus
2022-09-19 00:48:04 +08:00
class ModBus(_modbus._ModBus):
2022-09-19 14:35:05 +08:00
def serializeWriteBits(self, addr: int, src: list) -> bytes:
2023-02-23 15:52:44 +08:00
lenth = super().serializeWriteBits(addr, len(src), bytes(src))
2022-09-19 13:27:20 +08:00
return self.sendBuff[0:lenth]
2022-09-19 14:35:05 +08:00
def serializeWriteRegisters(self, addr: int, src: list) -> bytes:
2022-09-19 13:27:20 +08:00
_src = bytes(2 * len(src))
for i in range(len(src)):
_src[2 * i] = src[i] % 256
_src[2 * i + 1] = src[i] // 256
2022-09-19 14:35:05 +08:00
lenth = super().serializeWriteRegisters(addr, len(src), _src)
2022-09-19 13:27:20 +08:00
return self.sendBuff[0:lenth]
def serializeReadBits(self, addr: int, nb: int) -> bytes:
lenth = super().serializeReadBits(addr, nb)
return self.sendBuff[0:lenth]
def serializeReadInputBits(self, addr: int, nb: int) -> bytes:
lenth = super().serializeReadInputBits(addr, nb)
return self.sendBuff[0:lenth]
def serializeReadRegisters(self, addr: int, nb: int) -> bytes:
lenth = super().serializeReadRegisters(addr, nb)
return self.sendBuff[0:lenth]
def serializeReadInputRegisters(self, addr: int, nb: int) -> bytes:
lenth = super().serializeReadInputRegisters(addr, nb)
return self.sendBuff[0:lenth]
def serializeWriteBit(self, addr: int, status: int) -> bytes:
lenth = super().serializeWriteBit(addr, status)
return self.sendBuff[0:lenth]
def serializeWriteRegister(self, addr: int, value: int) -> bytes:
lenth = super().serializeWriteRegister(addr, value)
return self.sendBuff[0:lenth]
2022-09-19 00:48:04 +08:00
2022-09-19 13:28:44 +08:00
def serializeMaskWriteRegister(self,
addr: int,
andMask: int,
orMask: int) -> bytes:
2022-09-19 13:27:20 +08:00
lenth = super().serializeMaskWriteRegister(addr, andMask, orMask)
return self.sendBuff[0:lenth]
def serializeReportSlaveId(self) -> int:
lenth = super().serializeReportSlaveId()
return self.sendBuff[0:lenth]
def deserializeReadRegisters(self, msg: bytes) -> list:
self.readBuff = msg
2022-09-19 14:32:01 +08:00
dest = super().deserializeReadRegisters(len(msg))
2022-09-19 00:48:04 +08:00
ret = []
for i in range(0, len(dest), 2):
2022-09-19 13:27:20 +08:00
ret.append(int(dest[i]) + int(dest[i + 1]) * 256)
2022-09-19 00:48:04 +08:00
return ret
2022-09-19 13:27:20 +08:00
def deserializeReadBits(self, msg: bytes) -> list:
self.readBuff = msg
length = len(msg)
dest = super().deserializeReadBits(length)
return list(dest)
def deserializeReadInputBits(self, msg: bytes) -> list:
self.readBuff = msg
length = len(msg)
dest = super().deserializeReadInputBits(length)
return list(dest)
def deserializeReadInputRegisters(self, msg: bytes) -> list:
self.readBuff = msg
length = len(msg)
dest = super().deserializeReadInputRegisters(length)
2022-09-19 00:48:04 +08:00
ret = []
for i in range(0, len(dest), 2):
2022-09-19 13:27:20 +08:00
ret.append(int(dest[i]) + int(dest[i + 1]) * 256)
2022-09-19 00:48:04 +08:00
return ret
2022-09-19 13:27:20 +08:00
def deserializeWriteAndReadRegisters(self, msg: bytes) -> list:
self.readBuff = msg
length = len(msg)
dest = super().deserializeWriteAndReadRegisters(length)
2022-09-19 00:48:04 +08:00
ret = []
for i in range(0, len(dest), 2):
2022-09-19 13:27:20 +08:00
ret.append(int(dest[i]) + int(dest[i + 1]) * 256)
2022-09-19 00:48:04 +08:00
return ret
class ModBusRTU(ModBus):
def __init__(self, sendBuffSize: int, readBuffSize: int):
self.__init__rtu(sendBuffSize, readBuffSize)
class ModBusTCP(ModBus):
def __init__(self, sendBuffSize: int, readBuffSize: int):
self.__init__tcp(sendBuffSize, readBuffSize)