mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
fix modbus panic when decode faild
This commit is contained in:
parent
523aea3d73
commit
cd0029b6d1
@ -12,5 +12,10 @@ host_regists = mb_tcp.deserializeReadRegisters(
|
|||||||
print(host_regists)
|
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)
|
send_buff = mb_tcp.serializeWriteRegister(0, 0x1234)
|
||||||
print(send_buff)
|
print(send_buff)
|
||||||
|
@ -48,6 +48,9 @@ Arg* _modbus__ModBus_deserializeReadRegisters(PikaObj* self, int msgLength) {
|
|||||||
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_read_registers(ctx, msgLength,
|
int len = agile_modbus_deserialize_read_registers(ctx, msgLength,
|
||||||
(uint16_t*)buff);
|
(uint16_t*)buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes((uint8_t*)buff, len * 2);
|
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};
|
uint8_t buff[128] = {0};
|
||||||
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_read_bits(ctx, msgLength, buff);
|
int len = agile_modbus_deserialize_read_bits(ctx, msgLength, buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes(buff, len);
|
return arg_newBytes(buff, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +68,9 @@ Arg* _modbus__ModBus_deserializeReadInputBits(PikaObj* self, int msgLength) {
|
|||||||
uint8_t buff[128] = {0};
|
uint8_t buff[128] = {0};
|
||||||
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_read_input_bits(ctx, msgLength, buff);
|
int len = agile_modbus_deserialize_read_input_bits(ctx, msgLength, buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes(buff, len);
|
return arg_newBytes(buff, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,6 +80,9 @@ Arg* _modbus__ModBus_deserializeReadInputRegisters(PikaObj* self,
|
|||||||
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_read_input_registers(ctx, msgLength,
|
int len = agile_modbus_deserialize_read_input_registers(ctx, msgLength,
|
||||||
(uint16_t*)buff);
|
(uint16_t*)buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes((uint8_t*)buff, len * 2);
|
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");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_report_slave_id(ctx, msgLength, maxDest,
|
int len = agile_modbus_deserialize_report_slave_id(ctx, msgLength, maxDest,
|
||||||
(uint8_t*)buff);
|
(uint8_t*)buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes(buff, len);
|
return arg_newBytes(buff, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,6 +105,9 @@ Arg* _modbus__ModBus_deserializeWriteAndReadRegisters(PikaObj* self,
|
|||||||
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_write_and_read_registers(
|
int len = agile_modbus_deserialize_write_and_read_registers(
|
||||||
ctx, msgLength, (uint16_t*)buff);
|
ctx, msgLength, (uint16_t*)buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes((uint8_t*)buff, len * 2);
|
return arg_newBytes((uint8_t*)buff, len * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
port/linux/.vscode/launch.json
vendored
2
port/linux/.vscode/launch.json
vendored
@ -11,7 +11,7 @@
|
|||||||
"program": "${workspaceFolder}/build/test/pikascript_test",
|
"program": "${workspaceFolder}/build/test/pikascript_test",
|
||||||
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
||||||
"args": [
|
"args": [
|
||||||
"--gtest_filter=vm.ui_page"
|
"--gtest_filter=modbus.rtu_master"
|
||||||
],
|
],
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
|
@ -48,6 +48,9 @@ Arg* _modbus__ModBus_deserializeReadRegisters(PikaObj* self, int msgLength) {
|
|||||||
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_read_registers(ctx, msgLength,
|
int len = agile_modbus_deserialize_read_registers(ctx, msgLength,
|
||||||
(uint16_t*)buff);
|
(uint16_t*)buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes((uint8_t*)buff, len * 2);
|
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};
|
uint8_t buff[128] = {0};
|
||||||
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_read_bits(ctx, msgLength, buff);
|
int len = agile_modbus_deserialize_read_bits(ctx, msgLength, buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes(buff, len);
|
return arg_newBytes(buff, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +68,9 @@ Arg* _modbus__ModBus_deserializeReadInputBits(PikaObj* self, int msgLength) {
|
|||||||
uint8_t buff[128] = {0};
|
uint8_t buff[128] = {0};
|
||||||
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_read_input_bits(ctx, msgLength, buff);
|
int len = agile_modbus_deserialize_read_input_bits(ctx, msgLength, buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes(buff, len);
|
return arg_newBytes(buff, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,6 +80,9 @@ Arg* _modbus__ModBus_deserializeReadInputRegisters(PikaObj* self,
|
|||||||
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_read_input_registers(ctx, msgLength,
|
int len = agile_modbus_deserialize_read_input_registers(ctx, msgLength,
|
||||||
(uint16_t*)buff);
|
(uint16_t*)buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes((uint8_t*)buff, len * 2);
|
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");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_report_slave_id(ctx, msgLength, maxDest,
|
int len = agile_modbus_deserialize_report_slave_id(ctx, msgLength, maxDest,
|
||||||
(uint8_t*)buff);
|
(uint8_t*)buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes(buff, len);
|
return arg_newBytes(buff, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,6 +105,9 @@ Arg* _modbus__ModBus_deserializeWriteAndReadRegisters(PikaObj* self,
|
|||||||
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
|
||||||
int len = agile_modbus_deserialize_write_and_read_registers(
|
int len = agile_modbus_deserialize_write_and_read_registers(
|
||||||
ctx, msgLength, (uint16_t*)buff);
|
ctx, msgLength, (uint16_t*)buff);
|
||||||
|
if (len < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return arg_newBytes((uint8_t*)buff, len * 2);
|
return arg_newBytes((uint8_t*)buff, len * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -344,10 +344,13 @@ TEST(modbus, rtu_master) {
|
|||||||
pikaVM_runSingleFile(pikaMain, "test/python/modbus/rtu_master.py");
|
pikaVM_runSingleFile(pikaMain, "test/python/modbus/rtu_master.py");
|
||||||
/* collect */
|
/* collect */
|
||||||
/* assert */
|
/* assert */
|
||||||
EXPECT_STREQ(log_buff[3], "BEGIN\r\n");
|
EXPECT_STREQ(log_buff[5], "BEGIN\r\n");
|
||||||
EXPECT_STREQ(log_buff[2],
|
EXPECT_STREQ(log_buff[4],
|
||||||
"b'\\x01\\x03\\x00\\x00\\x00\\x0a\\xc5\\xcd'\r\n");
|
"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],
|
EXPECT_STREQ(log_buff[0],
|
||||||
"b'\\x01\\x06\\x00\\x00\\x12\\x34\\x84\\xbd'\r\n");
|
"b'\\x01\\x06\\x00\\x00\\x12\\x34\\x84\\xbd'\r\n");
|
||||||
/* deinit */
|
/* deinit */
|
||||||
|
@ -12,5 +12,10 @@ host_regists = mb_tcp.deserializeReadRegisters(
|
|||||||
print(host_regists)
|
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)
|
send_buff = mb_tcp.serializeWriteRegister(0, 0x1234)
|
||||||
print(send_buff)
|
print(send_buff)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user