plc_communicator.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef PLC_COMMUNICATOR_HH
  2. #define PLC_COMMUNICATOR_HH
  3. #include <iostream>
  4. #include <string.h>
  5. #include <mutex>
  6. #include <vector>
  7. #include <thread>
  8. #include <unistd.h>
  9. #include <chrono>
  10. #include "../error.h"
  11. #include "../task/task_command_manager.h"
  12. #include "LibmodbusWrapper.h"
  13. #include "StdCondition.h"
  14. const int PLC_SIGNAL_BEGIN_OFFSET=2;
  15. const int PLC_REGION_NUM=6;
  16. const int PLC_SIGNAL_NUM_PER_REGION=12;
  17. const int PLC_SLEEP_IN_MILLISECONDS=200;
  18. const int PLC_LASER_START_ADDR = 0;
  19. const int PLC_LASER_STATUS_ADDR = 1;
  20. const int PLC_LASER_X_ADDR = 2;
  21. const int PLC_LASER_Y_ADDR = 3;
  22. const int PLC_LASER_ANGLE_ADDR = 4;
  23. const int PLC_LASER_LENGTH_ADDR = 5;
  24. const int PLC_LASER_WIDTH_ADDR = 6;
  25. const int PLC_LASER_HEIGHT_ADDR = 7;
  26. const int PLC_LASER_CORRECTNESS_ADDR = 8;
  27. const int PLC_LASER_WHEELBASE_ADDR = 9;
  28. typedef ERROR_CODE(*Command_Callback)(int terminal_id, void * p_owner);
  29. // plc通信类,modbus通信
  30. class Plc_Communicator
  31. {
  32. public:
  33. Plc_Communicator(const char *ip, int port, int slave_id);
  34. ~Plc_Communicator();
  35. // get set 方法
  36. bool get_initialize_status();
  37. bool get_connection();
  38. // 设置plc检测到指令后外部回调函数
  39. ERROR_CODE set_plc_callback(Command_Callback callback, void * p_owner);
  40. // 执行任务单
  41. ERROR_CODE execute_task(Task_Base* task);
  42. // 获取实时数据
  43. ERROR_CODE get_plc_data(std::vector<uint16_t> &plc_data,int terminal_id=-1);
  44. // 设置plc状态更新超时时间
  45. ERROR_CODE set_status_update_timeout(int millisecond);
  46. struct plc_region_status{
  47. std::chrono::steady_clock::time_point last_time_point;
  48. int current_status;
  49. int cmd;
  50. };
  51. private:
  52. // 读写线程函数
  53. static void plc_update_thread(Plc_Communicator* plc_communicator);
  54. // 连接函数
  55. ERROR_CODE connect();
  56. ERROR_CODE disconnect();
  57. private:
  58. bool b_plc_is_connected; // 指示plc连接状态
  59. bool b_plc_initialized; // 指示plc是否初始化
  60. bool b_plc_is_updating; // 指示plc线程在运行
  61. void* p_plc_owner; // 回调函数所有者句柄
  62. Command_Callback m_plc_callback; // 回调函数
  63. std::thread* m_plc_thread; // plc更新线程句柄
  64. StdCondition m_plc_cond_exit; // plc更新线程退出条件控制变量
  65. std::mutex m_plc_mutex; // plc更新互斥锁,锁住与wrapper相关的所有操作
  66. modbus::CLibmodbusWrapper m_plc_wrapper; // plc连接与读写封装实例
  67. std::string m_plc_ip; // plc连接ip
  68. int m_plc_port; // plc连接端口
  69. int m_plc_slave_id; // plc连接id
  70. int m_plc_status_update_timeout; // plc状态更新超时时间
  71. std::vector<uint16_t> m_plc_data; // 从plc获取的实时数据
  72. ERROR_CODE m_plc_current_error; // 当前plc出现的错误
  73. // 当前系统状态,实时更新到plc。状态254-255互换,状态1从收到指令开始,紧接着改为状态2,
  74. // 之后根据外部传入的task,决定写入3或4,默认3保留3秒,4持续保留直到新指令
  75. plc_region_status m_plc_current_status[PLC_REGION_NUM];
  76. };
  77. #endif // !PLC_COMMUNICATOR_HH