Plc_socket.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // Created by wk on 2022/12/6.
  3. //
  4. #ifndef PLC_SOCKET_TEST_PLC_SOCKET_H
  5. #define PLC_SOCKET_TEST_PLC_SOCKET_H
  6. #include <string.h>
  7. #include <string>
  8. #include <queue>
  9. #include "communication_socket.h"
  10. typedef void (*ReadCallback)(char*,void*);
  11. class Plc_socket
  12. {
  13. public:
  14. Plc_socket();
  15. virtual ~Plc_socket();
  16. public://API functions
  17. bool plc_socket_init(std::string ip, unsigned short port);//初始化
  18. void set_read_callback(ReadCallback rcb,void* client);//设置回调函数
  19. void write_data(std::string data);//写入PLC数据
  20. void plc_socket_uninit();//初始化
  21. private:
  22. //读取数据线程
  23. void read_plc_data_thread();
  24. //写入数据线程
  25. void write_plc_data_thread();
  26. //自动重连线程
  27. void reconnect_thread();
  28. private:
  29. Communication_socket m_socket; //socket
  30. ReadCallback m_read_callback; //接收数据回调
  31. void* client_= nullptr;
  32. std::string m_ip; //IP地址
  33. unsigned short m_port; //端口
  34. std::thread* mp_reconnect_thread; //重连线程
  35. bool m_reconnect_condition; //重连线程条件变量
  36. std::thread* mp_read_data_thread;//读取线程
  37. bool m_read_data_condition;//读取线程的条件变量
  38. std::thread* mp_write_data_thread; //写线程
  39. bool m_write_data_condition; //写线程条件变量
  40. std::queue<std::string> m_write_data_queue;//写入数据queue
  41. };
  42. #endif //PLC_SOCKET_TEST_PLC_SOCKET_H