exfat支持修复。添加uart框架中控485端口功能完成
This commit is contained in:
1 parent
43f2057b44
commit
fd04893625
17 files changed
+1269
-648
No files matched your search
@@ -101,42 +101,44 @@ void pasre_DAT(const char *api, const char *json_str)
|
||||
cJSON_AddItemToObject(dat,"Groups",cJSON_CreateString(machine_ID));
|
||||
}
|
||||
else if (strcmp(api, "SC810") == 0)
|
||||
{//工单明细
|
||||
// === 解析整数变量 StepN ===
|
||||
GET_INT_VAR(StepN, root, "StepN", 0);
|
||||
// 字符串赋值
|
||||
GET_STRING(Work, root, "Work", sizeof(Work));
|
||||
GET_STRING(Dye, root, "Dye", sizeof(Dye));
|
||||
GET_STRING(StepID, root, "StepID", sizeof(StepID));
|
||||
GET_STRING(SIDS1, root, "SIDS1", sizeof(SIDS1));
|
||||
GET_STRING(SIDS2, root, "SIDS2", sizeof(SIDS2));
|
||||
GET_STRING(SIDS3, root, "SIDS3", sizeof(SIDS3));
|
||||
// 浮点数赋值
|
||||
GET_FLOAT_VAR(P1, root, "P1", 0.0f);
|
||||
GET_FLOAT_VAR(P2, root, "P2", 0.0f);
|
||||
GET_FLOAT_VAR(P3, root, "P3", 0.0f);
|
||||
GET_FLOAT_VAR(P4, root, "P4", 0.0f);
|
||||
GET_FLOAT_VAR(P5, root, "P5", 0.0f);
|
||||
GET_FLOAT_VAR(P1S1, root, "P1S1", 0.0f);
|
||||
GET_FLOAT_VAR(P2S1, root, "P2S1", 0.0f);
|
||||
GET_FLOAT_VAR(P3S1, root, "P3S1", 0.0f);
|
||||
GET_FLOAT_VAR(P4S1, root, "P4S1", 0.0f);
|
||||
GET_FLOAT_VAR(P5S1, root, "P5S1", 0.0f);
|
||||
GET_FLOAT_VAR(P1S2, root, "P1S2", 0.0f);
|
||||
GET_FLOAT_VAR(P2S2, root, "P2S2", 0.0f);
|
||||
GET_FLOAT_VAR(P3S2, root, "P3S2", 0.0f);
|
||||
GET_FLOAT_VAR(P4S2, root, "P4S2", 0.0f);
|
||||
GET_FLOAT_VAR(P5S2, root, "P5S2", 0.0f);
|
||||
GET_FLOAT_VAR(P1S3, root, "P1S3", 0.0f);
|
||||
GET_FLOAT_VAR(P2S3, root, "P2S3", 0.0f);
|
||||
GET_FLOAT_VAR(P3S3, root, "P3S3", 0.0f);
|
||||
GET_FLOAT_VAR(P4S3, root, "P4S3", 0.0f);
|
||||
GET_FLOAT_VAR(P5S3, root, "P5S3", 0.0f);
|
||||
cJSON_AddItemToObject(dat,"Work",cJSON_CreateString(Work));
|
||||
cJSON_AddItemToObject(dat,"ReDye",cJSON_CreateNumber(Redye));
|
||||
{
|
||||
}
|
||||
else if (strcmp(api, "SC811") == 0) {
|
||||
printf("Processing:SC811\n");
|
||||
else if (strcmp(api, "SC811") == 0)
|
||||
{//工单明细
|
||||
// === 解析整数变量 StepN ===
|
||||
GET_INT_VAR(StepN, root, "StepN", 0);
|
||||
// 字符串赋值
|
||||
GET_STRING(Work, root, "Work", sizeof(Work));
|
||||
GET_STRING(Dye, root, "Dye", sizeof(Dye));
|
||||
GET_STRING(StepID, root, "StepID", sizeof(StepID));
|
||||
GET_STRING(SIDS1, root, "SIDS1", sizeof(SIDS1));
|
||||
GET_STRING(SIDS2, root, "SIDS2", sizeof(SIDS2));
|
||||
GET_STRING(SIDS3, root, "SIDS3", sizeof(SIDS3));
|
||||
// 浮点数赋值
|
||||
GET_FLOAT_VAR(P1, root, "P1", 0.0f);
|
||||
GET_FLOAT_VAR(P2, root, "P2", 0.0f);
|
||||
GET_FLOAT_VAR(P3, root, "P3", 0.0f);
|
||||
GET_FLOAT_VAR(P4, root, "P4", 0.0f);
|
||||
GET_FLOAT_VAR(P5, root, "P5", 0.0f);
|
||||
GET_FLOAT_VAR(P1S1, root, "P1S1", 0.0f);
|
||||
GET_FLOAT_VAR(P2S1, root, "P2S1", 0.0f);
|
||||
GET_FLOAT_VAR(P3S1, root, "P3S1", 0.0f);
|
||||
GET_FLOAT_VAR(P4S1, root, "P4S1", 0.0f);
|
||||
GET_FLOAT_VAR(P5S1, root, "P5S1", 0.0f);
|
||||
GET_FLOAT_VAR(P1S2, root, "P1S2", 0.0f);
|
||||
GET_FLOAT_VAR(P2S2, root, "P2S2", 0.0f);
|
||||
GET_FLOAT_VAR(P3S2, root, "P3S2", 0.0f);
|
||||
GET_FLOAT_VAR(P4S2, root, "P4S2", 0.0f);
|
||||
GET_FLOAT_VAR(P5S2, root, "P5S2", 0.0f);
|
||||
GET_FLOAT_VAR(P1S3, root, "P1S3", 0.0f);
|
||||
GET_FLOAT_VAR(P2S3, root, "P2S3", 0.0f);
|
||||
GET_FLOAT_VAR(P3S3, root, "P3S3", 0.0f);
|
||||
GET_FLOAT_VAR(P4S3, root, "P4S3", 0.0f);
|
||||
GET_FLOAT_VAR(P5S3, root, "P5S3", 0.0f);
|
||||
|
||||
cJSON_AddItemToObject(dat,"Work",cJSON_CreateString(Work));
|
||||
cJSON_AddItemToObject(dat,"ReDye",cJSON_CreateNumber(Redye));
|
||||
cJSON_AddItemToObject(dat,"StepN",cJSON_CreateNumber(StepN));
|
||||
}
|
||||
else if (strcmp(api, "SC812") == 0) {
|
||||
printf("Processing:SC812\n");
|
||||
@@ -295,18 +297,22 @@ void proc_thread_entry(void *parameter)
|
||||
if (!req) continue;
|
||||
memcpy(DATA_api,req->input,5);
|
||||
extract_between(req->input, "[", "]", machine_ID, sizeof(machine_ID));
|
||||
|
||||
|
||||
char *p=strstr(req->input,"]");
|
||||
if(!p){cjson_falg=0;}
|
||||
else {
|
||||
p += strlen("]");
|
||||
}
|
||||
|
||||
//处理指令分析
|
||||
pasre_DAT(DATA_api,p);
|
||||
// 回复
|
||||
int16_t total_len = strlen(machine_ID) + strlen(json_buffer) + 8;
|
||||
char t_buffer[total_len];
|
||||
snprintf(t_buffer,total_len,"%s[%s]%s",DATA_api,machine_ID,json_buffer);
|
||||
snprintf(t_buffer,total_len,"%s[%s]%s\n",DATA_api,machine_ID,json_buffer);
|
||||
req->output_len = rt_snprintf(req->output, sizeof(req->output),t_buffer);
|
||||
|
||||
// 通知 UART 线程可以发送了
|
||||
if (req->sem)
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#define APPLICATIONS_DATA_COMM_H_
|
||||
|
||||
// 最大输入/输出长度
|
||||
#define MAX_INPUT_LEN 1024
|
||||
#define MAX_INPUT_LEN 512
|
||||
#define MAX_OUTPUT_LEN 1024*3
|
||||
|
||||
// 请求结构体
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
#define SAMPLE_UART_NAME "uart2"
|
||||
#define DMA_RX_BUF_SIZE 512
|
||||
#define RINGBUF_SIZE 4096
|
||||
#define RINGBUF_SIZE 1024
|
||||
|
||||
static rt_device_t serial;
|
||||
|
||||
@@ -74,21 +74,14 @@ void uart_thread_entry(void *parameter)
|
||||
{
|
||||
// 处理接收到的数据(提取完整行)
|
||||
while ((len = get_line(input, sizeof(input))) > 0)
|
||||
{ // 调试:打印每个字节
|
||||
/* rt_kprintf("Len=%d, Data: ", len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
rt_kprintf("[%02X '%c'] ", input[i],
|
||||
(input[i] >= 32 && input[i] < 127) ? input[i] : '.');
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
|
||||
rt_kprintf("Received: [%.*s]\n", len, input);*/
|
||||
{
|
||||
// 动态分配请求结构体(见之前完整代码)
|
||||
struct proc_request *req = rt_malloc(sizeof(struct proc_request));
|
||||
if (!req) continue;
|
||||
|
||||
rt_strncpy(req->input, input, sizeof(req->input));
|
||||
req->input_len = len;
|
||||
//req->input[len]='\0';
|
||||
|
||||
req->sem = rt_malloc(sizeof(struct rt_semaphore));
|
||||
if (!req->sem) {
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2025-10-25 Administrator the first version
|
||||
*/
|
||||
# include <rtthread.h>
|
||||
# include <rtdevice.h>
|
||||
# include <board.h>
|
||||
# include <stdio.h>
|
||||
#include "uart_framework.h"
|
||||
#include "data_comm.h"
|
||||
|
||||
#define LOG_TAG "uart.rx"
|
||||
#define LOG_LVL LOG_LVL_DBG
|
||||
//#include <ulog.h>
|
||||
|
||||
#define UART_NAME "uart2"
|
||||
#define UART_BAUD_RATE BAUD_RATE_115200
|
||||
|
||||
#define RS485RD GET_PIN(A,10)
|
||||
#define RS485_RX() rt_pin_write(RS485RD, 0)
|
||||
#define RS485_TX() rt_pin_write(RS485RD, 1)
|
||||
|
||||
#define RECV_BUF_SIZE (512)
|
||||
#define FRAME_TIMEOUT_MS 20
|
||||
#define SEND_INTERVAL_MS 0
|
||||
#define REQUEST_TIMEOUT_MS 250
|
||||
|
||||
static uart_framework_t uf = RT_NULL;
|
||||
|
||||
static void rs485_set_tx(void)
|
||||
{
|
||||
RS485_TX();
|
||||
rt_thread_mdelay(1);
|
||||
}
|
||||
|
||||
static void rs485_set_rx(void)
|
||||
{
|
||||
rt_thread_mdelay(1);
|
||||
RS485_RX();
|
||||
}
|
||||
|
||||
static void rs485_gpio_init(void)
|
||||
{
|
||||
rt_pin_mode(RS485RD, PIN_MODE_OUTPUT);
|
||||
rs485_set_rx();
|
||||
}
|
||||
|
||||
static rt_err_t uart_rs485_init(void)
|
||||
{
|
||||
rt_device_t serial_device = rt_device_find(UART_NAME);
|
||||
if (serial_device == RT_NULL)
|
||||
{
|
||||
rt_kprintf("not find the serial device:%s!", UART_NAME);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* 修改串口配置 */
|
||||
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
||||
config.baud_rate = UART_BAUD_RATE;
|
||||
config.bufsz = RECV_BUF_SIZE; // 接收缓冲区大小
|
||||
rt_device_control(serial_device, RT_DEVICE_CTRL_CONFIG, &config);
|
||||
|
||||
rs485_gpio_init();
|
||||
struct uart_framework_cfg cfg = { .uart_name = UART_NAME, .max_frame_size = RECV_BUF_SIZE, .frame_interval_ms =
|
||||
FRAME_TIMEOUT_MS, .send_interval_ms = SEND_INTERVAL_MS, .rs485_txd = rs485_set_tx, .rs485_rxd = rs485_set_rx };
|
||||
//如果不是485,则配置rs485_txd和rs485_rxd为RT_NULL
|
||||
|
||||
uf = uart_framework_create(&cfg);
|
||||
if (uf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("SCCM link create failed on %s\n", UART_NAME);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("SCCM link create success on %s!\n", UART_NAME);
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
|
||||
static rt_err_t frame_handler(rt_uint8_t *data, rt_size_t size)
|
||||
{
|
||||
//解析数据
|
||||
rt_err_t res;
|
||||
struct proc_request *req = rt_malloc(sizeof(struct proc_request));
|
||||
if (!req){ return RT_ERROR;}
|
||||
rt_strncpy(req->input, (const char *)data, sizeof(req->input));
|
||||
req->input_len = size;
|
||||
req->sem = rt_malloc(sizeof(struct rt_semaphore));
|
||||
|
||||
if (!req->sem) {
|
||||
rt_free(req);
|
||||
return RT_ERROR;
|
||||
}
|
||||
|
||||
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_sem_take(req->sem, RT_WAITING_FOREVER) == RT_EOK)
|
||||
{
|
||||
res= uart_framework_send(uf, (rt_uint8_t *)req->output, req->output_len);
|
||||
}
|
||||
}
|
||||
// 清理
|
||||
rt_sem_detach(req->sem); // 或 rt_sem_delete
|
||||
rt_free(req->sem);
|
||||
rt_free(req);
|
||||
|
||||
//这里直接返回接收到的数据
|
||||
// rt_uint8_t *frame_data = data;
|
||||
// rt_uint16_t frame_len = size;
|
||||
// rt_kprintf("rx_buf", 16, frame_data, frame_len);
|
||||
// rt_err_t res = uart_framework_send(uf, frame_data, frame_len);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static void rs485_rx_handler(void *params)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
if(uart_framework_receive(uf, RT_WAITING_FOREVER, RT_NULL, RT_NULL, 0) == RT_EOK)
|
||||
{
|
||||
frame_handler(uf->rx_buf, uf->rx_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int app_uart_rs485_startup(void)
|
||||
{
|
||||
rt_err_t rst;
|
||||
rst = uart_rs485_init();
|
||||
if (rst != RT_EOK)
|
||||
return rst;
|
||||
|
||||
rt_thread_t t = rt_thread_create("rs485rx", rs485_rx_handler, RT_NULL, 1024 * 4, 12, 20);
|
||||
if (t == RT_NULL)
|
||||
return -RT_ENOMEM;
|
||||
|
||||
rst = rt_thread_startup(t);
|
||||
|
||||
return rst;
|
||||
}
|
||||
//INIT_APP_EXPORT(app_uart_rs485_startup);
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2025-10-25 Administrator the first version
|
||||
*/
|
||||
#ifndef APPLICATIONS_DATA_UART_RS485_H_
|
||||
#define APPLICATIONS_DATA_UART_RS485_H_
|
||||
|
||||
int app_uart_rs485_startup(void);
|
||||
|
||||
#endif /* APPLICATIONS_DATA_UART_RS485_H_ */
|
||||
Reference in new issue
Block a user