单片机嵌入式字符流数据解析库

打印 上一主题 下一主题

主题 1793|帖子 1793|积分 5379

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
kw_charProtocol库阐明

本库针对字符数据流范例装备,吸收发送特定协议帧制作的微库。可以裸机运行,也可以共同实时操作系统运行。
本库开源连接地址:gitee连接
实现思绪

本库采用C语言进行编程,方便移植,用户通过调用库接口函数即可使用。
以[YY6500传感器]为例,对应,该传感器采用串口作为通讯接口。
重要接口函数如下:
项目函数功能备注1kw_CPInitPara初始化注册协议变量对象,包罗:
1. 异步队列数组
2. 吸收数组
3. 发送数组2kw_CPSetHead注册协议包头3kw_CPRegisterTailCB注册协议包尾4kw_CPRegisterCheckCB注册校验和回调5kw_CPRegisterFrameCB注册解完整数据包回调6kw_CPDecode周期解码7kw_CPReceiveByte吸收字符流数据 重要变量如下
项目变量范例阐明1_g_cpYY6500kw_charProtocol_s协议对象2_g_YY6500Queue吸收队列数组unsigned char[]3_g_YY6500RBuf吸收数组缓存unsigned char[]4_g_Head包头unsigned char[] 必要实现的协议接口如下:
项目接口阐明1_IsTail包尾判断2_IsCheckOK校验和判断3_frame完整数据包回调 测试代码

协议代码

  1. #include "CP_YY6500.h"
  2. #ifdef CONFIG_KW_CP
  3. kw_charProtocol_s _g_cpYY6500;
  4. unsigned char _g_YY6500Queue[128];
  5. unsigned char _g_YY6500RBuf[128];
  6. unsigned char _g_Head[1] = {0x16};
  7. unsigned char _YY6500_Tmp[4] = {0};
  8. cp_YY6500_e _g_YY6500_Flag = CP_YY6500_NONE;
  9. cp_YY6500_s _g_YY6500;
  10. static bool _IsTail(kw_frame_s *p, unsigned char in);
  11. static bool _IsCheckOK(kw_frame_s *p);
  12. static void _frame(kw_frame_s *p);
  13. void CP_YY6500Init()
  14. {
  15.     kw_CPInitPara(&_g_cpYY6500,
  16.                   _g_YY6500Queue, sizeof(_g_YY6500Queue),
  17.                   _g_YY6500RBuf, sizeof(_g_YY6500RBuf),
  18.                   0, 0);
  19.     kw_CPSetHead(&_g_cpYY6500, _g_Head, sizeof(_g_Head));
  20.     kw_CPRegisterTailCB(&_g_cpYY6500, _IsTail);
  21.     kw_CPRegisterCheckCB(&_g_cpYY6500, _IsCheckOK);
  22.     kw_CPRegisterFrameCB(&_g_cpYY6500, _frame);
  23. }
  24. void CP_YY6500Decode()
  25. {
  26.     kw_CPDecode(&_g_cpYY6500);
  27. }
  28. void CP_YY6500SendResult(cp_YY6500_e type, int (*send)(unsigned char *datas, unsigned int len))
  29. {
  30.     if (send != 0 && type < CP_YY6500_MAX && type > CP_YY6500_NONE)
  31.     {
  32.         _YY6500_Tmp[0] = 0x11;
  33.         _YY6500_Tmp[1] = 0x01;
  34.         if(type == CP_YY6500_RESULT)
  35.         {
  36.             _YY6500_Tmp[2] = 0x01;
  37.             _YY6500_Tmp[3] = 0xED;
  38.         }
  39.         else if(type == CP_YY6500_VERSION)
  40.         {
  41.             _YY6500_Tmp[2] = 0x1E;
  42.             _YY6500_Tmp[3] = 0xD0;
  43.         }
  44.         else if(type == CP_YY6500_NO)
  45.         {
  46.             _YY6500_Tmp[2] = 0x1F;
  47.             _YY6500_Tmp[3] = 0xCF;
  48.         }
  49.         send(_YY6500_Tmp, 4);
  50.         _g_YY6500_Flag = type;
  51.     }
  52. }
  53. void CP_YY6500Receive(unsigned char in)
  54. {
  55.     kw_CPReceiveByte(&_g_cpYY6500, in);
  56. }
  57. static bool _IsTail(kw_frame_s *p, unsigned char in)
  58. {
  59.     return p->wIdx > 4 && p->wIdx == p->buf.buffer[1] + 2;
  60. }
  61. static bool _IsCheckOK(kw_frame_s *p)
  62. {
  63.     unsigned char sum = 0;
  64.     for (unsigned char i = 0; i < p->wIdx - 1; i++)
  65.     {
  66.         sum += p->buf.buffer[i];
  67.     }
  68.     return (p->buf.buffer[p->wIdx - 1] + (sum)) & (0xFF) == 0xFF;
  69. }
  70. static void _frame(kw_frame_s *p)
  71. {
  72.     if (_g_YY6500_Flag == CP_YY6500_RESULT)
  73.     {
  74.         _g_YY6500.nongdu = (p->buf.buffer[3] << 8) + p->buf.buffer[4];
  75.         _g_YY6500.liuliang = (p->buf.buffer[5] << 8) + p->buf.buffer[6];
  76.         _g_YY6500.wendu = ((p->buf.buffer[7] << 8) + p->buf.buffer[8] - 500);
  77.         _g_YY6500.res = (p->buf.buffer[9] << 8) + p->buf.buffer[10];
  78.         _g_YY6500_Flag = CP_YY6500_NONE;
  79.     }
  80.     else if(_g_YY6500_Flag == CP_YY6500_VERSION)
  81.     {
  82.         for (unsigned char i = 0; i < 8; i++)
  83.         {
  84.             _g_YY6500.version[i] = p->buf.buffer[i + 3];
  85.         }
  86.         _g_YY6500_Flag = CP_YY6500_NONE;
  87.     }
  88.     else if(_g_YY6500_Flag == CP_YY6500_NO)
  89.     {
  90.         for (unsigned char i = 0; i < 10; i++)
  91.         {
  92.             _g_YY6500.NO[i] = p->buf.buffer[i + 3];
  93.         }
  94.         _g_YY6500_Flag = CP_YY6500_NONE;
  95.     }
  96. }
  97. #endif
