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