can0 read
authorkyz <kyz@kyz.pub>
Wed, 29 Oct 2025 10:06:59 +0000 (18:06 +0800)
committerkyz <kyz@kyz.pub>
Wed, 29 Oct 2025 10:06:59 +0000 (18:06 +0800)
src/main.c
src/user_bms.c

index 9477a4b7bac6fa13aae72c405e90931a8e2f2b0f..fb96cee9e90e9e62ad1f650628749b8b0236416d 100644 (file)
@@ -15,7 +15,7 @@ int main(void)
    user_log_init();
 
    printf("---PCS DTU, Version:1.0\n");
-
+#if 0
     // 启动PCS控制系统
     if ( pcs_start()==0) {
         printf("PCS controller started successfully\n");
@@ -23,7 +23,8 @@ int main(void)
         fprintf(stderr, "main: Failed to start PCS controller\n");
         return -1;
     }
-#if 0
+#endif
+#if 1
     // 启动BMS控制系统
     if (bms_start() == 0) {
         printf("BMS controller started successfully\n");
index cc37a7505ebf84c9454e7741c59c1a6c7f32355e..d435c597d6d28d40ea6fd741b037dd917b0f5b1a 100644 (file)
@@ -99,6 +99,11 @@ void user_bms_parse_frame1(const uint8_t *data) {
 
     // 解析总电流 (0.1A)
     g_bms.data.total_current_p1a = le16s_to_host(data + 6);
+    printf( "can recv frame 1: max_charge_current_p1a:%d, max_discharge_current_p1a:%d, total_voltage_p1v:%d, total_current_p1a:%d\n",
+        g_bms.data.max_charge_current_p1a,
+        g_bms.data.max_discharge_current_p1a,
+        g_bms.data.total_voltage_p1v,
+        g_bms.data.total_current_p1a );
 }
 
 void user_bms_parse_frame2(const uint8_t *data) {
@@ -113,6 +118,11 @@ void user_bms_parse_frame2(const uint8_t *data) {
 
     // 第7-8字节为预留,不处理
     g_bms.data.reserve_2 = le16_to_host(data + 6);
+    printf( "can recv frame 2: total_soc_p1:%d, total_soh_p1:%d, max_charge_voltage_p1v:%d, reserve_2:%d\n",
+        g_bms.data.total_soc_p1,
+        g_bms.data.total_soh_p1,
+        g_bms.data.max_charge_voltage_p1v,
+        g_bms.data.reserve_2 );
 }
 
 void user_bms_parse_frame3(const uint8_t *data) {
@@ -167,16 +177,23 @@ void user_bms_receive_data(void) {
         perror("read");
         return;
     }
-
+    printf( "can read msg, size:%d\n", nbytes );
     if (nbytes != sizeof(struct can_frame)) return;
 
+    printf( "read frame: id:%08X, dlc:%d, data:", frame.can_id, frame.can_dlc );
+    for( int i=0; i<frame.can_dlc; i++ ){
+        printf( "%02x ", frame.data[i] );
+    }
+    printf( "\n" );
+
     // 更新最后接收时间
     g_bms.data.last_update_time = time(NULL);
     g_bms.data.is_connected = true;
-
+    frame.can_id &= 0x1FFFFFFF;
     // 根据CAN ID解析不同的数据帧
     switch (frame.can_id) {
         case BMS_FRAME1_ID:
+printf( "FRAME1_ID\n" );
             if (frame.can_dlc == 8) {
                 user_bms_parse_frame1(frame.data);
             }
@@ -241,7 +258,7 @@ void user_bms_deinit(void) {
 static void* bms_task_thread(void* arg)
 {
   // 初始化BMS模块 (使用vcan0接口)
-    if (!user_bms_init("vcan0")) {
+    if (!user_bms_init("can0")) {
         printf("Failed to initialize BMS module\n");
         return NULL;
     }
@@ -251,9 +268,9 @@ static void* bms_task_thread(void* arg)
     // 主循环
     while (1) {
         // 发送心跳包 (每2秒一次)
+        heartbeat++;
         if (heartbeat % 4 == 0) {  // 因为主循环是500ms,4*500ms=2s
             user_bms_send_heartbeat(heartbeat);
-            heartbeat++;
             if (heartbeat > 65535) heartbeat = 0;
         }