diff --git a/package/modbus/_modbus.c b/package/modbus/_modbus.c index 4694e03de..d23dc6a5a 100644 --- a/package/modbus/_modbus.c +++ b/package/modbus/_modbus.c @@ -1,9 +1,7 @@ #include "_modbus__ModBus.h" -#include "_modbus__ModBusRTU.h" -#include "_modbus__ModBusTCP.h" #include "agile_modbus.h" -void _modbus__ModBusRTU___init__(PikaObj* self, +void _modbus__ModBus___init__rtu(PikaObj* self, int sendBUffSize, int readBuffSize) { agile_modbus_rtu_t ctx_rtu = {0}; @@ -22,7 +20,7 @@ void _modbus__ModBus_setSlave(PikaObj* self, int slave) { agile_modbus_set_slave(ctx, slave); } -void _modbus__ModBusTCP___init__(PikaObj* self, +void _modbus__ModBus___init__tcp(PikaObj* self, int sendBuffSize, int readBuffSize) { agile_modbus_tcp_t ctx_tcp = {0}; @@ -181,3 +179,13 @@ int _modbus__ModBus_serializeWriteRegisters(PikaObj* self, return agile_modbus_serialize_write_registers(ctx, addr, nb, (uint16_t*)src); } + +Arg* _modbus__ModBus_getSendBuff(PikaObj* self) { + agile_modbus_t* ctx = obj_getPtr(self, "ctx"); + return arg_newBytes(ctx->send_buf, ctx->send_bufsz); +} + +Arg* _modbus__ModBus_getReadBuff(PikaObj* self) { + agile_modbus_t* ctx = obj_getPtr(self, "ctx"); + return arg_newBytes(ctx->read_buf, ctx->read_bufsz); +} diff --git a/package/modbus/_modbus.pyi b/package/modbus/_modbus.pyi index 5334fd719..d044199b7 100644 --- a/package/modbus/_modbus.pyi +++ b/package/modbus/_modbus.pyi @@ -1,31 +1,32 @@ class _ModBus: def setSlave(self, slave: int): ... def serializeReadBits(self, addr: int, nb: int) -> int: ... - def deserializeReadBits(self, msgLength: int, dest: bytes) -> int: ... def serializeReadInputBits(self, addr: int, nb: int) -> int: ... - def deserializeReadInputBits(self, msgLength: int, dest: bytes) -> int: ... def serializeReadRegisters(self, addr: int, nb: int) -> int: ... - def deserializeReadRegisters(self, msgLength: int, dest: bytes) -> int: ... def serializeReadInputRegisters(self, addr: int, nb: int) -> int: ... - def deserializeReadInputRegisters(self, msgLength: int, dest: bytes) -> int: ... + def serializeWriteBit(self, addr: int, status: int) -> int: ... def deserializeWriteBit(self, msgLength: int) -> int: ... def serializeWriteRegister(self, addr: int, value: int) -> int: ... def deserializeWriteRegister(self, msgLength: int) -> int: ... - def serializeWriteBits(self, addr: int, nb: int, src: bytes) -> int: ... def deserializeWriteBits(self, msgLength: int) -> int: ... - def serializeWriteRegisters(self, addr: int, nb: int, src: bytes) -> int: ... def deserializeWriteRegisters(self, msgLength: int) -> int: ... def serializeMaskWriteRegister(self, addr: int, andMask: int, orMask: int) -> int: ... def deserializeMaskWriteRegister(self, msgLength: int) -> int: ... def serializeWriteAndReadRegisters(self, writeAddr: int, writeNb: int, src: bytes, readAddr: int, readNb: int) -> int: ... - def deserializeWriteAndReadRegisters(self, msgLength: int, dest: bytes) -> int: ... def serializeReportSlaveId(self) -> int: ... + + def deserializeReadBits(self, msgLength: int, dest: bytes) -> int: ... + def deserializeReadInputBits(self, msgLength: int, dest: bytes) -> int: ... + def deserializeReadRegisters(self, msgLength: int, dest: bytes) -> int: ... + def deserializeReadInputRegisters(self, msgLength: int, dest: bytes) -> int: ... + def serializeWriteBits(self, addr: int, nb: int, src: bytes) -> int: ... + def serializeWriteRegisters(self, addr: int, nb: int, src: bytes) -> int: ... + def deserializeWriteAndReadRegisters(self, msgLength: int, dest: bytes) -> int: ... + def deserializeReportSlaveId(self, msgLength: int, maxDest: int, dest: bytes) -> int: ... - -class _ModBusRTU(_ModBus): - def __init__(self, sendBuffSize: int, readBuffSize: int): ... - - -class _ModBusTCP(_ModBus): - def __init__(self, sendBuffSize: int, readBuffSize: int): ... + + def getSendBuff(self) -> bytes: ... + def getReadBuff(self) -> bytes: ... + def __init__rtu(self, sendBuffSize: int, readBuffSize: int): ... + def __init__tcp(self, sendBuffSize: int, readBuffSize: int): ... diff --git a/package/modbus/modbus.py b/package/modbus/modbus.py index 7a58f56ab..3cc536636 100644 --- a/package/modbus/modbus.py +++ b/package/modbus/modbus.py @@ -1,5 +1,58 @@ import _modbus -class ModBus(_modbus._Modbus): - ... +class ModBus(_modbus._ModBus): + def deserializeReadBits(self, msgLength: int): + dest = bytes(msgLength) + super().deserializeReadBits(msgLength, dest) + return list(dest) + + def deserializeReadInputBits(self, msgLength: int): + dest = bytes(msgLength) + super().deserializeReadInputBits(msgLength, dest) + return list(dest) + + def deserializeReadRegisters(self, msgLength: int) -> list: + dest = bytes(2 * msgLength) + super().deserializeReadRegisters(msgLength, dest) + ret = [] + for i in range(0, len(dest), 2): + ret.append(dest[i] + dest[i + 1] * 256) + return ret + + def deserializeReadInputRegisters(self, msgLength: int) -> list: + dest = bytes(2 * msgLength) + super().deserializeReadInputRegisters(msgLength, dest) + ret = [] + for i in range(0, len(dest), 2): + ret.append(dest[i] + dest[i + 1] * 256) + return ret + + def serializeWriteBits(self, addr: int, nb: int, src: list): + src = bytes(src) + return super().serializeWriteBits(addr, nb, src) + + def serializeWriteRegisters(self, addr: int, nb: int, src: list): + _src = bytes(2 * len(src)) + for i in range(len(src)): + _src[2 * i] = src[i] % 256 + _src[2 * i + 1] = src[i] // 256 + return super().serializeWriteRegisters(addr, nb, _src) + + def deserializeWriteAndReadRegisters(self, msgLength: int) -> list: + dest = bytes(2 * msgLength) + super().deserializeWriteAndReadRegisters(msgLength, dest) + ret = [] + for i in range(0, len(dest), 2): + ret.append(dest[i] + dest[i + 1] * 256) + 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) diff --git a/port/linux/package/pikascript/modbus.py b/port/linux/package/pikascript/modbus.py index 0059ce583..3cc536636 100644 --- a/port/linux/package/pikascript/modbus.py +++ b/port/linux/package/pikascript/modbus.py @@ -1,6 +1,6 @@ import _modbus -""" + class ModBus(_modbus._ModBus): def deserializeReadBits(self, msgLength: int): dest = bytes(msgLength) @@ -12,7 +12,7 @@ class ModBus(_modbus._ModBus): super().deserializeReadInputBits(msgLength, dest) return list(dest) - def deserializeReadRegisters(self, msgLength: int): + def deserializeReadRegisters(self, msgLength: int) -> list: dest = bytes(2 * msgLength) super().deserializeReadRegisters(msgLength, dest) ret = [] @@ -56,4 +56,3 @@ class ModBusRTU(ModBus): class ModBusTCP(ModBus): def __init__(self, sendBuffSize: int, readBuffSize: int): self.__init__tcp(sendBuffSize, readBuffSize) -"""