snap7_communication_base.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Created by huli on 2020/9/25.
  3. //
  4. #ifndef NNXX_TESTS_SNAP7_E_BASE_H
  5. #define NNXX_TESTS_SNAP7_E_BASE_H
  6. #include <s7_client.h>
  7. #include <mutex>
  8. #include <map>
  9. #include <glog/logging.h>
  10. #include "../error_code/error_code.h"
  11. #include "../tool/thread_condition.h"
  12. #include "../snap7_communication/snap7_buf.h"
  13. #include "../snap7_communication/s7_plc.h"
  14. #include "../snap7_communication/snap7_communication.pb.h"
  15. class Snap7_communication_base
  16. {
  17. public:
  18. //snap7的通信延时, 默认50ms
  19. #define SNAP7_COMMUNICATION_DELAY_TIME_MS 10
  20. //snap7的通信参数路径
  21. #define SNAP7_COMMUNICATION_PARAMETER_PATH "../setting/snap7_communication.prototxt"
  22. //通信状态
  23. enum Snap7_communication_statu
  24. {
  25. SNAP7_COMMUNICATION_UNKNOWN =0, //通信状态 未知
  26. SNAP7_COMMUNICATION_READY =1, //通信状态 正常
  27. SNAP7_COMMUNICATION_RECEIVE =2, //接受
  28. SNAP7_COMMUNICATION_SEND =3, //发送
  29. SNAP7_COMMUNICATION_DISCONNECT =4, //断连
  30. SNAP7_COMMUNICATION_FAULT =10, //通信状态 错误
  31. };
  32. public:
  33. Snap7_communication_base();
  34. Snap7_communication_base(const Snap7_communication_base& other)= default;
  35. Snap7_communication_base& operator =(const Snap7_communication_base& other)= default;
  36. ~Snap7_communication_base();
  37. public://API functions
  38. //初始化 通信 模块。如下三选一
  39. virtual Error_manager communication_init();
  40. //初始化 通信 模块。从文件读取
  41. virtual Error_manager communication_init_from_protobuf(std::string prototxt_path);
  42. //初始化 通信 模块。从protobuf读取
  43. virtual Error_manager communication_init_from_protobuf(Snap7_communication_proto::Snap7_communication_parameter_all& snap7_communication_parameter_all);
  44. //反初始化 通信 模块。
  45. virtual Error_manager communication_uninit();
  46. //唤醒s7通信线程
  47. virtual Error_manager communication_start();
  48. //停止s7通信线程
  49. virtual Error_manager communication_stop();
  50. public://get or set member variable
  51. Snap7_communication_statu get_status();
  52. //修改通信延时, 单位ms
  53. Error_manager set_communication_delay_time_ms(int time);
  54. protected://member functions
  55. //通信连接
  56. Error_manager communication_connect(std::string ip_string);
  57. //启动通信, run thread
  58. Error_manager communication_run();
  59. //通信断连
  60. Error_manager communication_disconnect();
  61. //mp_communication_thread线程的执行函数, 负责进行s7的通信
  62. void communication_thread();
  63. //接受数据, 读取DB块,
  64. Error_manager read_data_buf(Snap7_buf& snap7_buf);
  65. //发送数据, 写入DB块,
  66. Error_manager write_data_buf(Snap7_buf& snap7_buf);
  67. //数据颠倒
  68. Error_manager reverse_byte(void* p_buf_in, void* p_buf_out, int size);
  69. //更新数据
  70. virtual Error_manager updata_receive_buf();
  71. virtual Error_manager updata_send_buf();
  72. protected://member variable
  73. public:
  74. //状态
  75. Snap7_communication_statu m_communication_status; //通信状态
  76. std::string m_ip_string; //通信ip
  77. //通信模块
  78. std::mutex m_communication_lock; //通信锁
  79. TSnap7Client m_snap7_client; //通信的客户端
  80. int m_communication_delay_time_ms;//通信延时, 单位ms
  81. //注:s7协议通信很不稳定, 在每次使用 TSnap7Client 之后, 都需要加延时
  82. //数据
  83. std::mutex m_receive_buf_lock; //接受的锁
  84. std::map<int, Snap7_buf> m_receive_buf_map; //接受的map容器
  85. std::mutex m_send_buf_lock; //发送的锁
  86. std::map<int, Snap7_buf> m_send_buf_map; //发送的map容器
  87. //线程, snap7的通信核心就是对 发送和接受内存的 周期性读写, 所以使用一个线程即可.
  88. std::thread* mp_communication_thread; //通信的线程指针
  89. Thread_condition m_communication_condition; //通信的条件变量
  90. private:
  91. };
  92. #endif //NNXX_TESTS_SNAP7_E_BASE_H