snap7_clamp.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Created by huli on 2020/9/25.
  3. //
  4. #pragma once
  5. #include "tool/singleton.h"
  6. #include "snap7_communication_base.h"
  7. #include <glog/logging.h>
  8. class Snap7Clamp:public Singleton<Snap7Clamp>, public Snap7_communication_base
  9. {
  10. public:
  11. //发送db快的标记位, 保证先发数据再发唯一码
  12. enum Send_database_flag
  13. {
  14. E_SEND_UNKNOW = 0,
  15. E_SEND_DATA_START = 1, //开始发送数据
  16. E_SEND_DATA_END = 2, //结束发送数据
  17. E_SEND_KEY_START = 3, //开始发送唯一码
  18. E_SEND_KEY_END = 4, //结束发送唯一码
  19. };
  20. #pragma pack(push, 1) //struct按照1个byte对齐
  21. #define CLAMP_SAFETY_HEART_DBNUMBER 9070
  22. #define CLAMP_SAFETY_PLC_DBNUMBER 9070
  23. struct WheeLData {
  24. unsigned short wheel_exist;
  25. float offset;
  26. float gap;
  27. unsigned short clamp_completed;
  28. void info() const {
  29. if (wheel_exist == 0) {
  30. return;
  31. }
  32. LOG(INFO) << "wheel_exist = " << wheel_exist
  33. << ", offset = " << offset
  34. << ", gap = " << gap
  35. << ", clamp_completed = " << clamp_completed;
  36. }
  37. void clear() {
  38. wheel_exist = false;
  39. offset = 0;
  40. gap = 0;
  41. clamp_completed = 0;
  42. }
  43. };
  44. struct PLCData {
  45. unsigned short pingpong;
  46. struct WheeLData wheels[4];
  47. void info() {
  48. LOG(INFO) << "pingdong = " << pingpong;
  49. wheels[0].info();
  50. wheels[1].info();
  51. wheels[2].info();
  52. wheels[3].info();
  53. }
  54. void clear() {
  55. wheels[0].clear();
  56. wheels[1].clear();
  57. wheels[2].clear();
  58. wheels[3].clear();
  59. }
  60. };
  61. #pragma pack(pop) //取消对齐
  62. // 子类必须把父类设定为友元函数,这样父类才能使用子类的私有构造函数。
  63. friend class Singleton<Snap7Clamp>;
  64. private:
  65. // 父类的构造函数必须保护,子类的构造函数必须私有。
  66. Snap7Clamp();
  67. public:
  68. //必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
  69. Snap7Clamp(const Snap7Clamp& other) = delete;
  70. Snap7Clamp& operator =(const Snap7Clamp& other) = delete;
  71. ~Snap7Clamp() override;
  72. public://API functions
  73. //初始化 通信 模块。如下三选一
  74. Error_manager communication_init();
  75. //反初始化 通信 模块。
  76. Error_manager communication_uninit() override;
  77. protected://member functions
  78. //更新数据
  79. Error_manager updata_receive_buf() override;
  80. Error_manager updata_send_buf() override;
  81. protected://member variable
  82. public:
  83. std::mutex m_data_lock; //数据锁
  84. PLCData plcData{};
  85. unsigned short m_heart;
  86. private:
  87. };