From aab9ecaef10101f97c3675ff8bc311603e4bced0 Mon Sep 17 00:00:00 2001 From: kyz Date: Tue, 28 Oct 2025 21:16:03 +0800 Subject: [PATCH] modify pcs interface --- src/main.c | 6 ++++-- src/rtu_master.c | 13 +++++++------ src/user_mb.c | 7 +++++-- src/user_mb.h | 4 ++-- src/user_pcs.c | 38 ++++++++++++++++++++++++++++++++------ src/user_pcs.h | 7 ++++++- 6 files changed, 56 insertions(+), 19 deletions(-) diff --git a/src/main.c b/src/main.c index e64c067..04caee0 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,8 @@ int main(void) { user_log_init(); + printf("---PCS DTU, Version:1.0\n"); + // 启动PCS控制系统 if ( pcs_start()==0) { printf("PCS controller started successfully\n"); @@ -19,7 +21,7 @@ int main(void) fprintf(stderr, "main: Failed to start PCS controller\n"); return -1; } - +#if 0 // 启动BMS控制系统 if (bms_start() == 0) { printf("BMS controller started successfully\n"); @@ -28,7 +30,7 @@ int main(void) return -1; } webserver_start(); - +#endif while (1) { sleep(1); } diff --git a/src/rtu_master.c b/src/rtu_master.c index 88daae5..c7c2d21 100644 --- a/src/rtu_master.c +++ b/src/rtu_master.c @@ -72,17 +72,18 @@ int rtu_mb_init( rtu_bus_id_t bus, int baud, char parity, int data_bits, int sto USER_LOGE( "无法创建Modbus RTU主机上下文"); return -1; } + USER_LOGI( "-----------创建Modbus RTU主机上下文 %p, dev:%s, baud:%d", ctx, mb_rtu_port[bus], baud ); modbus_set_response_timeout(ctx, 1, 0); if( bus == MB_RTU_BUS_1 ){ //ttl }else if( bus == MB_RTU_BUS_2 ){ - system( "echo 14 > /sys/class/gpio/export" ); - system( "echo out > /sys/class/gpio/gpio14/direction" ); - system( "echo 0 > /sys/class/gpio/gpio14/value" ); +// system( "echo 14 > /sys/class/gpio/export" ); +// system( "echo out > /sys/class/gpio/gpio14/direction" ); +// system( "echo 0 > /sys/class/gpio/gpio14/value" ); }else if( bus == MB_RTU_BUS_3 ){ - system( "echo 0 > /sys/class/gpio/export" ); - system( "echo out > /sys/class/gpio/gpio0/direction" ); - system( "echo 0 > /sys/class/gpio/gpio0/value" ); +// system( "echo 0 > /sys/class/gpio/export" ); +// system( "echo out > /sys/class/gpio/gpio0/direction" ); +// system( "echo 0 > /sys/class/gpio/gpio0/value" ); }else if( bus == MB_RTU_BUS_4 ){ }else if( bus == MB_RTU_BUS_5 ){ } diff --git a/src/user_mb.c b/src/user_mb.c index 5ef6626..9066216 100644 --- a/src/user_mb.c +++ b/src/user_mb.c @@ -186,7 +186,6 @@ bool user_mb_get_reg_value(user_mb_handler_t *handler, uint16_t slave_id, user_m if( handler->reg_ranges[i].start_addr <= reg_addr && reg_addr < handler->reg_ranges[i].start_addr + handler->reg_ranges[i].count ){ *value = handler->reg_ranges[i].reg_values[reg_addr - handler->reg_ranges[i].start_addr]; pthread_mutex_unlock(&handler->mutex); - pthread_mutex_unlock(&handler->mutex); return true; } } @@ -231,9 +230,13 @@ static void *user_mb_thread_func(void *arg) { //printf( "to read addr[%d], range( %d -- %d )\n", handler->mb_address, range->start_addr, range->count ); read_count = rtu_mb_read_register(handler->mb_bus, handler->mb_address, range->start_addr, range->count, read_buffer); if( read_count == 0 ){ + pthread_mutex_lock(&handler->mutex); memcpy( (void*)&range->reg_values[0], read_buffer, range->count * sizeof(uint16_t)); + pthread_mutex_unlock(&handler->mutex); + } + if( read_count<0){ + user_log_error( "mb read hold reg %d , n:%d, ret:%d\n", range->start_addr, range->count, read_count); } - user_log_info( "mb read hold reg %d , n:%d, ret:%d\n", range->start_addr, range->count, read_count); /* printf( "read raw:["); for( int i=0; i< range->count; i++ ){ diff --git a/src/user_mb.h b/src/user_mb.h index 2c1920c..0502d9b 100644 --- a/src/user_mb.h +++ b/src/user_mb.h @@ -10,9 +10,9 @@ #include -#define MB_RTU_PORT_PATH_1 "/dev/ttyUSB0" +#define MB_RTU_PORT_PATH_1 "/dev/ttyS3" #define MB_RTU_PORT_PATH_2 "/dev/ttyS2" -#define MB_RTU_PORT_PATH_3 "/dev/ttyS3" +#define MB_RTU_PORT_PATH_3 "/dev/ttySUSB0" #define MB_RTU_PORT_PATH_4 "/dev/ttyS4" #define MB_RTU_PORT_PATH_5 "/dev/ttyS5" diff --git a/src/user_pcs.c b/src/user_pcs.c index 56fc77a..ce178e8 100644 --- a/src/user_pcs.c +++ b/src/user_pcs.c @@ -19,6 +19,18 @@ struct user_pcs_handler { volatile bool running; }; +/* 获取PCS设备信息 */ +int user_pcs_data_get(pcs_data_t *pcs_data) +{ + if( !pcs_data ){ + return -1; + } + pthread_mutex_lock(&g_pcs.mutex); + memcpy( pcs_data, &g_pcs.data, sizeof( pcs_data_t )); + pthread_mutex_unlock(&g_pcs.mutex); + return 0; +} + /* 协议解析函数 - 根据pcs-modbus.txt协议实现 */ int user_pcs_read_all(user_pcs_handler_t *pcs, pcs_data_t *pcs_data) { @@ -838,6 +850,8 @@ static void* pcs_task_thread(void* arg) { user_pcs_handler_t *pcs_handler = user_pcs_create("Main-PCS-Controller", MB_RTU_BUS_1, 1); if (pcs_handler != NULL) { g_pcs.handler = pcs_handler; + /* 初始化互斥锁 */ + pthread_mutex_init(&g_pcs, NULL); // 启动PCS模块 if (user_pcs_start(pcs_handler)) { printf("PCS module started successfully\n"); @@ -851,12 +865,6 @@ static void* pcs_task_thread(void* arg) { printf("Successfully updated target voltage to %d\n", new_target_voltage); } - // 示例:写入另一个参数 - uint16_t onoff = 1; - if (user_pcs_write_single_param(pcs_handler, USER_PCS_PARAM_ON_OFF_SETTING, &onoff)) { - printf("Successfully enabled protection\n"); - } - while (1) { // 读取设备信息 if (user_pcs_read_all(pcs_handler, &pcs_data) == 0) { @@ -864,14 +872,30 @@ static void* pcs_task_thread(void* arg) { pcs_data.firmware_version, pcs_data.protection_enable, pcs_data.reactive_overvoltage_exit_p1v); + + if( memcmp( &g_pcs.data, &pcs_data, sizeof( pcs_data )) != 0){ + uint16_t *s, *d; + d = (uint16_t*)&pcs_data; + s = (uint16_t*)&g_pcs.data; + printf( "-------------------pcs data --> changed\n"); + for( int i=0; i %d\n", i, *s, *d); + } + printf("\n"); + } + //locked + pthread_mutex_lock(&g_pcs.mutex); memcpy( &g_pcs.data, &pcs_data, sizeof( pcs_data_t ) ); + pthread_mutex_unlock(&g_pcs.mutex); //unlocked } // 每2秒读取一次 sleep(2); n++; + #if 0 if (n % 2) { uint16_t onoff = n; int16_t new_target_voltage = 2205 + n * 10; @@ -888,6 +912,7 @@ static void* pcs_task_thread(void* arg) { printf("failed!\n"); } } + #endif } // 实际应用中应添加退出逻辑 @@ -900,6 +925,7 @@ static void* pcs_task_thread(void* arg) { } while (1) { + sleep(5); } diff --git a/src/user_pcs.h b/src/user_pcs.h index 65a43eb..5cd87b0 100644 --- a/src/user_pcs.h +++ b/src/user_pcs.h @@ -122,7 +122,7 @@ typedef struct{ uint16_t reserved_77; // 保留 (地址77) uint16_t reserved_78; // 保留 (地址78) uint16_t reserved_79; // 保留 (地址79) - + uint16_t reserved_80_99[20]; // 保留 (地址80~99) // 新增寄存器100-298定义 uint16_t protection_enable; // 保护使能位设置 (地址100) uint16_t grid_overvoltage1_prot_time; // 电网过压1保护时间 (地址101) @@ -171,6 +171,7 @@ typedef struct{ pcs_data_t data; int connected; user_pcs_handler_t *handler; + pthread_mutex_t mutex; } pcs_info_t; /* 创建PCS模块实例 */ @@ -191,6 +192,10 @@ int user_pcs_read_all(user_pcs_handler_t *pcs, pcs_data_t *pcs_data); int user_pcs_write_all(user_pcs_handler_t *pcs, const pcs_data_t *pcs_data); /* 写入单个PCS参数 */ int user_pcs_write_single_param(user_pcs_handler_t *pcs, user_pcs_param_type_t param_type, const void *value); + +/* 获取PCS设备信息 */ +int user_pcs_data_get(pcs_data_t *pcs_data); + #ifdef __cplusplus } #endif -- 2.34.1