From: kyz Date: Thu, 20 Nov 2025 02:33:05 +0000 (+0800) Subject: add log info X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=93e27736c137fa4adca57a57f93aefb3fe62a56f;p=yn%2Fpcs.git add log info --- diff --git a/src/main.c b/src/main.c index 5be0bc6..4dd31c9 100644 --- a/src/main.c +++ b/src/main.c @@ -46,42 +46,36 @@ static int alone_runnind(void) int main(void) { - uint16_t heartbeat_value = 0x1234; - uint32_t n = 0; + uint16_t heartbeat_value = 0x1234; if( alone_runnind()<0){ printf( "pcs_dtu already running ...exit!\n " ); exit(-1); } - user_log_init(); + printf( "GL_DTU V%s\n", SW_VERSION); + user_log_init(); - printf("---PCS DTU, Version:1.0\n"); -#if 1 // 启动PCS控制系统 + USER_LOGI("PCS controller start ...\n"); if ( pcs_start()==0) { - printf("PCS controller started successfully\n"); + //USER_LOGD("PCS controller started successfully\n"); }else{ - fprintf(stderr, "main: Failed to start PCS controller\n"); - return -1; + USER_LOGE( "Failed to start PCS controller\n"); } -#endif -#if 1 + // 启动BMS控制系统 + USER_LOGI("BMS controller start ...\n"); if (bms_start() == 0) { - printf("BMS controller started successfully\n"); + //USER_LOGD("BMS controller started successfully\n"); }else{ - fprintf(stderr, "main: Failed to start PCS controller\n"); - return -1; + USER_LOGE( "Failed to start BMS controller\n"); } -#endif + webserver_start(); while (1) { - sleep(1); - if( n++ > 5 ){ - n = 0; - user_bms_send_heartbeat( heartbeat_value++ ); - } + usleep(2*1000*1000); + user_bms_send_heartbeat( heartbeat_value++ ); } return 0; diff --git a/src/user.h b/src/user.h index 1d2d3f4..2531f6a 100644 --- a/src/user.h +++ b/src/user.h @@ -15,6 +15,8 @@ #include "user_pcs.h" #include "user_web.h" +#define SW_VERSION "1.0" + #ifdef __x86_64__ #define PID_FILE "/tmp/pcs_dtu.pid" diff --git a/src/user_bms.c b/src/user_bms.c index adb5f39..263dfca 100644 --- a/src/user_bms.c +++ b/src/user_bms.c @@ -51,6 +51,7 @@ bool user_bms_send_heartbeat(uint16_t heartbeat_value) // 设置CAN ID和数据长度 frame.id = PCS_HEARTBEAT_ID; frame.dlc = 8; + frame.is_extended = 1; // 设置心跳值 (小端格式) frame.data[0] = heartbeat_value & 0xFF; @@ -60,7 +61,7 @@ bool user_bms_send_heartbeat(uint16_t heartbeat_value) memset(&frame.data[2], 0, 6); // 发送CAN帧 - printf( "bms write ... \n" ); + //USER_LOGD( "bms write heartbeat... \n" ); if (can_device_send( g_bms.can, &frame) == 0) { } @@ -87,7 +88,7 @@ void user_bms_parse_frame1(const uint8_t *data) pthread_mutex_unlock( &g_bms.mutex ); - printf( "can recv frame 1: max_charge_current_p1a:%d, max_discharge_current_p1a:%d, total_voltage_p1v:%d, total_current_p1a:%d\n", + USER_LOGD( "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, @@ -113,7 +114,7 @@ void user_bms_parse_frame2(const uint8_t *data) pthread_mutex_unlock( &g_bms.mutex ); - printf( "can recv frame 2: total_soc_p1:%d, total_soh_p1:%d, max_charge_voltage_p1v:%d, reserve_2:%d\n", + USER_LOGD( "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, @@ -138,7 +139,7 @@ void user_bms_parse_frame3(const uint8_t *data) g_bms.data.serious_alarm = le16_to_host(data + 6); pthread_mutex_unlock( &g_bms.mutex ); - printf( "can recv frame 3: battery_status:%d, light_alarm:%d, medium_alarm:%d, serious_alarm:%d\n", + USER_LOGD( "can recv frame 3: battery_status:%d, light_alarm:%d, medium_alarm:%d, serious_alarm:%d\n", g_bms.data.battery_status, g_bms.data.light_alarm, g_bms.data.medium_alarm, @@ -162,7 +163,7 @@ void user_bms_parse_frame4(const uint8_t *data) g_bms.data.max_cell_num = le16_to_host(data + 6); pthread_mutex_unlock( &g_bms.mutex ); - printf( "can recv frame 4: min_cell_voltage_p3v: %d, min_cell_num:%d, max_cell_voltage_p3v:%d, max_cell_num:%d\n", + USER_LOGD( "can recv frame 4: min_cell_voltage_p3v: %d, min_cell_num:%d, max_cell_voltage_p3v:%d, max_cell_num:%d\n", g_bms.data.min_cell_voltage_p3v, g_bms.data.min_cell_num, g_bms.data.max_cell_voltage_p3v, @@ -187,7 +188,7 @@ void user_bms_parse_frame5(const uint8_t *data) g_bms.data.max_temp_num = le16_to_host(data + 6); pthread_mutex_unlock( &g_bms.mutex ); - printf( "can recv frame 5: min_cell_temp_p1c:%d, min_temp_num:%d, max_cell_temp_p1c:%d, max_temp_num:%d \n", + USER_LOGD( "can recv frame 5: min_cell_temp_p1c:%d, min_temp_num:%d, max_cell_temp_p1c:%d, max_temp_num:%d \n", g_bms.data.min_cell_temp_p1c, g_bms.data.min_temp_num, g_bms.data.max_cell_temp_p1c, @@ -248,27 +249,21 @@ void user_bms_deinit(void) static int bms_connect_cb(can_device_t *dev, int connected, void *user_data) { - printf( "bms connect cb ---->%d\n\n", connected ); - g_bms.data.connected = connected; + USER_LOGI( "BMS connect cb( connected %d-->%d)\n", g_bms.data.connected, connected ); + g_bms.data.connected = connected; return 0; } // CAN数据接收回调函数 static int bms_receive_cb(can_device_t *dev, can_frame_t *frame, void *user_data) { - int ret = -1; - printf("收到CAN数据帧:\n"); - printf(" ID: 0x%08X\n", frame->id); - printf(" 类型: %s\n", frame->is_extended ? "扩展帧" : "标准帧"); - printf(" 长度: %d 字节\n", frame->dlc); - printf(" 数据: "); - for (int i = 0; i < frame->dlc; i++) { - printf("0x%02X ", frame->data[i]); - } - printf("\n\n"); - if (can_device_send( dev, frame) == 0) { - printf("send echo data...\n"); - } + int ret = -1; + USER_LOGD("收到CAN数据帧: ID: 0x%08X 类型: %s 长度: %d 字节\n" , + frame->id, frame->is_extended ? "扩展帧" : "标准帧", frame->dlc); + USER_LOGD(" 数据: %02X %02X %02X %02X %02X %02X %02X %02X\n", + frame->data[0],frame->data[1],frame->data[2],frame->data[3], + frame->data[4],frame->data[5],frame->data[6],frame->data[7] ); + // 根据CAN ID解析不同的数据帧 if(frame->id == BMS_FRAME1_ID && frame->dlc == 8) { user_bms_parse_frame1(frame->data); diff --git a/src/user_bms.h b/src/user_bms.h index 53480f9..df046a5 100644 --- a/src/user_bms.h +++ b/src/user_bms.h @@ -10,15 +10,18 @@ #define BMS_ADDR_1 0x01 #define BMS_ADDR_2 0x02 #define BMS_ADDR_3 0x03 + +#define BMS_ADDR (BMS_ADDR_1) #define PCS_ADDR 0x27 + // CAN帧ID定义 -#define BMS_FRAME1_ID 0x18102701 // BMS数据帧1 -#define BMS_FRAME2_ID 0x18112701 // BMS数据帧2 -#define BMS_FRAME3_ID 0x18122701 // BMS数据帧3 -#define BMS_FRAME4_ID 0x18132701 // BMS数据帧4 -#define BMS_FRAME5_ID 0x18142701 // BMS数据帧5 -#define PCS_HEARTBEAT_ID 0x18160127 // PCS心跳帧 +#define BMS_FRAME1_ID (0x18100000|(PCS_ADDR<<8)|BMS_ADDR) // BMS数据帧1 0x18102701 +#define BMS_FRAME2_ID (0x18110000|(PCS_ADDR<<8)|BMS_ADDR) // BMS数据帧2 0x18112701 +#define BMS_FRAME3_ID (0x18120000|(PCS_ADDR<<8)|BMS_ADDR) // BMS数据帧3 0x18122701 +#define BMS_FRAME4_ID (0x18130000|(PCS_ADDR<<8)|BMS_ADDR) // BMS数据帧4 0x18132701 +#define BMS_FRAME5_ID (0x18140000|(PCS_ADDR<<8)|BMS_ADDR) // BMS数据帧5 0x18142701 +#define PCS_HEARTBEAT_ID (0x18160000|(BMS_ADDR<<8)|PCS_ADDR) // PCS心跳帧 0x18160127 // BMS数据结构体定义 typedef struct { diff --git a/src/user_can.c b/src/user_can.c index add8948..38d53ff 100644 --- a/src/user_can.c +++ b/src/user_can.c @@ -298,13 +298,13 @@ int can_device_send(can_device_t *device, can_frame_t *frame) { // 检查数据长度 if (frame->dlc > 8) { - fprintf(stderr, "错误: CAN数据长度不能超过8字节\n"); + USER_LOGE( "错误: CAN数据长度不能超过8字节\n"); return -1; } // 将数据帧加入发送队列 if (queue_push(device, frame) < 0) { - fprintf(stderr, "发送队列已满\n"); + USER_LOGE( "发送队列已满\n"); return -1; } diff --git a/src/user_log.c b/src/user_log.c index 434b449..ad3a66b 100644 --- a/src/user_log.c +++ b/src/user_log.c @@ -23,7 +23,7 @@ void user_log_init( void ) /* 获取已配置的category */ user_log_category = log4c_category_get( LOG_CATEGORY ); - printf("XXX user_log_init: log4c_category_get ret:%p\n", user_log_category ); + printf("user_log_init: log4c_category_get ret:%p\n", user_log_category ); err: return; diff --git a/src/user_modbus.c b/src/user_modbus.c index 96db15f..e4e2f2f 100644 --- a/src/user_modbus.c +++ b/src/user_modbus.c @@ -6,6 +6,7 @@ #include #include "user_modbus.h" +#include "user_log.h" // 全局总线列表 @@ -128,12 +129,13 @@ static void *bus_thread_func(void *arg) // 读取寄存器 if (modbus_read_registers(bus->modbus_ctx, area->start_addr, area->count, area->data) == -1) { - fprintf(stderr, "%s read error: %s\n", device->name, modbus_strerror(errno)); + //USER_LOGD("%s read error: %s\n", device->name, modbus_strerror(errno)); device->read_err_count++; if( device->read_err_count >= 5 ){ if( device->connected == 1 ){ device->connected = 0; - printf( "%s on bus[%s] ----------------> disconnected\n", device->name, bus->port_cfg.port ); + USER_LOGD("%s read error: %s\n", device->name, modbus_strerror(errno)); + USER_LOGD("%s on bus[%s] ----------------> disconnected\n", device->name, bus->port_cfg.port ); } } }else{ diff --git a/src/user_pcs.c b/src/user_pcs.c index 194666c..a14b62d 100644 --- a/src/user_pcs.c +++ b/src/user_pcs.c @@ -817,7 +817,7 @@ int pcs_start(void) { pthread_t thread_id; int ret = pthread_create(&thread_id, NULL, pcs_task_thread, NULL); if (ret != 0) { - perror("pcs_start: pthread_create failed"); + USER_LOGE("pcs_start: pthread_create failed\n"); return -1; }