复制代码
  1. #ifndef _CP_YY6500_H
  2. #define _CP_YY6500_H
  3. #ifdef __cplusplus
  4. extern "C"
  5. {
  6. #endif
  7. #include "kw_baseDrvCFG.h"
  8. #ifdef CONFIG_KW_CP
  9.     typedef enum _cp_YY6500_e
  10.     {
  11.         CP_YY6500_NONE,
  12.         CP_YY6500_RESULT,
  13.         CP_YY6500_VERSION,
  14.         CP_YY6500_NO,
  15.         CP_YY6500_MAX,
  16.     } cp_YY6500_e;
  17.     typedef struct _cp_YY6500_s
  18.     {
  19.         unsigned short nongdu;
  20.         unsigned short liuliang;
  21.         short wendu;
  22.         unsigned short res;
  23.         unsigned char version[8];
  24.         unsigned char NO[10];
  25.     } cp_YY6500_s;
  26.     void CP_YY6500Init();
  27.     void CP_YY6500SendResult(cp_YY6500_e type, int (*send)(unsigned char *datas, unsigned int len));
  28.     void CP_YY6500Decode();
  29.     void CP_YY6500Receive(unsigned char in);
  30. #endif
  31. #ifdef __cplusplus
  32. }
  33. #endif
  34. #endif
复制代码
主函数调用

  1. #include "CP_YY6500.h"
  2. static void _hal_uartRecv(unsigned char data)
  3. {
  4.     CP_YY6500Receive(data);
  5. }
  6. void main()
  7. {
  8.     HAL_UARTInit();
  9.     CP_YY6500Init();
  10.     while (1)
  11.     {
  12.         CP_YY6500Decode();
  13.     }
  14. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

写过一篇

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表