DataHelper.hpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include <Cmnpp/IProtocol.h>
  3. #include <memepp/variable_buffer.hpp>
  4. #include <memepp/buffer_view.hpp>
  5. struct ret_value
  6. {
  7. double double_value;
  8. char str_value[255];
  9. int str_len;
  10. int type =0;
  11. };
  12. class DataHelper
  13. {
  14. public:
  15. // 检查给定数据缓冲区的校验和是否正确
  16. // 参数:
  17. // buf: 数据缓冲区数组
  18. // start: 校验和开始位置的索引
  19. // len: 校验和计算的字节数
  20. // checksum: 校验和计算的字节数
  21. // 返回值: 校验和是否正确的布尔值
  22. static void check_sum(uint8_t buf[], int start, int len, uint8_t checksum[]);
  23. // 获取buf数组的长度,因为buf数组是静态存储的,因此使用静态关键字static进行修饰
  24. // 参数:
  25. // - buf: 字节数组
  26. // 返回值:
  27. // - int: buf数组的长度
  28. static int get_len(uint8_t buf[]);
  29. // 生成协议的数组长度:
  30. // len: 生成的字节数组的长度
  31. // afterLen: 生成的字节数组的数组名
  32. static void generate_len(int len, uint8_t after_len[]);
  33. // 该函数用于从给定的字节数组buf中获取无符号整数,并返回该整数值
  34. // 参数:
  35. // - buf: 存储无符号整数的字节数组
  36. // - len: 字节数组的长度
  37. static int get_unsigned_int(uint8_t buf[], int len);
  38. // 该函数用于从给定的字节数组中获取一个有符号整数
  39. // 参数:
  40. // - buf:存放字节的数组
  41. // - len:字节数组的长度
  42. // 返回值:获取到的有符号整数
  43. static int get_signed_int(uint8_t buf[], int len);
  44. // 该函数用于从给定的字节数组中获取无符号的双精度浮点数
  45. // 参数:
  46. // - buf: 存储字节的数组
  47. // - len: 字节数组的长度
  48. // - factor: 用于缩放浮点数的因子
  49. // 返回值:获取到的无符号双精度浮点数
  50. static double get_unsigned_double(uint8_t buf[], int len, int factor);
  51. // 从buf数组中获取有len个元素的双精度有符号整数,每个元素的权因子为factor
  52. // 返回结果为获取到的双精度有符号整数
  53. static double get_signed_double(uint8_t buf[], int len, int factor);
  54. // 将一个字节的 ASCII 字符拷贝到缓冲区中
  55. // 参数:
  56. // - dest: 指向目标缓冲区的指针
  57. // - b: 要拷贝的字节
  58. // - index: 缓冲区中当前已拷贝的字节数,更新为拷贝后的值
  59. // 返回值:无
  60. static void buffer_copy_uint8_ascii(uint8_t* dest, uint8_t b, uint8_t &index);
  61. // 将一个字节的 ASCII 字符拷贝到缓冲区中
  62. // 参数:
  63. // - dest: 指向目标缓冲区的指针
  64. // - b: 要拷贝的字节
  65. // - index: 缓冲区中当前已拷贝的字节数,更新为拷贝后的值
  66. // 返回值:无
  67. static void buffer_copy_uint16_ascii(uint8_t* dest, uint16_t b, uint8_t &index);
  68. // 将一个字节的 ASCII 字符拷贝到缓冲区中
  69. static int buffer_from_ascii_uint8(uint8_t* dest, uint8_t* src, int len);
  70. // 从缓冲区中读取一个字节的 ASCII 字符
  71. static uint8_t read_uint8_ascii(uint8_t* src, uint8_t &index);
  72. // 从缓冲区中读取两个字节的 ASCII 字符
  73. static uint16_t read_uint16_ascii(uint8_t* src, uint8_t &index);
  74. };