feat: 添加通信帧解析超时机制
This commit is contained in:
@@ -6,10 +6,18 @@
|
||||
lwrb_t lwrb_struct;
|
||||
uint8_t buffer_rb[BY_TF_PARSE_BUFFER_SIZE];
|
||||
uint8_t buffer_out;
|
||||
uint8_t listern_flag;
|
||||
uint16_t listern_timeout;
|
||||
uint16_t listern_timevia;
|
||||
by_tf_parse_frame_t frame_now;
|
||||
by_tf_parse_success_handle_func parse_success_handle;
|
||||
|
||||
void by_tiny_frame_parse_init(void)
|
||||
{
|
||||
#if defined(BY_TF_DEVICE_MASTER)
|
||||
listern_timeout = BY_TF_PARSE_TIMEOUT;
|
||||
#endif
|
||||
|
||||
/** 初始化环形缓冲区 **/
|
||||
lwrb_init(&lwrb_struct, buffer_rb, 40);
|
||||
}
|
||||
@@ -62,23 +70,63 @@ uint8_t by_tiny_frame_parse_listening(by_tf_parse_frame_t *frame_s, const uint8_
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief by_tf_parse 串口回调函数,在对应串口中断函数中调用
|
||||
*
|
||||
* @param buff
|
||||
*/
|
||||
void by_tiny_frame_parse_uart_handle(uint8_t buff)
|
||||
{
|
||||
lwrb_write(&lwrb_struct, &buff, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief by_tf_parse 定时回调函数,要求触发周期为 1ms
|
||||
*
|
||||
*/
|
||||
void by_tiny_frame_parse_timer_handle(void)
|
||||
{
|
||||
#if defined(BY_TF_DEVICE_MASTER)
|
||||
if (listern_flag) {
|
||||
listern_timevia++;
|
||||
} else {
|
||||
listern_timevia = 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void by_tiny_frame_parse_run(void)
|
||||
{
|
||||
|
||||
#if defined(BY_TF_DEVICE_MASTER)
|
||||
if (0 == listern_flag) {
|
||||
return;
|
||||
} else {
|
||||
if (listern_timeout <= listern_timevia) {
|
||||
// 接收超时,停止监听
|
||||
by_tiny_frame_parse_end_listern();
|
||||
#if (BY_TF_DEBUG)
|
||||
printf("by_tf_listern timeout\r\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
for (uint8_t i = 0; i < lwrb_get_full(&lwrb_struct); i++) {
|
||||
|
||||
if (!lwrb_read(&lwrb_struct, &buffer_out, 1)) {
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO 目前接收校验错误也会等待直至超时
|
||||
// TODO 待结合 read&wirte 部分修改监听的从机地址
|
||||
if (!by_tiny_frame_parse_listening(&frame_now, 0x0D, buffer_out)) {
|
||||
if (!by_tiny_frame_parse_crc(&frame_now)) {
|
||||
|
||||
// 接收成功后停止监听
|
||||
by_tiny_frame_parse_end_listern();
|
||||
// 解析成功回调
|
||||
parse_success_handle();
|
||||
#if (BY_TF_DEBUG)
|
||||
printf("frame parsed!\r\n");
|
||||
#endif
|
||||
@@ -110,4 +158,19 @@ uint8_t by_tiny_frame_parse_crc(by_tf_parse_frame_t *frame_s)
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void by_tiny_frame_parse_handle_register(by_tf_parse_success_handle_func func)
|
||||
{
|
||||
parse_success_handle = func;
|
||||
}
|
||||
|
||||
void by_tiny_frame_parse_start_listern(void)
|
||||
{
|
||||
listern_flag = 1;
|
||||
}
|
||||
|
||||
void by_tiny_frame_parse_end_listern(void)
|
||||
{
|
||||
listern_flag = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user