dispatch_communication.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. //往map通信缓存里面添加所需要的buf
  15. {
  16. std::unique_lock<std::mutex> t_lock(m_receive_buf_lock);
  17. Snap7_buf t_response(RESPONSE_FROM_PLC_TO_DISPATCH_DBNUMBER, 0, sizeof(Response_from_plc_to_dispatch), Snap7_buf::LOOP_COMMUNICATION);
  18. m_receive_buf_map[0] = t_response;
  19. Snap7_buf t_plc_status(STATUS_FROM_PLC_TO_DISPATCH_DBNUMBER, 0, sizeof(Status_from_plc_to_dispatch), Snap7_buf::LOOP_COMMUNICATION);
  20. m_receive_buf_map[1] = t_plc_status;
  21. }
  22. {
  23. std::unique_lock<std::mutex> t_lock(m_send_buf_lock);
  24. Snap7_buf t_request(REQUEST_FROM_DISPATCH_TO_PLC_DBNUMBER, 0, sizeof(Request_from_dispatch_to_plc), Snap7_buf::NO_COMMUNICATION);
  25. m_send_buf_map[0] = t_request;
  26. Snap7_buf t_dispatch_status(STATUS_FROM_DISPATCH_TO_PLC_DBNUMBER, 0, sizeof(Status_from_dispatch_to_plc), Snap7_buf::NO_COMMUNICATION);
  27. m_send_buf_map[1] = t_dispatch_status;
  28. }
  29. return Snap7_communication_base::communication_init();
  30. }
  31. //反初始化 通信 模块。
  32. Error_manager Dispatch_communication::communication_uninit()
  33. {
  34. return Snap7_communication_base::communication_uninit();
  35. }
  36. //更新数据
  37. Error_manager Dispatch_communication::updata_receive_buf()
  38. {
  39. std::unique_lock<std::mutex> t_lock1(m_receive_buf_lock);
  40. std::unique_lock<std::mutex> t_lock2(m_data_lock);
  41. memcpy(&m_response_from_plc_to_dispatch, &m_receive_buf_map[0], m_receive_buf_map[0].m_size);
  42. memcpy(&m_status_from_plc_to_dispatch, &m_receive_buf_map[1], m_receive_buf_map[1].m_size);
  43. std::cout << " huli test :::: " << " m_response_from_plc_to_dispatch.m_command_key = " << m_response_from_plc_to_dispatch.m_command_key << std::endl;
  44. std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_elevator_coordinates = " << m_status_from_plc_to_dispatch.m_elevator_coordinates << std::endl;
  45. std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_carrier_coordinates = " << m_status_from_plc_to_dispatch.m_carrier_coordinates << std::endl;
  46. return Error_code::SUCCESS;
  47. }
  48. Error_manager Dispatch_communication::updata_send_buf()
  49. {
  50. std::unique_lock<std::mutex> t_lock1(m_send_buf_lock);
  51. std::unique_lock<std::mutex> t_lock2(m_data_lock);
  52. memcpy(&m_request_from_dispatch_to_plc.m_command_key, "from_dispatch_to_plc_123", 24);
  53. m_status_from_dispatch_to_plc.m_year = 2020;
  54. memcpy(&m_send_buf_map[0], &m_request_from_dispatch_to_plc, m_send_buf_map[0].m_size);
  55. m_send_buf_map[0].set_communication_mode(Snap7_buf::ONCE_COMMUNICATION);
  56. memcpy(&m_send_buf_map[1], &m_status_from_dispatch_to_plc, m_send_buf_map[1].m_size);
  57. m_send_buf_map[1].set_communication_mode(Snap7_buf::ONCE_COMMUNICATION);
  58. return Error_code::SUCCESS;
  59. }