From 43f52aca9f5d8e4a586d4c9fdeac8fb82d162369 Mon Sep 17 00:00:00 2001 From: kyz Date: Thu, 12 Nov 2026 10:48:55 +0800 Subject: [PATCH] =?utf8?q?=E5=BD=95=E6=B3=A2=E6=95=B0=E6=8D=AE=E5=AF=BC?= =?utf8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- APP/mb/device_model.c | 2 +- APP/mb/mb_api.c | 28 +++++++++++++++++++++ APP/mb/mb_proto.c | 15 ++++++++++- APP/mb/mb_proto.h | 7 ++++++ APP/shell/shell.c | 25 +++++++++++++++++++ APP/user_export.c | 58 ++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 132 insertions(+), 3 deletions(-) diff --git a/APP/mb/device_model.c b/APP/mb/device_model.c index 9104301..83283b9 100644 --- a/APP/mb/device_model.c +++ b/APP/mb/device_model.c @@ -408,7 +408,7 @@ static const fld_t protection_cfg[] = { FLD(1003, F_U16, 100, cta1_x100, "CT 额定电流 1 (A)"), FLD(1004, F_U16, 0, il_ct_sel, "漏电流互感器选择"), FLD(1005, F_U16, 0, v_input_mode, "电压输入方式选择"), - FLD(1006, F_FU16, 10, pt_ratio_x10, "PT 变比"), + FLD(1006, F_U16, 10, pt_ratio_x10, "PT 变比"), FLD(1007, F_U16, 0, allow_start_time, "允许起动时间 (s)"), FLD(1008, F_U16, 0, prot_start_long, "起动时间过长保护"), FLD(1009, F_U16, 10, td_x10, "断相延时 (s)"), diff --git a/APP/mb/mb_api.c b/APP/mb/mb_api.c index 115b0ee..d600fa1 100644 --- a/APP/mb/mb_api.c +++ b/APP/mb/mb_api.c @@ -16,6 +16,34 @@ void mb_api_init(void) mb_task_start(); /* 启动物理层 + 创建 mb_task 任务 */ } +/* ======================== 读保持寄存器 (0x43 私有协议) ======================== */ +int mb_read_wave_regs(uint8_t slave, uint16_t addr, uint16_t qty, + uint16_t *buf, uint32_t timeout_ms) +{ + uint8_t req[8]; + uint8_t resp[MB_MAX_FRAME]; + uint16_t req_len, resp_len; + int rc; + + if (!buf) + return MB_ERR_PARAM; + + req_len = mb_proto_build_read_wave_regs(slave, addr, qty, req); + + rc = mb_task_request(req, req_len, resp, &resp_len, timeout_ms); + if (rc != MB_OK){ + mb_log( "---mb_read_regs( dev:%d, reg:%d, nr:%d ) --- Failed:%d!\n", slave, addr, qty, rc ); + return rc; + } + + /* 解析读响应 */ + int n = mb_proto_parse_read_response(resp, resp_len, buf, qty); + if (n < 0) + return MB_ERR_CRC; + + return n; +} + /* ======================== 读保持寄存器 (0x03) ======================== */ int mb_read_regs(uint8_t slave, uint16_t addr, uint16_t qty, uint16_t *buf, uint32_t timeout_ms) diff --git a/APP/mb/mb_proto.c b/APP/mb/mb_proto.c index 205fabe..4c1011f 100644 --- a/APP/mb/mb_proto.c +++ b/APP/mb/mb_proto.c @@ -68,6 +68,19 @@ uint16_t mb_proto_add_crc(uint8_t *frame, uint16_t len) } /* ======================== 构建请求帧 ======================== */ +uint16_t mb_proto_build_read_wave_regs(uint8_t slave_addr, + uint16_t start_addr, + uint16_t quantity, + uint8_t *buf) +{ + buf[0] = MB_CMD_ADDR_MAKE(slave_addr); + buf[1] = MB_FUNC_READ_HOLDING_REGS | 0x40; // 私有协议 0x43 + buf[2] = (start_addr >> 8) & 0xFF; + buf[3] = start_addr & 0xFF; + buf[4] = (quantity >> 8) & 0xFF; + buf[5] = quantity & 0xFF; + return mb_proto_add_crc(buf, 6); +} uint16_t mb_proto_build_read_holding_regs(uint8_t slave_addr, uint16_t start_addr, @@ -177,7 +190,7 @@ int mb_proto_parse_read_response(const uint8_t *frame, uint16_t len, return -2; } - if (func != MB_FUNC_READ_HOLDING_REGS && func != MB_FUNC_READ_COILS) { + if (func != MB_FUNC_READ_HOLDING_REGS && func != MB_FUNC_READ_COILS && func != MB_FUNC_READ_WAVE_REGS ) { mb_log("[MB] unexpected func=0x%02X\n", func); return -1; } diff --git a/APP/mb/mb_proto.h b/APP/mb/mb_proto.h index 8b1aa99..24cd7c0 100644 --- a/APP/mb/mb_proto.h +++ b/APP/mb/mb_proto.h @@ -59,6 +59,8 @@ extern "C" { #define MB_FUNC_WRITE_SINGLE_REG 0x06 #define MB_FUNC_WRITE_MULTIPLE_REGS 0x10 +#define MB_FUNC_READ_WAVE_REGS 0x43 + /* ======================== API ======================== */ uint16_t mb_proto_crc16(const uint8_t *data, uint16_t len); @@ -68,6 +70,11 @@ bool mb_proto_check_crc(const uint8_t *frame, uint16_t len); uint16_t mb_proto_add_crc(uint8_t *frame, uint16_t len); /* ---- 构建请求帧 ---- */ +uint16_t mb_proto_build_read_wave_regs(uint8_t slave_addr, + uint16_t start_addr, + uint16_t quantity, + uint8_t *buf); + uint16_t mb_proto_build_read_holding_regs(uint8_t slave_addr, uint16_t start_addr, uint16_t quantity, diff --git a/APP/shell/shell.c b/APP/shell/shell.c index 5f5f1f8..a90496b 100644 --- a/APP/shell/shell.c +++ b/APP/shell/shell.c @@ -501,7 +501,32 @@ static int cmd_soe_export_start(int argc, char **argv) CSH_SCMD_EXPORT_ALIAS_FULL(cmd_soe_export_start, soe_export, "soe export start", "soe_export_start\r\n - mount USB, export soe to file\r\n"); +//----------------------------------wave_export +extern void user_wave_export_request( void ); +extern int32_t user_wave_export_get_result( void ); +static int cmd_wave_export_start(int argc, char **argv) +{ + chry_shell_t *csh = (void *)argv[argc + 1]; + int rc; + + (void)argc; + (void)argv; + + csh_printf(csh, "wave export start ...\r\n"); + user_wave_export_request(); + + while( user_wave_export_get_result() == 0 ){ + vTaskDelay(100); + } + printf( "\n\n********* export wave result: %s *********\n\n", user_wave_export_get_result()==1 ? "success" : "failed" ); + + return 0; +} +CSH_SCMD_EXPORT_ALIAS_FULL(cmd_wave_export_start, wave_export, "wave export start", + "wave_export_start\r\n - mount USB, export wave to file\r\n"); + +//---------------------------------------------- extern void user_param_export_request( void ); extern int32_t user_param_export_get_result( void ); diff --git a/APP/user_export.c b/APP/user_export.c index 3c93926..7da182b 100644 --- a/APP/user_export.c +++ b/APP/user_export.c @@ -253,6 +253,10 @@ static int32_t s_param_export_result = 0; // 0:wait, -1:failed, 1:success static int32_t s_soe_export_request = 0; // 0:未请求, 1:已请求 static int32_t s_soe_export_result = 0; // 0:wait, -1:failed, 1:success +//录波导出 +static int32_t s_wave_export_request = 0; // 0:未请求, 1:已请求 +static int32_t s_wave_export_result = 0; // 0:wait, -1:failed, 1:success + void user_param_export_request( void ) { s_param_export_request = 1; @@ -286,6 +290,17 @@ int32_t user_soe_export_get_result( void ) return s_soe_export_result; } +void user_wave_export_request( void ) +{ + s_wave_export_request = 1; + s_wave_export_result = 0; +} + +int32_t user_wave_export_get_result( void ) +{ + return s_wave_export_result; +} + /* * 生成文件名:设备位号(16字符,保留空格) + 可选时间后缀 _YYYYMMDD_HHMMSS @@ -971,9 +986,50 @@ void user_soe_export( void ) } +//录波导出 +//读取民工0x43,地址0到1999, 每个指令读取100个寄存器数据为一个数据包。一共读取2000个数据包 + +void user_wave_export( void ) +{ + char name[48]; + char path[64]; + char buf[256]; + int32_t ret=0; + + //函数在mb task中调用,检查到用户请求导出定值,才调用 + if( s_wave_export_request == 0 ) + return; + + s_wave_export_request = 0; + s_wave_export_result = 0; + +#if 1 + //0x43 读取测试 + for( int i=0; i<2000; i++ ){ + while( mb_read_wave_regs( USER_MB_TARGET_SLAVE, i, 100, buf, 500 ) != 100 ){ + printf( "read_wave_regs failed, err:%d\n", ret ); + vTaskDelay(100); + } + printf( "read_wave_regs success, addr:%d, qty:%d\n", i, 100 ); + } +#else + //设备位号(16字符) + 时间后缀 生成文件名 + user_param_make_name( name, sizeof(name), 1 ); + + //导出csv:usb:/BD_SET/设备位号_时间.csv + sprintf( path, "%s.csv", name ); + ret = user_soe_write_csv( path ); + printf( "export %s .... %s\n", path, ret == 0 ? "done" : "failed" ); +#endif + + s_wave_export_result = (ret == 0) ? 1 : -1; +} + + void user_export_request_handle( void ) { user_param_export(); user_param_import(); user_soe_export(); -} + user_wave_export(); +} \ No newline at end of file -- 2.34.1