dispatch_communication.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Created by huli on 2020/9/25.
  3. //
  4. #include "dispatch_communication.h"
  5. Dispatch_communication::Dispatch_communication()
  6. {
  7. }
  8. Dispatch_communication::~Dispatch_communication()
  9. {
  10. }
  11. //初始化 通信 模块。如下三选一
  12. Error_manager Dispatch_communication::communication_init()
  13. {
  14. int t_index = 0;
  15. std::vector<Snap7_buf::Variable_information> t_variable_information_vector;
  16. //往map通信缓存里面添加所需要的buf
  17. std::unique_lock<std::mutex> t_lock1(m_receive_buf_lock);
  18. std::unique_lock<std::mutex> t_lock2(m_send_buf_lock);
  19. t_index = 0;
  20. t_variable_information_vector.clear();
  21. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved0", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  22. t_index += sizeof(unsigned char)*1;
  23. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_command_flag", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  24. t_index += sizeof(unsigned char)*1;
  25. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_task_status", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  26. t_index += sizeof(unsigned char)*1;
  27. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved3_11", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 9 });
  28. t_index += sizeof(unsigned char)*9;
  29. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_block_id", typeid(unsigned short).name(), t_index,sizeof(unsigned short), 1 });
  30. t_index += sizeof(unsigned short)*1;
  31. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_floor_id", typeid(unsigned short).name(), t_index,sizeof(unsigned short), 1 });
  32. t_index += sizeof(unsigned short)*1;
  33. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_parkspace_id", typeid(unsigned short).name(), t_index,sizeof(unsigned short), 1 });
  34. t_index += sizeof(unsigned short)*1;
  35. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_command_key", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 256 });
  36. t_index += sizeof(unsigned char)*256;
  37. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_is_entrance_flag", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  38. t_index += sizeof(unsigned char)*1;
  39. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved275_289", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 15 });
  40. t_index += sizeof(unsigned char)*15;
  41. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_center_x", typeid(float).name(), t_index,sizeof(float), 1 });
  42. t_index += sizeof(float)*1;
  43. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_center_y", typeid(float).name(), t_index,sizeof(float), 1 });
  44. t_index += sizeof(float)*1;
  45. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_car_angle", typeid(float).name(), t_index,sizeof(float), 1 });
  46. t_index += sizeof(float)*1;
  47. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_car_length", typeid(float).name(), t_index,sizeof(float), 1 });
  48. t_index += sizeof(float)*1;
  49. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_car_width", typeid(float).name(), t_index,sizeof(float), 1 });
  50. t_index += sizeof(float)*1;
  51. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_car_height", typeid(float).name(), t_index,sizeof(float), 1 });
  52. t_index += sizeof(float)*1;
  53. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_wheel_base", typeid(float).name(), t_index,sizeof(float), 1 });
  54. t_index += sizeof(float)*1;
  55. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_wheel_width", typeid(float).name(), t_index,sizeof(float), 1 });
  56. t_index += sizeof(float)*1;
  57. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_voucher_number", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 256 });
  58. t_index += sizeof(unsigned char)*256;
  59. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_plate_number", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 512 });
  60. t_index += sizeof(unsigned char)*512;
  61. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_phone_number", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 256 });
  62. t_index += sizeof(unsigned char)*256;
  63. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved1346_1377", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 32 });
  64. t_index += sizeof(unsigned char)*32;
  65. Snap7_buf t_response(RESPONSE_FROM_PLC_TO_DISPATCH_DBNUMBER, 0, sizeof(Response_from_plc_to_dispatch), t_variable_information_vector, Snap7_buf::LOOP_COMMUNICATION);
  66. m_receive_buf_map[0] = t_response;
  67. Snap7_buf t_request(REQUEST_FROM_DISPATCH_TO_PLC_DBNUMBER, 0, sizeof(Request_from_dispatch_to_plc), t_variable_information_vector,Snap7_buf::NO_COMMUNICATION);
  68. m_send_buf_map[0] = t_request;
  69. t_index = 0;
  70. t_variable_information_vector.clear();
  71. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_heartbeat", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  72. t_index += sizeof(unsigned char)*1;
  73. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_check_flag", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  74. t_index += sizeof(unsigned char)*1;
  75. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved2_9", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 8 });
  76. t_index += sizeof(unsigned char)*8;
  77. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_year", typeid(unsigned short).name(), t_index,sizeof(unsigned short), 1 });
  78. t_index += sizeof(unsigned short)*1;
  79. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_month", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  80. t_index += sizeof(unsigned char)*1;
  81. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_day", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  82. t_index += sizeof(unsigned char)*1;
  83. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_weekday", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  84. t_index += sizeof(unsigned char)*1;
  85. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_hour", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  86. t_index += sizeof(unsigned char)*1;
  87. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_minute", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  88. t_index += sizeof(unsigned char)*1;
  89. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_second", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  90. t_index += sizeof(unsigned char)*1;
  91. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_nanosecond", typeid(unsigned int).name(), t_index,sizeof(unsigned int), 1 });
  92. t_index += sizeof(unsigned int)*1;
  93. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved22_29", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 8 });
  94. t_index += sizeof(unsigned char)*8;
  95. Snap7_buf t_dispatch_status(STATUS_FROM_DISPATCH_TO_PLC_DBNUMBER, 0, sizeof(Status_from_dispatch_to_plc), t_variable_information_vector,Snap7_buf::NO_COMMUNICATION);
  96. m_send_buf_map[1] = t_dispatch_status;
  97. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_elevator_information", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  98. t_index += sizeof(unsigned char)*1;
  99. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved31", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  100. t_index += sizeof(unsigned char)*1;
  101. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_elevator_coordinates", typeid(double).name(), t_index,sizeof(double), 1 });
  102. t_index += sizeof(double)*1;
  103. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved40_73", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 34 });
  104. t_index += sizeof(unsigned char)*34;
  105. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_carrier_information", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  106. t_index += sizeof(unsigned char)*1;
  107. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved75", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
  108. t_index += sizeof(unsigned char)*1;
  109. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_carrier_coordinates", typeid(double).name(), t_index,sizeof(double), 1 });
  110. t_index += sizeof(double)*1;
  111. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_carrier_angle", typeid(double).name(), t_index,sizeof(double), 1 });
  112. t_index += sizeof(double)*1;
  113. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved92_125", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 34 });
  114. t_index += sizeof(unsigned char)*34;
  115. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_carrier_error_code", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 50 });
  116. t_index += sizeof(unsigned char)*50;
  117. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_carrier_error_string", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 512 });
  118. t_index += sizeof(unsigned char)*512;
  119. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_elevator_error_code", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 50 });
  120. t_index += sizeof(unsigned char)*50;
  121. t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_elevator_error_string", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 512 });
  122. t_index += sizeof(unsigned char)*512;
  123. Snap7_buf t_plc_status(STATUS_FROM_PLC_TO_DISPATCH_DBNUMBER, 0, sizeof(Status_from_plc_to_dispatch), t_variable_information_vector,Snap7_buf::LOOP_COMMUNICATION);
  124. m_receive_buf_map[1] = t_plc_status;
  125. return Snap7_communication_base::communication_init();
  126. }
  127. //反初始化 通信 模块。
  128. Error_manager Dispatch_communication::communication_uninit()
  129. {
  130. return Snap7_communication_base::communication_uninit();
  131. }
  132. std::mutex * Dispatch_communication::get_data_lock()
  133. {
  134. return &m_data_lock;
  135. }
  136. Dispatch_communication::Request_from_dispatch_to_plc * Dispatch_communication::get_request_from_dispatch_to_plc()
  137. {
  138. return &m_request_from_dispatch_to_plc;
  139. }
  140. Dispatch_communication::Response_from_plc_to_dispatch * Dispatch_communication::get_response_from_plc_to_dispatch()
  141. {
  142. return &m_response_from_plc_to_dispatch;
  143. }
  144. Dispatch_communication::Status_from_dispatch_to_plc * Dispatch_communication::get_status_from_dispatch_to_plc()
  145. {
  146. return &m_status_from_dispatch_to_plc;
  147. }
  148. Dispatch_communication::Status_from_plc_to_dispatch * Dispatch_communication::get_status_from_plc_to_dispatch()
  149. {
  150. return &m_status_from_plc_to_dispatch;
  151. }
  152. //更新数据
  153. Error_manager Dispatch_communication::updata_receive_buf()
  154. {
  155. std::unique_lock<std::mutex> t_lock1(m_receive_buf_lock);
  156. std::unique_lock<std::mutex> t_lock2(m_data_lock);
  157. memcpy(&m_response_from_plc_to_dispatch, m_receive_buf_map[0].mp_buf_obverse, m_receive_buf_map[0].m_size);
  158. memcpy(&m_status_from_plc_to_dispatch, m_receive_buf_map[1].mp_buf_obverse, m_receive_buf_map[1].m_size);
  159. // std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_heartbeat = " << (int)m_status_from_plc_to_dispatch.m_heartbeat << std::endl;
  160. // std::cout << " huli test :::: " << " m_response_from_plc_to_dispatch.m_command_key = " << m_response_from_plc_to_dispatch.m_command_key << std::endl;
  161. // std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_elevator_coordinates = " << m_status_from_plc_to_dispatch.m_elevator_coordinates << std::endl;
  162. // std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_carrier_coordinates = " << m_status_from_plc_to_dispatch.m_carrier_coordinates << std::endl;
  163. return Error_code::SUCCESS;
  164. }
  165. Error_manager Dispatch_communication::updata_send_buf()
  166. {
  167. std::unique_lock<std::mutex> t_lock1(m_send_buf_lock);
  168. std::unique_lock<std::mutex> t_lock2(m_data_lock);
  169. // memcpy(m_request_from_dispatch_to_plc.m_command_key, "from_dispatch_to_plc_123", 20);
  170. // m_status_from_dispatch_to_plc.m_year = (unsigned short)20;
  171. // m_status_from_dispatch_to_plc.m_day = 20;
  172. // m_status_from_dispatch_to_plc.m_heartbeat = 20;
  173. memcpy(m_send_buf_map[0].mp_buf_obverse, &m_request_from_dispatch_to_plc, m_send_buf_map[0].m_size);
  174. m_send_buf_map[0].set_communication_mode(Snap7_buf::ONCE_COMMUNICATION);
  175. memcpy(m_send_buf_map[1].mp_buf_obverse, &m_status_from_dispatch_to_plc, m_send_buf_map[1].m_size);
  176. m_send_buf_map[1].set_communication_mode(Snap7_buf::ONCE_COMMUNICATION);
  177. return Error_code::SUCCESS;
  178. }