平车主控板(运维板)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

582 lines
15 KiB

/*****************************************************************************
update by cc @201501101001
针对多串口 和 单一串口 有区别 每个串口是独立的还是分开的有讲究 程序是复杂的还是软件应用简单是
个需要平衡的事情.
uartcom/uartlib.c:
公用的函数 和硬件无关
放置串行模式(串口等其他通讯总线类的输出)输出的函数,
一些覆盖模式输出的(lcd等固屏输出的)的也可使用
void Lc_print(void (*L0pf_send_uc)(char ww), char *dat,...)
-----------------------------------------------------------------------------------------
uartcom/uartcom0
和uart相关的通讯协议 com + n
为了适应不同的通讯协议需要不同的uart口来对应 和应用相关
typedef struct _ts_lcm_pro_; 应用协议包的定义? LCM的协议------------
L3_UARTcom0_exp_protocol 解析应用协议
-----------------------------------------------------------------------------------------
uartcom/uprotocol: 主要是为 uartcom + n服务的 驱动层到应用层缓存的过度
公用的串口通讯定义
struct _s_protocol_ 的公共协议包(关键的结构体)的声明------struct _s_protocol_
void L1_uart_2buf(struct _s_protocol_ *p)串行数据保存到缓冲中
--------------------------------------------------------------------------------------------
msp/uartx.c 底层代码 和cpu相关
L0_UART0_Init
UART0_IRQHandler
L0_Usend_uc----------s_at0
-----------------------------------------------------------------------------------------
********************************************************************************/
#include "tpc_uart.h"
#include "../clib/clib.h"
//NUM: 0 1 2 3 4
// Fx R1 R2 R3 ocr
// F+从机 R1 R2 R3 校验
#if 1 //相关功能移动到tpc_fx.c
/// 实践中发现 如果收到多个以0d0a结束的短协议时,如果
/// 协议之间间隔时间太短,ok处理不及时 会出现丢失协议的
/// 的情况,所以 对于短暂的多个协议 应该有一定容量的缓冲
/// 保留 ,同时 处理完协议后,应该清除接收缓冲,否则缓冲
/// 会在自身满了后自动清除
//_s_HRTU_P_rf_
/// _s_HRTU_Pfx_
/// fx 11 22 33 oc -- oc = 11+ 22+33
//buf 0 1 2 3 [4]
//
//// protocol hex 1 serial to buff
//fd 01 02 03 06 fd为头 010203为数据 06为数据校验和(01+02+03)
void L1_s2b_PH1 (struct _s_PH1_FX_ *p)// reentrant
{// p->ok = 1;
#if 1
p->cashe[1] = p->cashe[0];
p->cashe[0] = p->reg;//
if (0 == p->head)
{
if ((p->cashe[1] == D_HETU_FX_fi_0)&&(p->cashe[0] == D_HETU_FX_fi_1))
{
p->head = 1;
p->num = 0;
p->buf[0] = p->reg;
}
}else
{
p->num ++;
p->buf[p->num] = p->reg;
if(p->num >= D_HETU_FX_buf_max) // [4]
{
p->ocr = p->buf[1];
p->ocr += p->buf[2];
p->ocr += p->buf[3];
if(p->ocr == p->buf[D_HETU_FX_buf_max])
{
if (p->ok != 1)
{
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
}
}
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
//p->max = 0;
}
}
#endif
}
#endif
///FF FE 08 52 16 11 22 33 44 F5 9F 发送了一个modbus协议包(8个数据52 16 11 22 33 44 F5 9F )
/// 0 1 2 3 4 5 6 7 8 9 10
/// 协议表达的意思为地址为0x52的从机, 功能码16 数据为11 22 33 44,校验码 F5 9F
void L1_s2b_PH3(TS_Handle_PH3 *p)// reentrant
{
p->cashe[1] = p->cashe[0];
p->cashe[0] = p->reg;//
if (0 == p->head)
{
if ((p->cashe[1] == p->head_0)&&(p->cashe[0] == p->head_1))
{
p->head = 1;
p->max = D_s_PH3_ccmodbus_max - 1;
p->sp = (unsigned char *)&(p->__ts_ccmodbus);
p->sp[0] = p->head_0;
p->sp[1] = p->head_1;
p->num = 1;
}
}else
{
p->num ++;
p->sp[p->num] = p->cashe[0];
if(p->num == 2)//数量 第3个数据
{
p->max = p->reg + 2;//
if(p->max >= D_s_PH3_ccmodbus_max)
{
p->max = D_s_PH3_ccmodbus_max - 1;
p->sp[p->num] = p->max - 2;
}
}
if(p->num >= p->max)
{
//p->ts_ccmodbus.crc = (p->sp[p->num-1] | p->sp[p->num]) & 0xFFFF;
if(checkCRC16(&p->__ts_ccmodbus))
{
if (p->ok != 1)
{
byte_copy_uc((U8*)&p->ts_ccmodbus,(U8*)&p->__ts_ccmodbus,p->max + 1);
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
}
}
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
//p->max = 0;
}
}
}
#if 1
void L1_a9_PH3(TS_Handle_PH3 *p)// reentrant
{
p->cashe[1] = p->cashe[0];
p->cashe[0] = p->reg;//
if (0 == p->head) //等待那边处理完毕
{
if ((p->cashe[1] == p->head_0)&&(p->cashe[0] == p->head_1))
{
p->head = 1;
p->max = D_s_PH3_ccmodbus_max - 1;
p->sp = (unsigned char *)&(p->__ts_ccmodbus);
p->sp[0] = p->head_0;
p->sp[1] = p->head_1;
p->num = 1;
p->len_retry = 0;
}
}else
{
p->num ++;
p->sp[p->num] = p->cashe[0];
if(p->num == 2)//数量 第3个数据
{
if(p->cashe[0] == 0)
{
if(p->len_retry == 0) //丢弃本字节,探测下一个字节作为长度,以兼容:长度为0E和00 0E的协议
{
p->len_retry = 1;
p->num--;
return;
}
}
p->max = p->cashe[0] + 2;//
if((p->max > D_s_PH3_ccmodbus_max) || (p->max < D_s_PH3_ccmodbus_min)) //无效协议
{
p->head = 0;
return;
}
}
if(p->num >= p->max)
{
//p->ts_ccmodbus.crc = (p->sp[p->num-1] | p->sp[p->num]) & 0xFFFF;
if(checkCRC16(&p->__ts_ccmodbus))
{
if (p->ok != 1)
{
byte_copy_uc((U8*)&p->ts_ccmodbus,(U8*)&p->__ts_ccmodbus,p->max + 1);
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
}
}
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
//p->max = 0;
}
}
}
#else
void L1_a9_PH3(TS_Handle_PH3 *p)// reentrant
{
p->cashe[1] = p->cashe[0];
p->cashe[0] = p->reg;//
if (0 == p->head) //等待那边处理完毕
{
if ((p->cashe[1] == p->head_0)&&(p->cashe[0] == p->head_1))
{
p->head = 1;
p->max = D_s_PH3_ccmodbus_max - 1;
p->sp = (unsigned char *)&(p->__ts_ccmodbus);
p->sp[0] = p->head_0;
p->sp[1] = p->head_1;
p->num = 1;
}
}else
{
p->num ++;
p->sp[p->num] = p->cashe[0];
if(p->num == p->lenIndex)//数量 第3个数据
{
if(p->cashe[0] == 0) //过滤长度为0的协议
{
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
return;
}
p->max = p->reg + 2;//
if(p->max >= D_s_PH3_ccmodbus_max)
{
p->max = D_s_PH3_ccmodbus_max - 1;
p->sp[p->num] = p->max - 2;
}
}
if(p->num >= p->max)
{
//p->ts_ccmodbus.crc = (p->sp[p->num-1] | p->sp[p->num]) & 0xFFFF;
if(checkCRC16(&p->__ts_ccmodbus))
{
if (p->ok != 1)
{
byte_copy_uc((U8*)&p->ts_ccmodbus,(U8*)&p->__ts_ccmodbus,p->max + 1);
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
}
}
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
//p->max = 0;
}
}
}
#endif
///FF FE 08 52 16 11 22 33 44 F5 9F 发送了一个modbus协议包(8个数据52 16 11 22 33 44 F5 9F )
/// 0 1 2 3 4 5 6 7 8 9 10
/// 协议表达的意思为地址为0x52的从机, 功能码16 数据为11 22 33 44,校验码 F5 9F
void L1_s2b_PH3_1(TS_Handle_PH3 *p)// reentrant
{
p->cashe[1] = p->cashe[0];
p->cashe[0] = p->reg;//
if (0 == p->head)
{
if ((p->cashe[1] == p->head_0)&&(p->cashe[0] == p->head_1))
{
p->head = 1;
p->max = D_s_PH3_ccmodbus_max - 1;
p->sp = (unsigned char *)&(p->ts_ccmodbus);
p->sp[0] = p->head_0;
p->sp[1] = p->head_1;
p->num = 1;
}
}else
{
p->num ++;
p->sp[p->num] = p->cashe[0];
if(p->num == 2)//数量 第3个数据
{
p->max = p->reg + 2;//
if(p->max >= D_s_PH3_ccmodbus_max)
{
p->max = D_s_PH3_ccmodbus_max - 1;
}
}
if(p->num >= p->max)
{
//p->ts_ccmodbus.crc = (p->sp[p->num-1] | p->sp[p->num]) & 0xFFFF;
if(/*crc()==p->ts_ccmodbus.crc*/ 1)
{
if (p->ok != 1)
{
// D_led_D2_ON();
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
// D_led_D2_OFF();
}
}
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
//p->max = 0;
}
}
}
void L1_a9_PH3_1(TS_Handle_PH3 *p)// reentrant
{
p->cashe[1] = p->cashe[0];
p->cashe[0] = p->reg;//
if (0 == p->head) //等待那边处理完毕
{
if ((p->cashe[1] == p->head_0)&&(p->cashe[0] == p->head_1))
{
p->head = 1;
p->max = D_s_PH3_ccmodbus_max - 1;
p->sp = (unsigned char *)&(p->ts_ccmodbus);
p->sp[0] = p->head_0;
p->sp[1] = p->head_1;
p->num = 1;
}
}else
{
p->num ++;
p->sp[p->num] = p->cashe[0];
if(p->num == 2)//数量 第3个数据
{
if(p->reg == 0) //过滤长度为0的协议
{
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
return;
}
p->max = p->reg + 2;//
if(p->max >= D_s_PH3_ccmodbus_max)
{
p->max = D_s_PH3_ccmodbus_max - 1;
}
}
if(p->num >= p->max)
{
//p->ts_ccmodbus.crc = (p->sp[p->num-1] | p->sp[p->num]) & 0xFFFF;
if(/*crc()==p->ts_ccmodbus.crc*/ 1)
{
if (p->ok != 1)
{
// D_led_D2_ON();
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
// D_led_D2_OFF();
}
}
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
//p->max = 0;
}
}
}
#if 0 //相关功能移动到 tpc_ffee.c
//NUM: 0 1 2 3 4 5 6 7
// FF FE 10 02 0C 51 03
// cashe[1] cashe[0] 地址 数量 校验 R1 从机 R2 命令 R3 命令
// FF FE 10 02 0C 51 03
///S1 >> PC FF FE 51 03 9C 12 34 56 地址为0x51的从机, 向PC发送了3个数据0x12,0x34,0x56
/// 接收以filter1 filter2为开始的的16进制数据的协议 类薣? FF FE 01 02 03 xx 等
/// 数据中包含发送数据的数量(也可能为定长)
/// 测试时调试数据要在16进制模式下 协议具体内容见 对应的结构体
/// 在串口中断中调用 uart hardware to buffer 串口到缓存中
// RTU with head 有标头的RTU模式
void L1_s2b_PH2(struct _s_PH2_FFFE_ *p)// reentrant
{
p->cashe[1] = p->cashe[0];
p->cashe[0] = p->reg;//
if (0 == p->head)
{
if (((p->cashe[1] == p->head_0)&&(p->cashe[0] == p->head_1)))
{
p->head = 1;
p->max = D_s_SSTR_0D0A_len - 1;
p->sp[0] = p->head_0;
p->sp[1] = p->head_1;
p->num = 1;
}
}else
{
p->num ++;
p->sp[p->num] = p->cashe[0];
if(p->num == 3)//数量 第4个数据 下标为3
{
p->max = p->reg + 3 ;//
if(p->max >= D_s_SSTR_0D0A_len)
{
p->max = D_s_SSTR_0D0A_len - 1;
}
}
if(p->num > p->max)
{
if (p->ok != 1)
{
// D_led_D2_ON();
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
// D_led_D2_OFF();
}
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
//p->max = 0;
}
}
}
#endif
#if 0 //相关功能已在tpc_0d0a.c中实现
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//L1_uart_2strbuf(&s_uart0_rec);s_uart0_rec.ok = 2
/// 接收以od oa为结束的标准的assci码的协议 类似 send xx (回车换行)测试时注意以回车换行
/// 为结束标志,也就是发送的数据后面 必须有回车换行 :0x0d 0x0a
void L1_s2b_PC1(struct _s_PC1_0D0A_ *p)// reentrant
{
//p->cashe[2] = p->cashe[1];
p->cashe[1] = p->cashe[0];
p->cashe[0] = p->reg;//
p->buf[p->num] = p->cashe[0];
p->num ++;
if(p->num > D_s_SSTR_0D0A_len)
{
p->num = 0;
}
if ((p->cashe[0] == 0x0a)&&(p->cashe[1] == 0x0d))// 0d0a
{ // 0a 0d
p->max = p->num;
p->buf[p->max] = 0;
if(0 == p->ok)
{
// for(;0 != p->num;p->num --)
// {
// p->array[p->num] = p->buf[p->num];
// }
// p->array[0] = p->buf[0];
}
if(2!= p->ok)// ok = 2 代表关闭串口
{
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
}
p->num = 0;
}
}
#endif
/// 在串口中断中调用 uart hardware to buffer 串口到缓存中
void L1_s2b_PC2(struct _s_protocol_ *p)
{
// unsigned int i;
//p->cashe[3] = p->cashe[2];
//p->cashe[2] = p->cashe[1];
p->cashe[1] = p->cashe[0];
p->cashe[0] = p->reg;//
//p->cashe[0] = SBUF;//
//D_led_debug_REV();
// p->ok = 1;
// D_led_D2_ON();D_led_D2_OFF();D_led_D2_ON();
if (p->head||((p->cashe[0] == p->filter1)&&(p->cashe[1] == p->filter2)))
{ // p->ok = 1;//收到 标志置1 ; 55 aa 44 55
p->head = 1;
p->num ++;
p->buf[p->num] = p->cashe[0];
//D_led_D2_OFF();
if(p->num > 8)
{// protect
p->head = 0;p->ok = 0;p->num = 0;
{
//while(9)
{
// D_led_D1_ON();D_led_D1_REV();D_led_D1_ON(); D_led_D2_ON();D_led_D2_REV();D_led_D2_ON();
}
}
}else
{
if (p->num >= (p->buf[2]+2))
{// aa 55 (03) 11 22 33
if (p->ok != 1)
{
// D_led_D2_ON();
p->buf[0] = p->filter2;
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
// D_led_D2_OFF();
}
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
}
}
}
else {}
}
int checkCRC16(TS_PH3_ccmodbus *pCcmodbus)
{
U8 crc[2],dataCrc[2],num;
num = pCcmodbus->num;
crc16(crc,&(pCcmodbus->addr),num - 2);
dataCrc[1] = (pCcmodbus->buf[num - 4]) & 0xFF;
dataCrc[0] = (pCcmodbus->buf[num - 3]) & 0xFF;
if(crc[1] == dataCrc[1] && crc[0] == dataCrc[0])
return 1;
return 0;
}
////485
//// 带地址判定的
void L1_s2b_PH1A (TS_Handle_PH1A *p)// reentrant
{
if (0 == p->head)
{
if (D_HETU_FXA == p->reg)
{//p->ok = 1;p->head = 0;
p->head = 1;
p->num = 0;
//p->ocx = 0;
p->buf[p->num]=p->reg;
}
}else
{
p->num ++;
p->buf[p->num] = p->reg;
if(p->num >= D_HETU_FXA_buf_max - 1) // [4]
{
///p->ocr = p->buf[1]; p->ocr += p->buf[2];p->ocr += p->buf[3];
///if(p->ocr == p->buf[D_HETU_FX_buf_max])
{
if (p->add == p->buf[1])///地址不匹配
{
// D_led_D2_ON();
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
// D_led_D2_OFF();
}
// p->ok = 1;
}
p->num = 0; //放在if (p->ok != 1) 外
p->head = 0; //放在if (p->ok != 1) 外
//p->max = 0;
}
}
}
void L1_pack_PH1A(TS_PH1A_DAT *ph1A,U8 dstId,U8 oper)
{
ph1A->filter = D_HETU_FXA;
ph1A->id = dstId;
ph1A->oper = oper;
ph1A->xaxis[0] = 0x22;
ph1A->xaxis[1] = 0x33;
ph1A->yaxis[0] = 0x44;
ph1A->yaxis[1] = 0x55;
ph1A->zaxis[0] = 0x66;
ph1A->zaxis[1] = 0x77;
}
/******************************************************************************
** End Of File
******************************************************************************/