123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- //
- // 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()
- {
- int t_index = 0;
- std::vector<Snap7_buf::Variable_information> t_variable_information_vector;
- //往map通信缓存里面添加所需要的buf
- std::unique_lock<std::mutex> t_lock1(m_receive_buf_lock);
- std::unique_lock<std::mutex> t_lock2(m_send_buf_lock);
- t_index = 0;
- t_variable_information_vector.clear();
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved0", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_command_flag", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_task_status", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved3_11", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 9 });
- t_index += sizeof(unsigned char)*9;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_block_id", typeid(unsigned short).name(), t_index,sizeof(unsigned short), 1 });
- t_index += sizeof(unsigned short)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_floor_id", typeid(unsigned short).name(), t_index,sizeof(unsigned short), 1 });
- t_index += sizeof(unsigned short)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_parkspace_id", typeid(unsigned short).name(), t_index,sizeof(unsigned short), 1 });
- t_index += sizeof(unsigned short)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_command_key", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 256 });
- t_index += sizeof(unsigned char)*256;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_is_entrance_flag", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved275_289", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 15 });
- t_index += sizeof(unsigned char)*15;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_center_x", typeid(float).name(), t_index,sizeof(float), 1 });
- t_index += sizeof(float)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_center_y", typeid(float).name(), t_index,sizeof(float), 1 });
- t_index += sizeof(float)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_car_angle", typeid(float).name(), t_index,sizeof(float), 1 });
- t_index += sizeof(float)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_car_length", typeid(float).name(), t_index,sizeof(float), 1 });
- t_index += sizeof(float)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_car_width", typeid(float).name(), t_index,sizeof(float), 1 });
- t_index += sizeof(float)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_car_height", typeid(float).name(), t_index,sizeof(float), 1 });
- t_index += sizeof(float)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_wheel_base", typeid(float).name(), t_index,sizeof(float), 1 });
- t_index += sizeof(float)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_wheel_width", typeid(float).name(), t_index,sizeof(float), 1 });
- t_index += sizeof(float)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_voucher_number", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 256 });
- t_index += sizeof(unsigned char)*256;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_plate_number", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 512 });
- t_index += sizeof(unsigned char)*512;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_phone_number", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 256 });
- t_index += sizeof(unsigned char)*256;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved1346_1377", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 32 });
- t_index += sizeof(unsigned char)*32;
- 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);
- m_receive_buf_map[0] = t_response;
- 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);
- m_send_buf_map[0] = t_request;
- t_index = 0;
- t_variable_information_vector.clear();
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_heartbeat", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_check_flag", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved2_9", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 8 });
- t_index += sizeof(unsigned char)*8;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_year", typeid(unsigned short).name(), t_index,sizeof(unsigned short), 1 });
- t_index += sizeof(unsigned short)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_month", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_day", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_weekday", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_hour", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_minute", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_second", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_nanosecond", typeid(unsigned int).name(), t_index,sizeof(unsigned int), 1 });
- t_index += sizeof(unsigned int)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved22_29", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 8 });
- t_index += sizeof(unsigned char)*8;
- 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);
- m_send_buf_map[1] = t_dispatch_status;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_elevator_information", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved31", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_elevator_coordinates", typeid(double).name(), t_index,sizeof(double), 1 });
- t_index += sizeof(double)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved40_73", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 34 });
- t_index += sizeof(unsigned char)*34;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_carrier_information", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved75", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 1 });
- t_index += sizeof(unsigned char)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_carrier_coordinates", typeid(double).name(), t_index,sizeof(double), 1 });
- t_index += sizeof(double)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_carrier_angle", typeid(double).name(), t_index,sizeof(double), 1 });
- t_index += sizeof(double)*1;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_reserved92_125", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 34 });
- t_index += sizeof(unsigned char)*34;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_carrier_error_code", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 50 });
- t_index += sizeof(unsigned char)*50;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_carrier_error_string", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 512 });
- t_index += sizeof(unsigned char)*512;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_elevator_error_code", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 50 });
- t_index += sizeof(unsigned char)*50;
- t_variable_information_vector.push_back(Snap7_buf::Variable_information{"m_elevator_error_string", typeid(unsigned char).name(), t_index,sizeof(unsigned char), 512 });
- t_index += sizeof(unsigned char)*512;
- 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);
- m_receive_buf_map[1] = t_plc_status;
- return Snap7_communication_base::communication_init();
- }
- //反初始化 通信 模块。
- Error_manager Dispatch_communication::communication_uninit()
- {
- return Snap7_communication_base::communication_uninit();
- }
- std::mutex * Dispatch_communication::get_data_lock()
- {
- return &m_data_lock;
- }
- Dispatch_communication::Request_from_dispatch_to_plc * Dispatch_communication::get_request_from_dispatch_to_plc()
- {
- return &m_request_from_dispatch_to_plc;
- }
- Dispatch_communication::Response_from_plc_to_dispatch * Dispatch_communication::get_response_from_plc_to_dispatch()
- {
- return &m_response_from_plc_to_dispatch;
- }
- Dispatch_communication::Status_from_dispatch_to_plc * Dispatch_communication::get_status_from_dispatch_to_plc()
- {
- return &m_status_from_dispatch_to_plc;
- }
- Dispatch_communication::Status_from_plc_to_dispatch * Dispatch_communication::get_status_from_plc_to_dispatch()
- {
- return &m_status_from_plc_to_dispatch;
- }
- //更新数据
- Error_manager Dispatch_communication::updata_receive_buf()
- {
- std::unique_lock<std::mutex> t_lock1(m_receive_buf_lock);
- std::unique_lock<std::mutex> 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<std::mutex> t_lock1(m_send_buf_lock);
- std::unique_lock<std::mutex> 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;
- }
|