packet.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma once
  2. #include <iostream>
  3. #include "defines.hpp"
  4. class CloudPointPacket {
  5. public:
  6. /**
  7. * @brief 获取当前信息的数据内容
  8. **/
  9. virtual unsigned char *data() const {
  10. return m_data_;
  11. }
  12. /**
  13. * @brief 获取当前信息的数据长度
  14. **/
  15. virtual int size() const {
  16. return m_length_;
  17. }
  18. virtual bool packet(void *data, int size) = 0;
  19. protected:
  20. CloudPointPacket() {};
  21. ~CloudPointPacket() = default;
  22. bool packInfo(ZX::LidarMessageType type, int point_number, unsigned char distance_accuracy) {
  23. if (m_length_ < ZX::PACKET_HEAD_SIZE + ZX::PACKET_END_SIZE) {
  24. return false;
  25. }
  26. static int times = 0;
  27. m_data_[0] = '$';
  28. m_data_[1] = '&';
  29. m_data_[2] = type >> 8;
  30. m_data_[3] = type;
  31. m_data_[4] = point_number >> 24;
  32. m_data_[5] = point_number >> 16;
  33. m_data_[6] = point_number >> 8;
  34. m_data_[7] = point_number;
  35. m_data_[8] = distance_accuracy;
  36. m_data_[9] = times >> 16;
  37. m_data_[10] = times >> 8;
  38. m_data_[11] = times;
  39. m_data_[m_length_ - 2] = '&';
  40. m_data_[m_length_ - 1] = '$';
  41. times++;
  42. if (times > 0X00FFFFFF) {
  43. times = 0;
  44. }
  45. return true;
  46. }
  47. protected:
  48. unsigned char *m_data_ = nullptr;
  49. int m_length_ = 0;
  50. };