--- /dev/null
+/*
+ * lws-minimal-http-server-form-get
+ *
+ * Written in 2010-2019 by Andy Green <andy@warmcat.com>
+ *
+ * This file is made available under the Creative Commons CC0 1.0
+ * Universal Public Domain Dedication.
+ *
+ * This demonstrates a minimal http server that performs a form GET with a couple
+ * of parameters. It dumps the parameters to the console log and redirects
+ * to another page.
+ */
+
+#include <libwebsockets.h>
+#include <string.h>
+#include <signal.h>
+
+static int interrupted;
+
+static const char * param_names[] = {
+ "text1",
+ "send"
+};
+
+static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
+ void *in, size_t len)
+{
+ uint8_t buf[LWS_PRE + LWS_RECOMMENDED_MIN_HEADER_SPACE],
+ *start = &buf[LWS_PRE], *p = start,
+ *end = &buf[sizeof(buf) - 1];
+ int n;
+
+ switch (reason) {
+ case LWS_CALLBACK_HTTP:
+
+ if (!lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI))
+ /* not a GET */
+ break;
+ lwsl_err("%s: %s\n", __func__, (const char *)in);
+ if (strcmp((const char *)in, "/api"))
+ /* not our form URL */
+ break;
+
+ /* we just dump the decoded things to the log */
+
+ for (n = 0; n < (int)LWS_ARRAY_SIZE(param_names); n++) {
+ int rv = lws_get_urlarg_by_name_safe(wsi, param_names[n],
+ (char *)buf, sizeof(buf));
+ if (rv < 0)
+ lwsl_user("%s: undefined\n", param_names[n]);
+ else
+ lwsl_user("%s: (len %d) '%s'\n", param_names[n],
+ (int)rv, buf);
+ }
+
+ /*
+ * 修改为返回JSON格式数据,而不是重定向到静态页面
+ */
+
+ // 设置HTTP响应头
+ const char *json_response = "{\"status\": \"success\", \"message\": \"API request processed\", \"params_received\": true}";
+ int json_len = strlen(json_response);
+
+ if (lws_add_http_common_headers(wsi, HTTP_STATUS_OK, "application/json",
+ json_len, &p, end) < 0)
+ return -1;
+ printf( "reponse http header:[%s]\n", start);
+ // 发送HTTP头
+ if (lws_finalize_write_http_header(wsi, start, &p, end) < 0)
+ return -1;
+
+ // 准备JSON响应内容
+ // 发送JSON响应体
+ if (lws_write(wsi, (unsigned char *)json_response, strlen(json_response), LWS_WRITE_HTTP_FINAL) < 0){
+ printf( " response json data: %s fail!\n", json_response);
+ return -1;
+ }else{
+ printf( " response json data: %s success!\n", json_response);
+ }
+
+ break;
+
+ default:
+ break;
+ }
+
+ return lws_callback_http_dummy(wsi, reason, user, in, len);
+}
+
+static struct lws_protocols protocols[] = {
+ { "http", callback_http, 0, 0, 0, NULL, 0 },
+ LWS_PROTOCOL_LIST_TERM
+};
+
+/* default mount serves the URL space from ./mount-origin */
+
+static const struct lws_http_mount mount = {
+ /* .mount_next */ NULL, /* linked-list "next" */
+ /* .mountpoint */ "/", /* mountpoint URL */
+ /* .origin */ "./mount-origin", /* serve from dir */
+ /* .def */ "index.html", /* default filename */
+ /* .protocol */ NULL,
+ /* .cgienv */ NULL,
+ /* .extra_mimetypes */ NULL,
+ /* .interpret */ NULL,
+ /* .cgi_timeout */ 0,
+ /* .cache_max_age */ 0,
+ /* .auth_mask */ 0,
+ /* .cache_reusable */ 0,
+ /* .cache_revalidate */ 0,
+ /* .cache_intermediaries */ 0,
+ /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
+ /* .mountpoint_len */ 1, /* char count */
+ /* .basic_auth_login_file */ NULL,
+};
+
+void sigint_handler(int sig)
+{
+ interrupted = 1;
+}
+
+int http_get_test( int argc, const char **argv)
+{
+ struct lws_context_creation_info info;
+ struct lws_context *context;
+ const char *p;
+ int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
+ /* for LLL_ verbosity above NOTICE to be built into lws,
+ * lws must have been configured and built with
+ * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
+ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
+ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
+ /* | LLL_DEBUG */;
+
+ //signal(SIGINT, sigint_handler);
+
+ lws_set_log_level(logs, NULL);
+ lwsl_user("LWS minimal http server GET | visit http://localhost:3000\n");
+
+ memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
+ info.port = 3000;
+ info.protocols = protocols;
+ info.mounts = NULL; //&mount;
+ info.options =
+ LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
+
+ context = lws_create_context(&info);
+ if (!context) {
+ lwsl_err("lws init failed\n");
+ return 1;
+ }
+
+ while (n >= 0 && !interrupted)
+ n = lws_service(context, 0);
+
+ lws_context_destroy(context);
+
+ return 0;
+}
#include <stdio.h>
#include <unistd.h>
#include "pcs_ctrl.h"
-#include "user_pcs.h"
-#include "user_log.h"
-#include "user_mb.h"
-int main(int argc, char **argv)
-{
- int n;
- user_log_init();
-
- printf("pcs_controller demo start!\n");
- user_log_info("-----------pcs_controller demo start!\n");
-
- // 创建并启动PCS模块
- user_pcs_handler_t *pcs_handler = user_pcs_create("Main-PCS-Controller", MB_RTU_BUS_1, 1);
- if (pcs_handler != NULL) {
- // 创建Modbus模块实例
- // 启动PCS模块
- if (user_pcs_start(pcs_handler)) {
- printf("PCS module started successfully\n");
-
- // 示例:读取和使用设备信息
- user_pcs_device_info_t device_info;
- memset(&device_info, 0, sizeof(device_info));
-
- // 示例:写入单个参数
- int16_t new_target_voltage = 2205;
- if (user_pcs_write_single_param(pcs_handler, USER_PCS_PARAM_TARGET_VOLTAGE, &new_target_voltage)) {
- 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, &device_info)==0) {
- printf("----------------Device ID: %d, protection_enable[100]:%d,reactive_overvoltage_exit[136]:%d\n",
- device_info.firmware_version,
- device_info.protection_enable,
- device_info.reactive_overvoltage_exit);
- }
-
- // 每5秒读取一次
- sleep(2);
- n++;
- if( n %2 ){
- uint16_t onoff = n; //
- int16_t new_target_voltage = 2205+n*10;
- printf( "write to reg 35 ---- ");
- if (user_pcs_write_single_param(pcs_handler, USER_PCS_PARAM_ON_OFF_SETTING, &onoff)==0) {
- printf("Successfully!\n");
- }else{
- printf("failed!\n");
- }
- printf( "write to reg 70 ---- ");
- if (user_pcs_write_single_param(pcs_handler, USER_PCS_PARAM_CLEAR_FAULT, &new_target_voltage)==0) {
- printf("Successfully!\n");
- }else{
- printf("failed!\n");
- }
- }
- }
-
- // 实际应用中,这里应该有合适的退出机制
- //user_pcs_stop(pcs_handler);
- } else {
- printf("Failed to start PCS module\n");
- }
+int bms_start(void);
+int pcs_start(void);
+int http_get_test( int argc, const char **argv);
+int main(void) {
+ // 启动PCS控制系统
+ if ( pcs_start()==0) {
+ printf("PCS controller started successfully\n");
}else{
- printf("Failed to create Modbus module\n");
+ fprintf(stderr, "main: Failed to start PCS controller\n");
+ return -1;
}
+ // 启动BMS控制系统
+ if (bms_start() == 0) {
+ printf("BMS controller started successfully\n");
+ }else{
+ fprintf(stderr, "main: Failed to start PCS controller\n");
+ return -1;
+ }
+ // 主线程保持运行(可根据需要添加信号处理或退出逻辑)
+ http_get_test(1, NULL );
while (1) {
- sleep(5);
+ sleep(1);
}
return 0;
}
-
#include <linux/can.h>
#include <linux/can/raw.h>
#include <time.h>
+#include <pthread.h>
static int can_socket = -1;
static bms_data_t bms_data = {0};
void user_bms_parse_frame1(const uint8_t *data) {
// 解析最大允许充电电流 (0.1A)
- bms_data.max_charge_current = le16_to_host(data) * 0.1f;
+ bms_data.max_charge_current_p1a = le16_to_host(data);
// 解析最大允许放电电流 (0.1A)
- bms_data.max_discharge_current = le16_to_host(data + 2) * 0.1f;
+ bms_data.max_discharge_current_p1a = le16_to_host(data + 2);
// 解析总电压 (0.1V)
- bms_data.total_voltage = le16_to_host(data + 4) * 0.1f;
+ bms_data.total_voltage_p1v = le16_to_host(data + 4);
// 解析总电流 (0.1A)
- bms_data.total_current = le16s_to_host(data + 6) * 0.1f;
+ bms_data.total_current_p1a = le16s_to_host(data + 6);
}
void user_bms_parse_frame2(const uint8_t *data) {
// 解析总SOC (0.1%)
- bms_data.total_soc = le16_to_host(data) * 0.1f;
+ bms_data.total_soc_p1 = le16_to_host(data);
// 解析总SOH (0.1%)
- bms_data.total_soh = le16_to_host(data + 2) * 0.1f;
+ bms_data.total_soh_p1 = le16_to_host(data + 2);
// 解析最大允许充电电压 (0.1V)
- bms_data.max_charge_voltage = le16_to_host(data + 4) * 0.1f;
+ bms_data.max_charge_voltage_p1v = le16_to_host(data + 4);
// 第7-8字节为预留,不处理
+ bms_data.reserve_2 = le16_to_host(data + 6);
}
void user_bms_parse_frame3(const uint8_t *data) {
void user_bms_parse_frame4(const uint8_t *data) {
// 解析单体最低电池电压 (0.001V)
- bms_data.min_cell_voltage = le16_to_host(data) * 0.001f;
+ bms_data.min_cell_voltage_p3v = le16_to_host(data);
// 解析单体最低电池电压编号
bms_data.min_cell_num = le16_to_host(data + 2);
// 解析单体最高电池电压 (0.001V)
- bms_data.max_cell_voltage = le16_to_host(data + 4) * 0.001f;
+ bms_data.max_cell_voltage_p3v = le16_to_host(data + 4);
// 解析单体最高电池电压编号
bms_data.max_cell_num = le16_to_host(data + 6);
void user_bms_parse_frame5(const uint8_t *data) {
// 解析单体最低电池温度 (0.1℃)
- bms_data.min_cell_temp = le16s_to_host(data) * 0.1f;
+ bms_data.min_cell_temp_p1c = le16s_to_host(data);
// 解析单体最低电池温度编号
bms_data.min_temp_num = le16_to_host(data + 2);
// 解析单体最高电池温度 (0.1℃)
- bms_data.max_cell_temp = le16s_to_host(data + 4) * 0.1f;
+ bms_data.max_cell_temp_p1c = le16s_to_host(data + 4);
// 解析单体最高电池温度编号
bms_data.max_temp_num = le16_to_host(data + 6);
printf("BMS CAN interface closed\n");
}
}
+
+// 线程函数声明
+static void* bms_task_thread(void* arg)
+{
+ // 初始化BMS模块 (使用vcan0接口)
+ if (!user_bms_init("vcan0")) {
+ printf("Failed to initialize BMS module\n");
+ return NULL;
+ }
+
+ uint16_t heartbeat = 0;
+
+ // 主循环
+ while (1) {
+ // 发送心跳包 (每2秒一次)
+ if (heartbeat % 4 == 0) { // 因为主循环是500ms,4*500ms=2s
+ user_bms_send_heartbeat(heartbeat);
+ heartbeat++;
+ if (heartbeat > 65535) heartbeat = 0;
+ }
+
+ // 接收BMS数据
+ user_bms_receive_data();
+
+ // 获取BMS数据
+ const bms_data_t *bms = user_bms_get_data();
+
+ // 打印BMS状态
+ if (bms->is_connected) {
+ printf("\rBMS Connected - SOC: %.1f%% Voltage: %.1fV Current: %.1fA ",
+ bms->total_soc_p1/10.0f, bms->total_voltage_p1v/10.0, bms->total_current_p1a/10.0);
+ fflush(stdout);
+ } else {
+ printf("\rBMS Disconnected... ");
+ fflush(stdout);
+ }
+
+ // 500ms延时
+ usleep(500000);
+ }
+
+ // 关闭BMS模块
+ user_bms_deinit();
+ return NULL;
+}
+
+/**
+ * @brief 启动PCS控制线程
+ * @return true-启动成功 false-启动失败
+ */
+int bms_start(void) {
+ pthread_t thread_id;
+ int ret = pthread_create(&thread_id, NULL, bms_task_thread, NULL);
+ if (ret != 0) {
+ perror("pcs_start: pthread_create failed");
+ return -1;
+ }
+
+ // 分离线程,避免资源泄漏
+ pthread_detach(thread_id);
+ return 0;
+}
// BMS数据结构体定义
typedef struct {
// 数据帧1
- float max_charge_current; // 最大允许充电电流 (A)
- float max_discharge_current; // 最大允许放电电流 (A)
- float total_voltage; // 总电压 (V)
- float total_current; // 总电流 (A)
+ uint16_t max_charge_current_p1a; // 最大允许充电电流 (A)
+ uint16_t max_discharge_current_p1a; // 最大允许放电电流 (A)
+ uint16_t total_voltage_p1v; // 总电压 (V)
+ int16_t total_current_p1a; // 总电流 (A)
// 数据帧2
- float total_soc; // 总SOC (%)
- float total_soh; // 总SOH (%)
- float max_charge_voltage; // 最大允许充电电压 (V)
+ uint16_t total_soc_p1; // 总SOC (%)
+ uint16_t total_soh_p1; // 总SOH (%)
+ uint16_t max_charge_voltage_p1v; // 最大允许充电电压 (V)
+ uint16_t reserve_2;
// 数据帧3
uint16_t battery_status; // 电池状态
uint16_t serious_alarm; // 严重告警
// 数据帧4
- float min_cell_voltage; // 单体最低电池电压 (V)
- uint16_t min_cell_num; // 单体最低电池电压编号
- float max_cell_voltage; // 单体最高电池电压 (V)
- uint16_t max_cell_num; // 单体最高电池电压编号
+ uint16_t min_cell_voltage_p3v; // 单体最低电池电压 (V)
+ uint16_t min_cell_num; // 单体最低电池电压编号
+ uint16_t max_cell_voltage_p3v; // 单体最高电池电压 (V)
+ uint16_t max_cell_num; // 单体最高电池电压编号
// 数据帧5
- float min_cell_temp; // 单体最低电池温度 (℃)
- uint16_t min_temp_num; // 单体最低电池温度编号
- float max_cell_temp; // 单体最高电池温度 (℃)
- uint16_t max_temp_num; // 单体最高电池温度编号
+ int16_t min_cell_temp_p1c; // 单体最低电池温度 (℃)
+ uint16_t min_temp_num; // 单体最低电池温度编号
+ int16_t max_cell_temp_p1c; // 单体最高电池温度 (℃)
+ uint16_t max_temp_num; // 单体最高电池温度编号
// 内部状态
bool is_connected; // BMS连接状态
return true;
}
+
+
+
+
+/**
+ * @brief PCS控制任务线程函数
+ */
+static void* pcs_task_thread(void* arg) {
+ int n = 0; // 修复原代码中n未初始化问题
+ user_log_init();
+
+ printf("pcs_controller demo start!\n");
+ user_log_info("-----------pcs_controller demo start!\n");
+
+ // 创建并启动PCS模块
+ user_pcs_handler_t *pcs_handler = user_pcs_create("Main-PCS-Controller", MB_RTU_BUS_1, 1);
+ if (pcs_handler != NULL) {
+ // 启动PCS模块
+ if (user_pcs_start(pcs_handler)) {
+ printf("PCS module started successfully\n");
+
+ // 示例:读取和使用设备信息
+ user_pcs_device_info_t device_info;
+ memset(&device_info, 0, sizeof(device_info));
+
+ // 示例:写入单个参数
+ int16_t new_target_voltage = 2205;
+ if (user_pcs_write_single_param(pcs_handler, USER_PCS_PARAM_TARGET_VOLTAGE, &new_target_voltage)) {
+ 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, &device_info) == 0) {
+ printf("----------------Device ID: %d, protection_enable[100]:%d,reactive_overvoltage_exit[136]:%d\n",
+ device_info.firmware_version,
+ device_info.protection_enable,
+ device_info.reactive_overvoltage_exit);
+ }
+
+ // 每2秒读取一次
+ sleep(2);
+ n++;
+ if (n % 2) {
+ uint16_t onoff = n;
+ int16_t new_target_voltage = 2205 + n * 10;
+ printf("write to reg 35 ---- ");
+ if (user_pcs_write_single_param(pcs_handler, USER_PCS_PARAM_ON_OFF_SETTING, &onoff) == 0) {
+ printf("Successfully!\n");
+ } else {
+ printf("failed!\n");
+ }
+ printf("write to reg 70 ---- ");
+ if (user_pcs_write_single_param(pcs_handler, USER_PCS_PARAM_CLEAR_FAULT, &new_target_voltage) == 0) {
+ printf("Successfully!\n");
+ } else {
+ printf("failed!\n");
+ }
+ }
+ }
+
+ // 实际应用中应添加退出逻辑
+ // user_pcs_stop(pcs_handler);
+ } else {
+ printf("Failed to start PCS module\n");
+ }
+ } else {
+ printf("Failed to create Modbus module\n");
+ }
+
+ while (1) {
+ sleep(5);
+ }
+
+ return NULL;
+}
+
+// 线程函数声明
+
+/**
+ * @brief 启动PCS控制线程
+ * @return true-启动成功 false-启动失败
+ */
+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");
+ return -1;
+ }
+
+ // 分离线程,避免资源泄漏
+ pthread_detach(thread_id);
+ return 0;
+}