// // Created by huli on 2020/9/25. // #include "dispatch_communication.h" Dispatch_communication::Dispatch_communication() { } Dispatch_communication::~Dispatch_communication() { } //初始化 通信 模块。如下三选一 Error_manager Dispatch_communication::communication_init() { //往map通信缓存里面添加所需要的buf { std::unique_lock t_lock(m_receive_buf_lock); Snap7_buf t_response(RESPONSE_FROM_PLC_TO_DISPATCH_DBNUMBER, 0, sizeof(Response_from_plc_to_dispatch), Snap7_buf::LOOP_COMMUNICATION); m_receive_buf_map[0] = t_response; Snap7_buf t_plc_status(STATUS_FROM_PLC_TO_DISPATCH_DBNUMBER, 0, sizeof(Status_from_plc_to_dispatch), Snap7_buf::LOOP_COMMUNICATION); m_receive_buf_map[1] = t_plc_status; } { std::unique_lock t_lock(m_send_buf_lock); Snap7_buf t_request(REQUEST_FROM_DISPATCH_TO_PLC_DBNUMBER, 0, sizeof(Request_from_dispatch_to_plc), Snap7_buf::NO_COMMUNICATION); m_send_buf_map[0] = t_request; Snap7_buf t_dispatch_status(STATUS_FROM_DISPATCH_TO_PLC_DBNUMBER, 0, sizeof(Status_from_dispatch_to_plc), Snap7_buf::NO_COMMUNICATION); m_send_buf_map[1] = t_dispatch_status; } return Snap7_communication_base::communication_init(); } //反初始化 通信 模块。 Error_manager Dispatch_communication::communication_uninit() { return Snap7_communication_base::communication_uninit(); } //更新数据 Error_manager Dispatch_communication::updata_receive_buf() { std::unique_lock t_lock1(m_receive_buf_lock); std::unique_lock t_lock2(m_data_lock); memcpy(&m_response_from_plc_to_dispatch, m_receive_buf_map[0].mp_buf_obverse, m_receive_buf_map[0].m_size); memcpy(&m_status_from_plc_to_dispatch, m_receive_buf_map[1].mp_buf_obverse, m_receive_buf_map[1].m_size); std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_heartbeat = " << (int)m_status_from_plc_to_dispatch.m_heartbeat << std::endl; std::cout << " huli test :::: " << " m_response_from_plc_to_dispatch.m_command_key = " << m_response_from_plc_to_dispatch.m_command_key << std::endl; std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_elevator_coordinates = " << m_status_from_plc_to_dispatch.m_elevator_coordinates << std::endl; std::cout << " huli test :::: " << " m_status_from_plc_to_dispatch.m_carrier_coordinates = " << m_status_from_plc_to_dispatch.m_carrier_coordinates << std::endl; return Error_code::SUCCESS; } Error_manager Dispatch_communication::updata_send_buf() { std::unique_lock t_lock1(m_send_buf_lock); std::unique_lock t_lock2(m_data_lock); memcpy(m_request_from_dispatch_to_plc.m_command_key, "from_dispatch_to_plc_123", 20); m_status_from_dispatch_to_plc.m_year = (unsigned short)20; m_status_from_dispatch_to_plc.m_day = 20; m_status_from_dispatch_to_plc.m_heartbeat = 20; memcpy(m_send_buf_map[0].mp_buf_obverse, &m_request_from_dispatch_to_plc, m_send_buf_map[0].m_size); m_send_buf_map[0].set_communication_mode(Snap7_buf::ONCE_COMMUNICATION); memcpy(m_send_buf_map[1].mp_buf_obverse, &m_status_from_dispatch_to_plc, m_send_buf_map[1].m_size); m_send_buf_map[1].set_communication_mode(Snap7_buf::ONCE_COMMUNICATION); return Error_code::SUCCESS; }