fix modbus panic when decode faild

This commit is contained in:
lyon 2023-02-10 11:07:25 +08:00
parent 523aea3d73
commit cd0029b6d1
6 changed files with 53 additions and 4 deletions

View File

@ -12,5 +12,10 @@ host_regists = mb_tcp.deserializeReadRegisters(
print(host_regists)
mb_tcp.serializeReadInputRegisters(0, 2)
mb_tcp.deserializeReadInputRegisters(b'\x01\x04\x04\x00\x00\x08\xE6\x7D\xCE')
send_buff = mb_tcp.serializeWriteRegister(0, 0x1234)
print(send_buff)

View File

@ -48,6 +48,9 @@ Arg* _modbus__ModBus_deserializeReadRegisters(PikaObj* self, int msgLength) {
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_read_registers(ctx, msgLength,
(uint16_t*)buff);
if (len < 0) {
return NULL;
}
return arg_newBytes((uint8_t*)buff, len * 2);
}
@ -55,6 +58,9 @@ Arg* _modbus__ModBus_deserializeReadBits(PikaObj* self, int msgLength) {
uint8_t buff[128] = {0};
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_read_bits(ctx, msgLength, buff);
if (len < 0) {
return NULL;
}
return arg_newBytes(buff, len);
}
@ -62,6 +68,9 @@ Arg* _modbus__ModBus_deserializeReadInputBits(PikaObj* self, int msgLength) {
uint8_t buff[128] = {0};
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_read_input_bits(ctx, msgLength, buff);
if (len < 0) {
return NULL;
}
return arg_newBytes(buff, len);
}
@ -71,6 +80,9 @@ Arg* _modbus__ModBus_deserializeReadInputRegisters(PikaObj* self,
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_read_input_registers(ctx, msgLength,
(uint16_t*)buff);
if (len < 0) {
return NULL;
}
return arg_newBytes((uint8_t*)buff, len * 2);
}
@ -81,6 +93,9 @@ Arg* _modbus__ModBus_deserializeReportSlaveId(PikaObj* self,
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_report_slave_id(ctx, msgLength, maxDest,
(uint8_t*)buff);
if (len < 0) {
return NULL;
}
return arg_newBytes(buff, len);
}
@ -90,6 +105,9 @@ Arg* _modbus__ModBus_deserializeWriteAndReadRegisters(PikaObj* self,
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_write_and_read_registers(
ctx, msgLength, (uint16_t*)buff);
if (len < 0) {
return NULL;
}
return arg_newBytes((uint8_t*)buff, len * 2);
}

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
"--gtest_filter=vm.ui_page"
"--gtest_filter=modbus.rtu_master"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -48,6 +48,9 @@ Arg* _modbus__ModBus_deserializeReadRegisters(PikaObj* self, int msgLength) {
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_read_registers(ctx, msgLength,
(uint16_t*)buff);
if (len < 0) {
return NULL;
}
return arg_newBytes((uint8_t*)buff, len * 2);
}
@ -55,6 +58,9 @@ Arg* _modbus__ModBus_deserializeReadBits(PikaObj* self, int msgLength) {
uint8_t buff[128] = {0};
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_read_bits(ctx, msgLength, buff);
if (len < 0) {
return NULL;
}
return arg_newBytes(buff, len);
}
@ -62,6 +68,9 @@ Arg* _modbus__ModBus_deserializeReadInputBits(PikaObj* self, int msgLength) {
uint8_t buff[128] = {0};
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_read_input_bits(ctx, msgLength, buff);
if (len < 0) {
return NULL;
}
return arg_newBytes(buff, len);
}
@ -71,6 +80,9 @@ Arg* _modbus__ModBus_deserializeReadInputRegisters(PikaObj* self,
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_read_input_registers(ctx, msgLength,
(uint16_t*)buff);
if (len < 0) {
return NULL;
}
return arg_newBytes((uint8_t*)buff, len * 2);
}
@ -81,6 +93,9 @@ Arg* _modbus__ModBus_deserializeReportSlaveId(PikaObj* self,
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_report_slave_id(ctx, msgLength, maxDest,
(uint8_t*)buff);
if (len < 0) {
return NULL;
}
return arg_newBytes(buff, len);
}
@ -90,6 +105,9 @@ Arg* _modbus__ModBus_deserializeWriteAndReadRegisters(PikaObj* self,
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
int len = agile_modbus_deserialize_write_and_read_registers(
ctx, msgLength, (uint16_t*)buff);
if (len < 0) {
return NULL;
}
return arg_newBytes((uint8_t*)buff, len * 2);
}

View File

@ -344,10 +344,13 @@ TEST(modbus, rtu_master) {
pikaVM_runSingleFile(pikaMain, "test/python/modbus/rtu_master.py");
/* collect */
/* assert */
EXPECT_STREQ(log_buff[3], "BEGIN\r\n");
EXPECT_STREQ(log_buff[2],
EXPECT_STREQ(log_buff[5], "BEGIN\r\n");
EXPECT_STREQ(log_buff[4],
"b'\\x01\\x03\\x00\\x00\\x00\\x0a\\xc5\\xcd'\r\n");
EXPECT_STREQ(log_buff[1], "[0, 0, 1234, 0, 0, 123, 0, 0, 0, 0]\r\n");
EXPECT_STREQ(log_buff[3], "[0, 0, 1234, 0, 0, 123, 0, 0, 0, 0]\r\n");
EXPECT_STREQ(log_buff[2],
"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],
"b'\\x01\\x06\\x00\\x00\\x12\\x34\\x84\\xbd'\r\n");
/* deinit */

View File

@ -12,5 +12,10 @@ host_regists = mb_tcp.deserializeReadRegisters(
print(host_regists)
mb_tcp.serializeReadInputRegisters(0, 2)
mb_tcp.deserializeReadInputRegisters(b'\x01\x04\x04\x00\x00\x08\xE6\x7D\xCE')
send_buff = mb_tcp.serializeWriteRegister(0, 0x1234)
print(send_buff)