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.
193 lines
5.6 KiB
193 lines
5.6 KiB
5 years ago
|
|
||
|
/*****************************************************************************
|
||
|
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"
|
||
|
|
||
|
|
||
|
//NUM: 0 1 2 3 4
|
||
|
// Fx R1 R2 R3 ocr
|
||
|
// F+从机 R1 R2 R3 校验
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/// 实践中发现 如果收到多个以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
|
||
|
#ifdef D_use_uart_rx
|
||
|
void L1_s2b_PH1 (TS_Handle_PH1 *p)// reentrant
|
||
|
{
|
||
|
if (0 == p->head)
|
||
|
{
|
||
|
if (D_HETU_FX_fi == (p->reg&D_HETU_FX_H4))
|
||
|
{//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_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)
|
||
|
{
|
||
|
// 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
|
||
|
|
||
|
////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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
///FF FE 08 52 16 11 22 33 44 F5 9F 发送了一个modbus协议包(8个数据52 16 11 22 33 44 F5 9F )
|
||
|
/// 1 2 3 4 5 6 7 8
|
||
|
/// 0 1 2 3 4 5 6 7 8 9 10
|
||
|
/// 协议表达的意思为地址为0x52的从机, 功能码16 数据为11 22 33 44,校验码 F5 9F
|
||
|
|
||
|
void L1_s2b_PH3_init(TS_Handle_PH3 *p)
|
||
|
{
|
||
|
p->max = 0;
|
||
|
p->ok = 0;
|
||
|
p->num = 0;
|
||
|
p->head = 0;
|
||
|
p->head_0 = TS_PH3_FILTER0;
|
||
|
p->head_1 = TS_PH3_FILTER1;
|
||
|
}
|
||
|
|
||
|
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 = p->buf;
|
||
|
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)
|
||
|
{
|
||
|
p->ok = 1;//收到 命令结束,必须有个地方清0,否则无法再次接受报文
|
||
|
}
|
||
|
}
|
||
|
p->num = 0;
|
||
|
p->head = 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/******************************************************************************
|
||
|
** End Of File
|
||
|
******************************************************************************/
|
||
|
|