reset for tx fail
authorkyz <kyz@kyz.pub>
Fri, 14 Nov 2025 05:47:02 +0000 (13:47 +0800)
committerkyz <kyz@kyz.pub>
Fri, 14 Nov 2025 05:47:02 +0000 (13:47 +0800)
src/user_can.c

index e0f531a2c5f181e6928eeee727da9cd71a8eb8e0..1ab726ec35af3b8bf19433464c4a1e423c8372d9 100644 (file)
@@ -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;
                     }
                 }
             }