fix modbus err

This commit is contained in:
lyon 2023-02-23 15:52:44 +08:00
parent b360b6d821
commit 2077880671
5 changed files with 30 additions and 22 deletions

View File

@ -1,21 +1,24 @@
import modbus
mb_tcp = modbus.ModBusRTU(128, 128)
mb_tcp.setSlave(1)
mb = modbus.ModBusRTU(128, 128)
mb.setSlave(1)
send_buff = mb_tcp.serializeReadRegisters(0, 10)
send_buff = mb.serializeReadRegisters(0, 10)
print(send_buff)
host_regists = mb_tcp.deserializeReadRegisters(
host_regists = mb.deserializeReadRegisters(
b'\x01\x03\x14\x00\x00\x00\x00\x04\xD2\x00\x00\x00\x00\x00\x7B\x00\x00\x00\x00\x00\x00\x00\x00\xE5\x0B'
)
print(host_regists)
mb_tcp.serializeReadInputRegisters(0, 2)
mb.serializeReadInputRegisters(0, 2)
mb_tcp.deserializeReadInputRegisters(b'\x01\x04\x04\x00\x00\x08\xE6\x7D\xCE')
mb.deserializeReadInputRegisters(b'\x01\x04\x04\x00\x00\x08\xE6\x7D\xCE')
send_buff = mb_tcp.serializeWriteRegister(0, 0x1234)
send_buff = mb.serializeWriteRegister(0, 0x1234)
print(send_buff)
send_buff = mb.serializeWriteBits(0, [1, 1, 1, 0, 1, 0, 1, 0])
print(send_buff)

View File

@ -4,7 +4,7 @@ import _modbus
class ModBus(_modbus._ModBus):
def serializeWriteBits(self, addr: int, src: list) -> bytes:
lenth = super().serializeWriteBits(addr, len(list), bytes(src))
lenth = super().serializeWriteBits(addr, len(src), bytes(src))
return self.sendBuff[0:lenth]
def serializeWriteRegisters(self, addr: int, src: list) -> bytes:

View File

@ -4,7 +4,7 @@ import _modbus
class ModBus(_modbus._ModBus):
def serializeWriteBits(self, addr: int, src: list) -> bytes:
lenth = super().serializeWriteBits(addr, len(list), bytes(src))
lenth = super().serializeWriteBits(addr, len(src), bytes(src))
return self.sendBuff[0:lenth]
def serializeWriteRegisters(self, addr: int, src: list) -> bytes:

View File

@ -396,15 +396,17 @@ TEST(modbus, rtu_master) {
pikaVM_runSingleFile(pikaMain, "test/python/modbus/rtu_master.py");
/* collect */
/* assert */
EXPECT_STREQ(log_buff[5], "BEGIN\r\n");
EXPECT_STREQ(log_buff[4],
EXPECT_STREQ(log_buff[6], "BEGIN\r\n");
EXPECT_STREQ(log_buff[5],
"b'\\x01\\x03\\x00\\x00\\x00\\x0a\\xc5\\xcd'\r\n");
EXPECT_STREQ(log_buff[3], "[0, 0, 1234, 0, 0, 123, 0, 0, 0, 0]\r\n");
EXPECT_STREQ(log_buff[2],
EXPECT_STREQ(log_buff[4], "[0, 0, 1234, 0, 0, 123, 0, 0, 0, 0]\r\n");
EXPECT_STREQ(log_buff[3],
"b'\\x01\\x04\\x00\\x00\\x00\\x02\\x71\\xcb'\r\n");
EXPECT_STREQ(log_buff[1], "[0, 2278]\r\n");
EXPECT_STREQ(log_buff[0],
EXPECT_STREQ(log_buff[2], "[0, 2278]\r\n");
EXPECT_STREQ(log_buff[1],
"b'\\x01\\x06\\x00\\x00\\x12\\x34\\x84\\xbd'\r\n");
EXPECT_STREQ(log_buff[0],
"b'\\x01\\x0f\\x00\\x00\\x00\\x08\\x01\\x57\\xbf\\x6b'\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);

View File

@ -1,21 +1,24 @@
import modbus
mb_tcp = modbus.ModBusRTU(128, 128)
mb_tcp.setSlave(1)
mb = modbus.ModBusRTU(128, 128)
mb.setSlave(1)
send_buff = mb_tcp.serializeReadRegisters(0, 10)
send_buff = mb.serializeReadRegisters(0, 10)
print(send_buff)
host_regists = mb_tcp.deserializeReadRegisters(
host_regists = mb.deserializeReadRegisters(
b'\x01\x03\x14\x00\x00\x00\x00\x04\xD2\x00\x00\x00\x00\x00\x7B\x00\x00\x00\x00\x00\x00\x00\x00\xE5\x0B'
)
print(host_regists)
mb_tcp.serializeReadInputRegisters(0, 2)
mb.serializeReadInputRegisters(0, 2)
mb_tcp.deserializeReadInputRegisters(b'\x01\x04\x04\x00\x00\x08\xE6\x7D\xCE')
mb.deserializeReadInputRegisters(b'\x01\x04\x04\x00\x00\x08\xE6\x7D\xCE')
send_buff = mb_tcp.serializeWriteRegister(0, 0x1234)
send_buff = mb.serializeWriteRegister(0, 0x1234)
print(send_buff)
send_buff = mb.serializeWriteBits(0, [1, 1, 1, 0, 1, 0, 1, 0])
print(send_buff)