1-修改中控串口数据分析线程操作逻辑为双队列
2-数据库线程添加连续1000调用强制关闭一次数据库以清理内存
This commit is contained in:
1 parent
efc78addd9
commit
4096b49a15
4 files changed
+31
-27
No files matched your search
@@ -63,29 +63,22 @@ void uart_thread_entry(void *parameter)
|
|||||||
if (!req) continue;
|
if (!req) continue;
|
||||||
rt_strncpy(req->input, (const char *)input, sizeof(req->input));
|
rt_strncpy(req->input, (const char *)input, sizeof(req->input));
|
||||||
req->input_len = len;
|
req->input_len = len;
|
||||||
req->sem = rt_malloc(sizeof(struct rt_semaphore));
|
|
||||||
if (!req->sem) {
|
|
||||||
rt_free(req);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
rt_sem_init(req->sem, "uart_resp", 0, RT_IPC_FLAG_FIFO);
|
|
||||||
|
|
||||||
// 发送到处理线程
|
// 发送到处理线程
|
||||||
if (rt_mq_send(proc_mq, &req, sizeof(req)) == RT_EOK)
|
if (rt_mq_send(request_mq, &req, sizeof(req)) == RT_EOK)
|
||||||
{
|
{
|
||||||
// 等待处理完成
|
// 等待处理完成
|
||||||
if (rt_sem_take(req->sem, rt_tick_from_millisecond(100)) >= RT_EOK)
|
if (rt_mq_recv(response_mq, &req, sizeof(req), 500) >= RT_EOK)
|
||||||
{
|
{
|
||||||
if(req->output_len>0) rs485_send(hinst, req->output, req->output_len);
|
if(req->output_len>0) rs485_send(hinst, req->output, req->output_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 清理
|
// 清理
|
||||||
rt_sem_detach(req->sem); // 或 rt_sem_delete
|
|
||||||
rt_free(req->sem);
|
|
||||||
rt_free(req);
|
rt_free(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern struct rt_memheap sram_DTCMRAM;
|
extern struct rt_memheap sram_DTCMRAM;
|
||||||
static struct rt_thread dat_uart_thread;
|
static struct rt_thread dat_uart_thread;
|
||||||
static void *dat_uart_stack __attribute__((aligned(4)))= RT_NULL ;
|
static void *dat_uart_stack __attribute__((aligned(4)))= RT_NULL ;
|
||||||
|
|||||||
@@ -24,8 +24,8 @@
|
|||||||
#define DBG_LVL DBG_LOG
|
#define DBG_LVL DBG_LOG
|
||||||
#include <rtdbg.h>
|
#include <rtdbg.h>
|
||||||
|
|
||||||
rt_mq_t proc_mq;
|
struct rt_messagequeue *request_mq; // UART → Proc
|
||||||
//static struct proc_request *req;
|
struct rt_messagequeue *response_mq; // Proc → UART
|
||||||
|
|
||||||
char cjson_falg=0;//是否解析cjson
|
char cjson_falg=0;//是否解析cjson
|
||||||
char *json_buffer=NULL;//
|
char *json_buffer=NULL;//
|
||||||
@@ -535,7 +535,7 @@ void proc_thread_entry(void *parameter)
|
|||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
// 接收请求指针
|
// 接收请求指针
|
||||||
if (rt_mq_recv(proc_mq, &req, sizeof(req), RT_WAITING_FOREVER) >= RT_EOK)
|
if (rt_mq_recv(request_mq, &req, sizeof(req), RT_WAITING_FOREVER) >= RT_EOK)
|
||||||
{
|
{
|
||||||
if (!req) continue;
|
if (!req) continue;
|
||||||
memcpy(DATA_api,req->input,5);
|
memcpy(DATA_api,req->input,5);
|
||||||
@@ -565,11 +565,8 @@ void proc_thread_entry(void *parameter)
|
|||||||
{
|
{
|
||||||
req->input[i]=0;
|
req->input[i]=0;
|
||||||
}
|
}
|
||||||
// 通知 UART 线程可以发送了
|
// 发送到 UART 线程
|
||||||
if (req->sem)
|
rt_mq_send(response_mq, &req, sizeof(req));
|
||||||
{
|
|
||||||
rt_sem_release(req->sem);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -580,8 +577,9 @@ static void *dat_comm_stack __attribute__((aligned(4)))= RT_NULL ;
|
|||||||
int data_comm_init(void)
|
int data_comm_init(void)
|
||||||
{
|
{
|
||||||
rt_err_t dat_err;
|
rt_err_t dat_err;
|
||||||
proc_mq = rt_mq_create("proc_mq", sizeof(struct proc_request*), 5, RT_IPC_FLAG_FIFO);
|
request_mq = rt_mq_create("request_mq", sizeof(struct proc_request*), 5, RT_IPC_FLAG_FIFO);
|
||||||
if (proc_mq == RT_NULL)
|
response_mq = rt_mq_create("response_mq", sizeof(struct proc_request*), 5, RT_IPC_FLAG_FIFO);
|
||||||
|
if ((request_mq == RT_NULL) || (response_mq ==RT_NULL))
|
||||||
{
|
{
|
||||||
LOG_E("Failed to create message queue!\n");
|
LOG_E("Failed to create message queue!\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@@ -20,11 +20,11 @@ struct proc_request {
|
|||||||
int input_len;
|
int input_len;
|
||||||
char output[MAX_OUTPUT_LEN];
|
char output[MAX_OUTPUT_LEN];
|
||||||
int output_len;
|
int output_len;
|
||||||
struct rt_semaphore *sem; // 指向动态分配的信号量
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 消息队列(供外部使用)
|
// 双消息队列声明
|
||||||
extern rt_mq_t proc_mq;
|
extern struct rt_messagequeue *request_mq; // UART → Proc
|
||||||
|
extern struct rt_messagequeue *response_mq; // Proc → UART
|
||||||
|
|
||||||
int data_comm_init(void);
|
int data_comm_init(void);
|
||||||
|
|
||||||
|
|||||||
@@ -276,6 +276,8 @@ static void db_sqlite(void *parameter) {
|
|||||||
// 默认启用数据库
|
// 默认启用数据库
|
||||||
db_enabled = RT_TRUE;
|
db_enabled = RT_TRUE;
|
||||||
static rt_bool_t first_wait_done = RT_FALSE;
|
static rt_bool_t first_wait_done = RT_FALSE;
|
||||||
|
//连续链接次数
|
||||||
|
rt_int16_t op_link=0;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
// 接收命令
|
// 接收命令
|
||||||
@@ -284,12 +286,23 @@ static void db_sqlite(void *parameter) {
|
|||||||
if (first_wait_done)
|
if (first_wait_done)
|
||||||
{
|
{
|
||||||
first_wait_done = RT_FALSE;
|
first_wait_done = RT_FALSE;
|
||||||
//sqlite3_exec(g_db, "PRAGMA shrink_memory;", 0, 0, 0);
|
sqlite3_exec(g_db, "PRAGMA shrink_memory;", 0, 0, 0);
|
||||||
sqlite3_close(g_db);
|
sqlite3_close(g_db);
|
||||||
g_db=RT_NULL;
|
g_db=RT_NULL;
|
||||||
|
op_link=0;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}else{first_wait_done = RT_TRUE;}
|
}else{
|
||||||
|
if(op_link>1000)
|
||||||
|
{//连续操作数据库1000次后强制关闭防止内存占用
|
||||||
|
sqlite3_close(g_db);
|
||||||
|
g_db=RT_NULL;
|
||||||
|
op_link=0;
|
||||||
|
}
|
||||||
|
op_link++;
|
||||||
|
|
||||||
|
first_wait_done = RT_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
//检查排队超时
|
//检查排队超时
|
||||||
rt_tick_t now = rt_tick_get();
|
rt_tick_t now = rt_tick_get();
|
||||||
@@ -328,8 +341,8 @@ static void db_sqlite(void *parameter) {
|
|||||||
return_disabled(&cmd);
|
return_disabled(&cmd);
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
//限制缓存大小 (100KB)
|
//限制缓存大小 (KB)
|
||||||
//sqlite3_exec(g_db, "PRAGMA cache_size = -50;", 0, 0, 0);
|
sqlite3_exec(g_db, "PRAGMA cache_size = 8;", 0, 0, 0);
|
||||||
//设置临时存储为文件模式 (节省内存)
|
//设置临时存储为文件模式 (节省内存)
|
||||||
//sqlite3_exec(g_db, "PRAGMA temp_store = FILE;", 0, 0, 0);
|
//sqlite3_exec(g_db, "PRAGMA temp_store = FILE;", 0, 0, 0);
|
||||||
//rt_kprintf("Opened database: %s\n", DB_NAME);
|
//rt_kprintf("Opened database: %s\n", DB_NAME);
|
||||||
|
|||||||
Reference in new issue
Block a user