From ce914bb02f154773b72edab7173ec3c65eccdb23 Mon Sep 17 00:00:00 2001 From: kyz Date: Fri, 14 Nov 2025 13:47:02 +0800 Subject: [PATCH] reset for tx fail --- src/user_can.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/user_can.c b/src/user_can.c index e0f531a..1ab726e 100644 --- a/src/user_can.c +++ b/src/user_can.c @@ -88,16 +88,19 @@ int can_bus_reset(int bus_id) { // 关闭CAN接口 char command[256]; snprintf(command, sizeof(command), "ip link set down %s", ifname); +printf( "can bus down cmd : %s\n", command ); if (execute_system_command(command) < 0) { return -1; } - + usleep(200); // 重新启用CAN接口 snprintf(command, sizeof(command), "ip link set up %s", ifname); +printf( "can bus up cmd : %s\n", command ); if (execute_system_command(command) < 0) { return -1; } + usleep(200); printf("CAN总线 %d (%s) 重置成功\n", bus_id, ifname); return 0; @@ -255,9 +258,19 @@ printf( "----connected -> 0 \n" ); // 发送数据 ssize_t nbytes = write(device->socket_fd, &canfd_frame, sizeof(canfd_frame)); if (nbytes < 0) { + static int err_n = 0; fprintf(stderr, "发送CAN数据失败: %s\n", strerror(errno)); + err_n++; + printf( "can write err cnt:%d\n", err_n ); + if( err_n > 4 ){ + err_n = 0; + printf( "TODO reset can \n" ); + can_bus_reset( device->bus_id); + } // 可以考虑重新入队 //queue_push(device, &frame); + }else{ + err_n = 0; } } } -- 2.34.1