From 649f1f8711ba63c2e39e65e9803ba3f8dd2e4960 Mon Sep 17 00:00:00 2001 From: "mr.zhangsan" Date: Tue, 30 Apr 2024 21:42:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=B2=E5=8F=A34=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/msp/UART4.C | 23 +++++++++++----- source/msp/UART4.h | 3 +-- source/tpc/debug.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++ source/tpc/debug.h | 1 + 4 files changed, 84 insertions(+), 8 deletions(-) diff --git a/source/msp/UART4.C b/source/msp/UART4.C index 61ff1b8..1883e6e 100644 --- a/source/msp/UART4.C +++ b/source/msp/UART4.C @@ -55,11 +55,13 @@ msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面 ----------------------------------------------------------------------------------------- ********************************************************************************/ #include "uart4.h" +#include "../tpc/debug.h" //发送缓冲区 static volatile Ts_uart_send_buf idata ts_uart_send_buf; //接收缓冲区最多存放16个字符,这个值尽量小,但要大于实际处理协议的缓冲区大小 -static U8 uart4_recv_buf[16]; +#define RECV_BUF_SIZE 32 +static U8 uart4_recv_buf[RECV_BUF_SIZE]; Ts_uart_recv_buf ts_uart4_recv_buf; //#define FOSC 11059200L //系统频率 @@ -95,16 +97,24 @@ void L0_uart4_init(void)//115200bps@11.0592MHz void L0_uart4_buf_init(void) { + //初始化协议发送缓冲区 ts_uart[uNum4].p = &ts_uart_send_buf; ts_uart[uNum4].p->now = 0; ts_uart[uNum4].p->ok = D_ready; + + //初始化协议接收缓冲区 ts_uart[uNum4].t = &ts_uart4_recv_buf; ts_uart[uNum4].t->buf = uart4_recv_buf; - ts_uart[uNum4].t->head_0 = D_CMD_Filter1_ff; - ts_uart[uNum4].t->head_1 = D_CMD_Filter2_fe; ts_uart[uNum4].t->head = 0; ts_uart[uNum4].t->ok = 0; - ts_uart[uNum4].tp_handler = L1_s2b_PH3; + + //串口协议解析专用字段 + ts_uart[uNum4].t->head_0 = 0xAA; + ts_uart[uNum4].t->head_1 = 0x55; + ts_uart[uNum4].t->maxnum = RECV_BUF_SIZE; + ts_uart[uNum4].tp_handler = L1_s2b_485; + + //串口初始化 L0_uart4_init(); D_uart4_ES_INT(1); //打开串口中断 @@ -134,7 +144,9 @@ void INTERRUPT_uart4(void) D_SERVE_uart4// using 2 { L0_uart4_IntRIClear(); //清除接收中断标志 ts_uart[uNum4].t->reg = L0_uartN_get(uNum4); - ts_uart[uNum4].tp_handler(ts_uart[uNum4].t); + if(ts_uart[uNum4].tp_handler != NULL){ + ts_uart[uNum4].tp_handler(ts_uart[uNum4].t); + } } if(L0_uart4_IntTI()) //如果是U0发送中断 { @@ -152,7 +164,6 @@ void INTERRUPT_uart4(void) D_SERVE_uart4// using 2 #if (D_UART4_485_TYPE != TYPE_485_NONE) D_UART4_485_RX() //切换到接收状态 #endif - } } //NOP(); NOP(); NOP(); diff --git a/source/msp/UART4.h b/source/msp/UART4.h index b20adc3..3ecda9f 100644 --- a/source/msp/UART4.h +++ b/source/msp/UART4.h @@ -54,8 +54,7 @@ msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面 #define _uart4_H #include "../bsp/bsp_config.h" -#include "../tpc/tpc_ccmodbus.h" -#include "uartN.h" +#include "uart_x.h" #define uNum4 3 diff --git a/source/tpc/debug.c b/source/tpc/debug.c index d37ba08..fed2fb7 100644 --- a/source/tpc/debug.c +++ b/source/tpc/debug.c @@ -166,6 +166,71 @@ void L1_s2b_rfid (Ts_uart_recv_buf *p) //reentrant } } +//485协议:AA 55 00 04 01 10 00 00 15 +//帧头:AA 55 +//数据长度:00 04,后续数据的长度,不包括最后的ocr +//SlaveId:01 +//Command: 10轮询读取 11设置参数 +//数据:00 00 +//校验:15 从协议长度到数据字段 [00 04 01 10 00 00] 的所有字节的累加和 +void L1_s2b_485 (Ts_uart_recv_buf *p) //reentrant +{ + if (0 == p->head) + { + p->buf[p->index++] = p->reg; + if(p->index == 2){ + if (p->head_0 == p->buf[0] && p->head_1 == p->buf[1]) + { + p->head = 1; + p->index = 1; + p->num = p->maxnum; + p->ocr = 0; + } + else + { + p->head = 0; + p->ok = 0; + p->index = 0; + } + } + } + else + { + p->buf[++p->index] = p->reg; + if(p->index == 3) + { + //2个字节协议头 + 2字节数据长度 + 数据 + 1ocr + p->num = 4 + 1 + ((p->buf[2] << 8) | (p->buf[3])); + if(p->num > p->maxnum){ + //error + p->head = 0; + p->ok = 0; + p->index = 0; + return; + } + } + if(p->index < p->num - 1) + { + //计算OCR + p->ocr += p->reg; + } + else if(p->index == p->num - 1) + { + if(p->ocr == p->buf[p->num-1]) + { + if (p->ok != 1) + { + //命令结束,必须有个地方清0,否则无法再次接受报文 + p->ok = 1; + } + } + p->head = 0; + p->index = 0; + } + } +} + + /****************************************************************************** diff --git a/source/tpc/debug.h b/source/tpc/debug.h index 309ece1..300cc51 100644 --- a/source/tpc/debug.h +++ b/source/tpc/debug.h @@ -68,6 +68,7 @@ typedef struct _tpc_rfid_ extern void L1_s2b_debug(Ts_uart_recv_buf *p); extern void L1_s2b_nfc (Ts_uart_recv_buf *p); extern void L1_s2b_rfid (Ts_uart_recv_buf *p); +extern void L1_s2b_485 (Ts_uart_recv_buf *p); extern void L1_s2b_PH1 (Ts_uart_recv_buf *p);// reentrant; #endif /* end __TPC_DEBUG_H_ */