PLCMonitor.h 3.4 KB

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