plc_data.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // Created by zx on 2019/12/9.
  3. //
  4. #ifndef PLC_DATA_H
  5. #define PLC_DATA_H
  6. #include <iostream>
  7. #include <string.h>
  8. #include <mutex>
  9. #include <atomic>
  10. #include "../tool/StdCondition.h"
  11. #include <thread>
  12. #include "../tool/s7_plc.h"
  13. #include "unistd.h"
  14. #include "define.h"
  15. #include "glog/logging.h"
  16. #define MAX_REGIONS 6
  17. #define ELE_FENCE_BOUNDARY_START_ADDR 3
  18. #define ELE_FENCE_DB_NUM 95
  19. #define ELE_FENCE_OFFSET 7
  20. #define ELE_FENCE_COUNT 6
  21. #define CENTRAL_CONTROLLER_DB_NUM 41
  22. #define DOOR_STATUS_OFFSET 18
  23. class Plc_data
  24. {
  25. public:
  26. // 获取plc通信类单例
  27. static Plc_data *get_instance(std::string ip = "");
  28. // static void Release();
  29. // 更新区域状态数据
  30. void update_data(int state_code, int border_status, int id);
  31. // 更新错误信息提示
  32. void update_error_info(int id, std::string error_info);
  33. // plc更新线程
  34. static void plc_update_thread(Plc_data *p);
  35. // 获取plc实时状态
  36. bool get_plc_status();
  37. public:
  38. std::atomic<bool> mb_is_ready; // 状态正常与否
  39. private:
  40. static Plc_data *g_ins; // 全局Plc_data实例
  41. static std::mutex g_lock; // 全局Plc_data实例访问互斥锁
  42. StdCondition m_cond_exit; // 系统关闭标志
  43. std::thread *mp_update_thread; // plc更新线程
  44. S7PLC m_plc; // S7协议句柄
  45. const int m_update_interval_milli = 100; // plc更新频率
  46. // 有参构造函数
  47. Plc_data(std::string ip);
  48. // class CGarbo
  49. // {
  50. // public:
  51. // ~CGarbo() {
  52. // if (g_ins)
  53. // {
  54. // delete g_ins;
  55. // g_ins = 0;
  56. // }
  57. // }
  58. // };
  59. // static CGarbo Garbo; //--->1
  60. protected:
  61. short m_data[MAX_REGIONS*2];
  62. short m_last_data[MAX_REGIONS*2];
  63. std::string m_ip_str;
  64. std::string m_current_error_info[6];
  65. };
  66. #endif //PLC_DATA_H