PLCMonitor.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef __PLC_MONITOR__HH_
  2. #define __PLC_MONITOR__HH_
  3. #include "LibmodbusWrapper.h"
  4. #include "Runnable.h"
  5. #include <string.h>
  6. #include <mutex>
  7. #include <vector>
  8. #include <time.h>
  9. #include "qtmessagedef.h"
  10. struct whiskboom_laser_value
  11. {
  12. float x;
  13. float y;
  14. float a;
  15. float l;
  16. float w;
  17. float h;
  18. int corrected;
  19. };
  20. #define WM_LOG_MSG WM_USER+1001
  21. #define WM_MONITOR_MSG WM_USER+1002
  22. namespace modbus
  23. {
  24. #define REG_PARKSTATUS (0)
  25. #define REG_WHISKBOOMLASER_STATUS (1)
  26. #define REG_WHISKBOOMLASER_CENTERX (2)
  27. #define REG_WHISKBOOMLASER_CENTERY (3)
  28. #define REG_WHISKBOOMLASER_ANGLEA (4)
  29. #define REG_WHISKBOOMLASER_LENGTH (5)
  30. #define REG_WHISKBOOMLASER_WIDTH (6)
  31. #define REG_WHISKBOOMLASER_HEIGHT (7)
  32. enum PLC_PARKING_STATUS
  33. {
  34. PLC_PARKING_WAIT = 0,
  35. PCL_PARKING_REQUEST,
  36. PCL_PARKING_ERROR
  37. };
  38. enum LASER_DATA_CORRECT
  39. {
  40. eLaser_data_init=0,
  41. eLaser_data_ok,
  42. eLaser_data_failed
  43. };
  44. enum PCL_LASER_STATUS
  45. {
  46. PLC_LASER_READY = 0,
  47. PLC_LASER_START,
  48. PLC_LASER_WORKING,
  49. PLC_LASER_FINISH_OK,
  50. PLC_LASER_FINISH_FAILED,
  51. PLC_LASER_ERROR,
  52. PLC_LASER_PING=254,
  53. PLC_LASER_PONG
  54. };
  55. typedef void(*CommandCallback)(bool On, uint16_t param,void* pointer,int action_type);
  56. typedef void(*PLCMonitorCallback)(QtMessageData data,void* pointer);
  57. class CPLCMonitor :
  58. public CRunnable
  59. {
  60. public:
  61. CPLCMonitor(void* pMainObject=NULL);
  62. ~CPLCMonitor();
  63. public:
  64. int connect(const char *ip, int port, int slave_id);
  65. void disconnect();
  66. int reconnect();
  67. void set_callback(CommandCallback func, PLCMonitorCallback monitorCallback,void* pointer);
  68. private:
  69. std::mutex cs_cmd;
  70. private:
  71. bool _measure_finished;
  72. struct whiskboom_laser_value _value;
  73. public:
  74. bool setMeasureResult(int addr, struct whiskboom_laser_value * value = NULL);
  75. void MeasureComplete(bool bOK=true);
  76. public:
  77. int write_laserstatus_register(uint16_t value);
  78. protected:
  79. CommandCallback m_callback;
  80. void* m_pointer;
  81. PLCMonitorCallback m_PlcDataCallback;
  82. public:
  83. bool isMonitor() { return _monitoring == true; };
  84. private:
  85. bool _monitoring;
  86. private:
  87. CLibmodbusWrapper dev;
  88. private:
  89. clock_t _heartbeat_write_clock;
  90. private:
  91. int read_registers(int addr, int nb, uint16_t *dest);
  92. int write_registers(int addr, int nb, uint16_t *dest);
  93. int read_register(int addr, uint16_t *dest);
  94. int write_register(int addr, uint16_t *dest);
  95. int clear_laserstatus_register(uint16_t value);
  96. int write_laserresult_register(int addr ,uint16_t *pvalue);
  97. private:
  98. static const double PLC_LASER_TIMEOUT_READY;
  99. static const double PLC_LASER_TIMEOUT_PINGPANG;
  100. static const double PLC_LASER_TIMEOUT_START;
  101. static const double PLC_LASER_TIMEOUT_MAXMEASURE;
  102. public:
  103. virtual void Run();
  104. private:
  105. void Monitoring();
  106. static void thread_monitor(void* lp);
  107. void* m_logWnd;
  108. void* m_laser_data_wnd;
  109. std::thread* m_thread_read;
  110. private:
  111. std::mutex cs_pcl;
  112. std::string m_strIP;
  113. int m_port;
  114. int m_slave_id;
  115. };
  116. }
  117. #endif