plc_data.h 1.7 KB

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