PLCMonitor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "stdafx.h"
  2. #include "Automatic.h"
  3. #include "PLCMonitor.h"
  4. namespace modbus
  5. {
  6. const clock_t CPLCMonitor::PLC_LASER_TIMEOUT_READY = 10000; //����źŵ�������ʱ
  7. const clock_t CPLCMonitor::PLC_LASER_TIMEOUT_PINGPANG = 3000;
  8. const clock_t CPLCMonitor::PLC_LASER_TIMEOUT_START = 300;
  9. const clock_t CPLCMonitor::PLC_LASER_TIMEOUT_MAXMEASURE = 600000;
  10. CPLCMonitor::CPLCMonitor(void* pOwnerObject)
  11. :_monitoring(FALSE)
  12. , _heartbeat_write_clock(clock())
  13. , m_lockValue(0)
  14. , m_lock_cmd_Value(0)
  15. , m_callback(0)
  16. ,m_pointer(0)
  17. , m_logWnd(0)
  18. ,m_laser_data_wnd(0)
  19. {
  20. cs_pcl.Initlock(&m_lockValue);
  21. cs_cmd.Initlock(&m_lock_cmd_Value);
  22. memset(&_value, 0, sizeof(_value));
  23. }
  24. CPLCMonitor::~CPLCMonitor()
  25. {
  26. _monitoring = false;
  27. if (m_thread_read)
  28. {
  29. WaitForSingleObject(m_thread_read->m_hThread, INFINITE);
  30. //CloseHandle(m_pThread);
  31. m_thread_read = NULL;
  32. }
  33. }
  34. int CPLCMonitor::connect(const char *ip, int port, int slave_id)
  35. {
  36. cs_pcl.Lock();
  37. if (-1 == dev.initialize(ip, port, slave_id))
  38. {
  39. cs_pcl.Unlock();
  40. return -1;
  41. }
  42. cs_pcl.Unlock();
  43. _monitoring = TRUE;
  44. if (FALSE == Start())
  45. {
  46. dev.deinitialize();
  47. return -1;
  48. }
  49. m_thread_read = AfxBeginThread(thread_monitor, this);
  50. m_strIP = ip;
  51. m_port = port;
  52. m_slave_id = slave_id;
  53. return 0;
  54. }
  55. void CPLCMonitor::set_callback(CommandCallback func, void* pointer)
  56. {
  57. m_callback = func;
  58. m_pointer = pointer;
  59. }
  60. int CPLCMonitor::reconnect()
  61. {
  62. cs_pcl.Lock();
  63. if (-1 == dev.initialize(m_strIP.c_str(), m_port, m_slave_id))
  64. {
  65. cs_pcl.Unlock();
  66. return -1;
  67. }
  68. cs_pcl.Unlock();
  69. return 0;
  70. }
  71. void CPLCMonitor::disconnect()
  72. {
  73. _monitoring = FALSE;
  74. Join(1000);
  75. Terminate();
  76. cs_pcl.Lock();
  77. dev.deinitialize();
  78. cs_pcl.Unlock();
  79. }
  80. int CPLCMonitor::read_registers(int addr, int nb, uint16_t *dest)
  81. {
  82. cs_pcl.Lock();
  83. int rc = dev.read_registers(addr, nb, dest);
  84. cs_pcl.Unlock();
  85. if (0 != rc)
  86. {
  87. reconnect();//�˿�������
  88. }
  89. return rc;
  90. }
  91. int CPLCMonitor::write_registers(int addr, int nb, uint16_t *dest)
  92. {
  93. cs_pcl.Lock();
  94. int rc = dev.write_registers(addr, nb, dest);
  95. cs_pcl.Unlock();
  96. return rc;
  97. }
  98. int CPLCMonitor::read_register(int addr, uint16_t *dest)
  99. {
  100. cs_pcl.Lock();
  101. int rc = dev.read_register(addr, dest);
  102. cs_pcl.Unlock();
  103. return rc;
  104. }
  105. int CPLCMonitor::write_register(int addr, uint16_t *dest)
  106. {
  107. cs_pcl.Lock();
  108. int rc = dev.write_register(addr, dest);
  109. cs_pcl.Unlock();
  110. return rc;
  111. }
  112. int CPLCMonitor::clear_laserstatus_register(uint16_t value)
  113. {
  114. cs_pcl.Lock();
  115. int rc = dev.write_register(REG_WHISKBOOMLASER_STATUS, &value);
  116. cs_pcl.Unlock();
  117. _heartbeat_write_clock = clock();
  118. return rc;
  119. }
  120. int CPLCMonitor::write_laserstatus_register(uint16_t value)
  121. {
  122. uint16_t dest = 0xff;
  123. cs_pcl.Lock();
  124. int rc = dev.read_register(REG_WHISKBOOMLASER_STATUS, &dest);
  125. if (PLC_LASER_ERROR != dest)
  126. {
  127. rc = dev.write_register(REG_WHISKBOOMLASER_STATUS, &value);
  128. }
  129. cs_pcl.Unlock();
  130. _heartbeat_write_clock = clock();
  131. if (0 != rc)
  132. {
  133. reconnect();//�˿�������
  134. }
  135. return rc;
  136. }
  137. int CPLCMonitor::write_laserresult_register(int addr,uint16_t *pvalue)
  138. {
  139. uint16_t dest = 0xff;
  140. cs_pcl.Lock();
  141. int rc= dev.write_registers(addr, 7, pvalue);
  142. /*int rc = dev.read_register(REG_WHISKBOOMLASER_STATUS, &dest);
  143. if (PLC_LASER_ERROR != dest)
  144. {
  145. rc = dev.write_registers(REG_WHISKBOOMLASER_STATUS, 7, pvalue);
  146. }*/
  147. cs_pcl.Unlock();
  148. _heartbeat_write_clock = clock();
  149. return rc;
  150. }
  151. void CPLCMonitor::MeasureComplete(bool bOK)
  152. {
  153. //_measure_finished = TRUE;
  154. if (!bOK)
  155. {
  156. write_laserstatus_register(PLC_LASER_FINISH_FAILED);
  157. }
  158. else
  159. {
  160. write_laserstatus_register(PLC_LASER_FINISH_OK);
  161. }
  162. }
  163. bool CPLCMonitor::setMeasureResult(int addr, struct whiskboom_laser_value * p)
  164. {
  165. if (p == 0||addr<0) //ʧ�ܣ�û�а�ɨ�����Ӳ������
  166. {
  167. return false;
  168. }
  169. else
  170. {
  171. uint16_t response[7] = { 0 };
  172. memset(response, 0, sizeof(uint16_t) * 7);
  173. if (p->corrected)
  174. {
  175. response[6] = eLaser_data_ok;
  176. }
  177. else {
  178. response[6] = eLaser_data_failed;
  179. }
  180. response[0] = p->x;
  181. response[1] = p->y;
  182. response[2] = (int(p->a) % 18000);
  183. response[3] = p->l;
  184. response[4] = p->w;
  185. response[5] = p->h;
  186. write_laserresult_register(addr,(uint16_t *)response);
  187. return true;
  188. }
  189. }
  190. void CPLCMonitor::Monitoring()
  191. {
  192. const int SIGNAL_NUM = 44;
  193. const int SIGNAL_ADDR = 0;
  194. static uint16_t value[SIGNAL_NUM] = { 0 };
  195. static uint16_t last_value[SIGNAL_NUM] = { 0 };
  196. int rc = read_registers(SIGNAL_ADDR, SIGNAL_NUM, last_value);
  197. LOG(INFO) << "\t����signal����߳�";
  198. while (_monitoring)
  199. {
  200. memset(value, 0, SIGNAL_NUM * sizeof(uint16_t));
  201. int rc = read_registers(SIGNAL_ADDR, SIGNAL_NUM, value);
  202. if (rc == 0)
  203. {
  204. for (int i = REG_PARKSTATUS; i < SIGNAL_NUM; ++i)
  205. {
  206. if (value[i] != last_value[i])
  207. {
  208. if (m_logWnd != NULL)
  209. ::PostMessage(m_logWnd->m_hWnd, WM_LOG_MSG, (WPARAM)(i + SIGNAL_ADDR), (LPARAM)(value[i]));
  210. }
  211. }
  212. memcpy(last_value, value, sizeof(uint16_t)*SIGNAL_NUM);
  213. }
  214. if (m_logWnd != NULL)
  215. ::PostMessage(m_logWnd->m_hWnd, WM_MONITOR_MSG, (WPARAM)SIGNAL_NUM, (LPARAM)value);
  216. if (m_laser_data_wnd)
  217. ::PostMessage(m_laser_data_wnd->m_hWnd, WM_MONITOR_MSG, (WPARAM)(SIGNAL_NUM), (LPARAM)(value));
  218. Sleep(100);
  219. }
  220. LOG(INFO) << "\t����signal����߳�";
  221. }
  222. UINT CPLCMonitor::thread_monitor(LPVOID lp)
  223. {
  224. CPLCMonitor* plc = (CPLCMonitor*)lp;
  225. plc->Monitoring();
  226. return 0;
  227. }
  228. void CPLCMonitor::Run()
  229. {
  230. uint16_t value[2] = { 0xffff,0xffff };
  231. uint16_t parking_status = 0xff;
  232. uint16_t response[7] = { 0 };
  233. int rc = -1;
  234. clock_t current_clock;
  235. uint16_t finite_state_machines = PLC_LASER_READY;
  236. write_laserstatus_register(finite_state_machines);
  237. uint16_t las_parkstatus = 0;
  238. while (_monitoring)
  239. {
  240. memset(value, 0xff, sizeof(value));
  241. rc = read_registers(REG_PARKSTATUS, 2, value);
  242. if (-1 == rc)
  243. {
  244. fprintf(stderr, "CPLCMonitor: read regiser failed\n");
  245. }
  246. parking_status = value[0];
  247. finite_state_machines = value[1];
  248. //PLC ��Ҫͣ���ȴ�״̬
  249. if (PLC_PARKING_WAIT == parking_status)
  250. {
  251. current_clock = clock();
  252. //�����ϴ�״̬Ϊ��������ɳɹ����߲������ʧ�ܵ�״̬
  253. if (PLC_LASER_FINISH_OK == finite_state_machines
  254. || PLC_LASER_FINISH_FAILED == finite_state_machines)
  255. {
  256. //����10�룬��ʼ���ؾ���״̬
  257. if (current_clock - _heartbeat_write_clock >= PLC_LASER_TIMEOUT_READY)
  258. {
  259. //���������
  260. if (PLC_LASER_FINISH_FAILED == finite_state_machines)
  261. {
  262. /*memset(response, 0, 7 * sizeof(uint16_t));
  263. write_laserresult_register((uint16_t *)response);*/
  264. }
  265. write_laserstatus_register(PLC_LASER_READY);
  266. finite_state_machines = PLC_LASER_READY;
  267. }
  268. }
  269. //�����ϴ�״̬Ϊ��������������״̬1��
  270. else if (PLC_LASER_READY == finite_state_machines
  271. || PLC_LASER_PONG == finite_state_machines)
  272. {
  273. //����3�룬��ʼ��������״̬2
  274. if (current_clock - _heartbeat_write_clock >= PLC_LASER_TIMEOUT_PINGPANG)
  275. {
  276. write_laserstatus_register(PLC_LASER_PING);
  277. finite_state_machines = PLC_LASER_PING;
  278. }
  279. }
  280. //�����ϴ�״̬Ϊ������״̬2
  281. else if (PLC_LASER_PING == finite_state_machines)
  282. {
  283. //����3�룬��ʼ��������״̬1
  284. if (current_clock - _heartbeat_write_clock >= PLC_LASER_TIMEOUT_PINGPANG)
  285. {
  286. write_laserstatus_register(PLC_LASER_PONG);
  287. finite_state_machines = PLC_LASER_PONG;
  288. }
  289. }
  290. //�����ϴ�״̬Ϊ��������ʼ�������У�
  291. else if (PLC_LASER_START == finite_state_machines
  292. || PLC_LASER_WORKING == finite_state_machines)
  293. {
  294. //��ʱ��������ڲ���״̬��,�ü������ֹͣ�������
  295. // 1. ���δ�ڲ���״̬���������״̬�������κη���ֵ��
  296. // 2. ����ڲ���״̬���в���ʧ����Ϣ�봦��
  297. LOG(ERROR) << "\t �����п�ʼ�ź���ֹ";
  298. /* cs_cmd.Lock();
  299. for (std::vector<CLaserNet*>::iterator it = _cmdlistLaserPointer.begin(); it != _cmdlistLaserPointer.end(); ++it)
  300. (*it)->Stop();
  301. cs_cmd.Unlock();*/
  302. write_laserstatus_register(PLC_LASER_READY);
  303. finite_state_machines = PLC_LASER_READY;
  304. }
  305. //�����޸����󣬵�ϵͳ����
  306. else if (PLC_LASER_ERROR == finite_state_machines)
  307. {
  308. fprintf(stderr, "\n\n\n\n\n\n�����޸����󣬵�ϵͳ����\n\n\n\n\n\n");
  309. }
  310. else
  311. {
  312. fprintf(stderr, "\n\n\n\n\n\n&&&&&&&&&!!!!!!!!!!!!!@@@@@@@@@@@@@@$$$$$$$$$$$$$$$$$$$��ȫ�����ܳ��ֵ�״̬\n\n\n\n\n\n");
  313. }
  314. las_parkstatus = PLC_PARKING_WAIT;
  315. }
  316. //PLC ��Ҫͣ��������ʼ
  317. else if (parking_status>0)
  318. {
  319. //�����ϴ�״̬Ϊ������������״̬1������״̬2
  320. if(PLC_LASER_READY == finite_state_machines
  321. || PLC_LASER_PING == finite_state_machines
  322. || PLC_LASER_PONG == finite_state_machines
  323. || PLC_LASER_FINISH_OK == finite_state_machines
  324. || PLC_LASER_FINISH_FAILED == finite_state_machines)
  325. {
  326. if (PLC_PARKING_WAIT == las_parkstatus)
  327. //�ò������Ϊδ���״̬
  328. {
  329. _measure_finished = FALSE;
  330. //������ʼ
  331. if (m_callback)
  332. {
  333. //char plate = parking_status;
  334. uint16_t param = 0x03;
  335. m_callback(true, param, m_pointer);
  336. }
  337. //���ز�����ʼ״̬
  338. write_laserstatus_register(PLC_LASER_START);
  339. finite_state_machines = PLC_LASER_START;
  340. }
  341. }
  342. //�ϴ�״̬Ϊ��������ɳɹ���������ɴ���
  343. if (PLC_LASER_FINISH_OK == finite_state_machines
  344. || PLC_LASER_FINISH_FAILED == finite_state_machines)
  345. {
  346. if (current_clock - _heartbeat_write_clock >= PLC_LASER_TIMEOUT_PINGPANG)
  347. {
  348. write_laserstatus_register(PLC_LASER_PING);
  349. finite_state_machines = PLC_LASER_PING;
  350. }
  351. }
  352. //�����ϴ�״̬Ϊ:��ʼ����
  353. else if (PLC_LASER_START == finite_state_machines)
  354. {
  355. current_clock = clock();
  356. //����200���룬���ز�����
  357. if (current_clock - _heartbeat_write_clock >= PLC_LASER_TIMEOUT_START)
  358. {
  359. write_laserstatus_register(PLC_LASER_WORKING);
  360. finite_state_machines = PLC_LASER_WORKING;
  361. }
  362. }
  363. else if (PLC_LASER_WORKING == finite_state_machines)
  364. {
  365. if (TRUE == _measure_finished)
  366. {
  367. //�������
  368. /* memset(response, 0, sizeof(uint16_t) * 7);
  369. if (_value.corrected)
  370. {
  371. response[0] = PLC_LASER_FINISH_OK;
  372. }
  373. else {
  374. response[0] = PLC_LASER_FINISH_FAILED;
  375. }
  376. response[1] = _value.x;
  377. response[2] = _value.y;
  378. response[3] = (int(_value.a) % 18000);
  379. response[4] = _value.l;
  380. response[5] = _value.w;
  381. response[6] = _value.h;
  382. write_laserresult_register((uint16_t *)response);*/
  383. write_laserstatus_register(PLC_LASER_FINISH_FAILED);
  384. finite_state_machines = PLC_LASER_FINISH_FAILED;
  385. }
  386. else {
  387. current_clock = clock();
  388. if (current_clock - _heartbeat_write_clock > PLC_LASER_TIMEOUT_MAXMEASURE) //10���ӻ�û���ز�������� ˵��ϵͳ���ܳ�����
  389. {
  390. write_laserstatus_register(PLC_LASER_ERROR);
  391. finite_state_machines = PLC_LASER_ERROR;
  392. }
  393. }
  394. }
  395. else if (PLC_LASER_ERROR == finite_state_machines)
  396. {
  397. fprintf(stderr, "\n\n\n\n\n\n�����޸����󣬵�ϵͳ����\n\n\n\n\n\n");
  398. }
  399. else
  400. {
  401. fprintf(stderr, "\n\n\n\n\n\n&&&&&&&&&!!!!!!!!!!!!!@@@@@@@@@@@@@@$$$$$$$$$$$$$$$$$$$��ȫ�����ܳ��ֵ�״̬\n\n\n\n\n\n");
  402. }
  403. las_parkstatus = PCL_PARKING_REQUEST;
  404. }
  405. else
  406. {
  407. if (PCL_PARKING_ERROR != las_parkstatus)
  408. {
  409. LOG(ERROR)<<("\t���ؾ���:PLC��Ҫͣ��״̬�쳣�������޸����󣬵�ϵͳ����");
  410. //ֹͣ���м���
  411. if (m_callback)
  412. {
  413. char laser = 0xFF;
  414. m_callback(false, uint16_t(laser), m_pointer);
  415. }
  416. write_laserstatus_register(PLC_LASER_READY);
  417. finite_state_machines = PLC_LASER_READY;
  418. }
  419. Sleep(200);
  420. las_parkstatus = PCL_PARKING_ERROR;
  421. }
  422. Sleep(50);
  423. }
  424. }
  425. }