udpcan.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <WinSock2.h>
  2. #include "./include/EgoInterface.h"
  3. #include "EgoWindow.h"
  4. #include "protocol.pb.h"
  5. #include "udpcan.h"
  6. #include "../common/iobuffer.h"
  7. #include "protocol.pb.h"
  8. //#include "PCANBasic.h"
  9. int16_t ChangeINT(unsigned char h, unsigned char l)
  10. {
  11. int16_t hi = (int16_t)(h & 0x00FF);
  12. int16_t low = (int16_t)(l & 0x00FF);
  13. return (hi << 8) | low;
  14. }
  15. CUdpSensor::CUdpSensor(CMessageQueue* q)
  16. {
  17. _window = static_cast<CEgoWindow*>(q);
  18. assert(_window != nullptr);
  19. }
  20. void CUdpSensor::Start()
  21. {
  22. /*cannet_frame frame;
  23. memset(&frame, 0, sizeof(frame));
  24. frame.canid = htonl(0x0601);
  25. frame.dlc = 3;
  26. frame.data[0] = 0xb3;
  27. frame.data[1] = 0x10;
  28. frame.data[2] = 0xff;
  29. CIOBuffer buffer;
  30. memcpy(buffer.Buffer, &frame, sizeof(frame));
  31. buffer.Length = sizeof(frame);*/
  32. //_socket->Write(&buffer);
  33. }
  34. void CUdpSensor::Stop()
  35. {
  36. }
  37. void CUdpSensor::SetSensorSocket(SensorSocket<CUdpSensor>* can)
  38. {
  39. _socket = can;
  40. }
  41. void CUdpSensor::Notify(int8_t* buffer, int32_t size)
  42. {
  43. int16_t m_cmd = ChangeINT(buffer[1], buffer[0]);
  44. if (m_cmd == RemoNet::CC_STATE)
  45. {
  46. RemoNet::State msg;
  47. msg.ParseFromArray(buffer + 4, size - 4);
  48. FeedData data;
  49. data.brake_pressure = msg.brake_pressure();//制动气压
  50. data.engine_rpm = msg.engine_rpm();//发动机转速
  51. data.engine_pressure = msg.engine_pressure();//发动机压力
  52. data.gearbox_oil_pressure = msg.gearbox_oil_pressure();//变速箱油压
  53. data.gearbox_oil_temp = msg.gearbox_oil_temp();//变速箱油温
  54. data.speed = msg.speed();//行驶速度
  55. data.cold_water = msg.cold_water();//发动机水温
  56. data.steer_angle = msg.steer_angle();//车辆姿态???原来是轮子现在定义为铰链
  57. data.left_lock_status = msg.left_lock();
  58. data.right_lock_status = msg.right_lock();
  59. data.work_pressure = msg.work_pressure();
  60. //data.gear = msg.gear();
  61. //发送给qt的信息以下是大冶增加的:
  62. data.engine_time = msg.engine_time();//发动机工作小时
  63. data.wire_sensor_l = msg.wire_sensor_l();//左支腿高度
  64. data.wire_sensor_r = msg.wire_sensor_r();//右支腿高度
  65. //OutputDebugStringA(("===================打印左支腿高度 " + std::to_string(data.wire_sensor_l) + "============\n").c_str());
  66. //OutputDebugStringA(("===================打印右支腿高度 " + std::to_string(data.wire_sensor_r) + "============\n").c_str());
  67. data.fuel_level = msg.fuel_level();//燃油油位油量
  68. //data.lock_switch_l = msg.lock_switch_l();//锁钩到位开关L
  69. //data.lock_switch_r = msg.lock_switch_r();//锁钩到位开关R
  70. //data.tip_limit = msg.tip_limit();//倾翻限位
  71. data.lock_switch_l = msg.outgrigger_l();//左支腿伸到位开关
  72. data.lock_switch_r = msg.outgrigger_r();//右支腿伸到位开关
  73. data.tip_limit = msg.tip_pressure();//倾翻压力开关
  74. data.turn_left = msg.turn_left();//左转
  75. data.turn_right = msg.turn_right();//右转
  76. data.hight_beam = msg.hight_beam();//远光灯
  77. data.low_beam = msg.low_beam();//近光灯
  78. data.parking = msg.parking();//驻车
  79. data.brake = msg.brake();//刹车
  80. data.back_car = msg.back_car();//倒车
  81. data.front_work_lamp = msg.front_work_lamp();//前工作灯
  82. data.rear_work_lamp = msg.rear_work_lamp();//后工作灯
  83. data.cargo_weight = msg.cargo_weight();//渣包重量
  84. data.system_vol = msg.system_vol();//系统电压
  85. data.f_gear = msg.f_gear();//档位信息1F 2R 3N
  86. data.gear_1 = msg.gear_1();//档位1234
  87. //发送信息给PCAN(下位机传的0x280透传)
  88. TPCANMsg* CANMsg = (TPCANMsg*)malloc(sizeof(TPCANMsg));
  89. CANMsg->ID = 0x280;
  90. CANMsg->MSGTYPE = PCAN_MESSAGE_STANDARD;
  91. CANMsg->LEN = msg.error_buff().size();
  92. memcpy(CANMsg->DATA, data.warningData, CANMsg->LEN);
  93. //SendCANMessage(CANMsg);
  94. //_car->SendCanDate(CANMsg);
  95. _window->WriteStatue(CANMsg);
  96. free(CANMsg);
  97. for (int i = 0; i < msg.error_buff().size(); ++i)//报警信息
  98. {
  99. data.warningData[i] = msg.error_buff()[i];
  100. }
  101. //将报警信息解读出来赋值给data中的变量
  102. data.low_oil_pressure_alarm = (data.warningData[0] & 0x1) == 0x1 ? 1 : 0;//机油压力低报警
  103. data.engine_water_temperature_alarm = (data.warningData[0] & 0x2) == 0x2 ? 1 : 0;//发动机水温高报警
  104. data.high_engine_oil_temperature_alarm = (data.warningData[0] & 0x4) == 0x4 ? 1 : 0;//发动机油温高报警
  105. data.engine_water_level_low_alarm = (data.warningData[0] & 0x8) == 0x8 ? 1 : 0;//发动机水位低报警
  106. data.low_fuel_oil_alarm = (data.warningData[0] & 0x10) == 0x10 ? 1 : 0;//燃油油位低报警
  107. data.return_oil_filter_blocked_alarm = (data.warningData[0] & 0x20) == 0x20 ? 1 : 0;//回油滤网堵塞报警
  108. data.oil_filter_blocked_alarm = (data.warningData[0] & 0x40) == 0x40 ? 1 : 0;//吸油滤网堵塞报警
  109. data.control_screen_blockage_alarm = (data.warningData[0] & 0x80) == 0x80 ? 1 : 0;//控制滤网堵塞报警
  110. data.emergency_filter_blocking_alarm = (data.warningData[1] & 0x1) == 0x1 ? 1 : 0;//应急滤网堵塞报警
  111. data.cooling_filter_blocked_alarm = (data.warningData[1] & 0x2) == 0x2 ? 1 : 0;//冷却滤网堵塞报警
  112. data.electric_pump_screen_blocked_alarm = (data.warningData[1] & 0x4) == 0x4 ? 1 : 0;//电泵滤网堵塞报警
  113. data.brake_filter_blocked_alarm = (data.warningData[1] & 0x8) == 0x8 ? 1 : 0;//制动滤网堵塞报警
  114. data.variable_speed_filter_blocked_alarm = (data.warningData[1] & 0x10) == 0x10 ? 1 : 0;//变速滤网堵塞报警
  115. data.low_voltage_alarm = (data.warningData[1] & 0x20) == 0x20 ? 1 : 0;//电压低报警
  116. data.high_transmission_oil_temperature = (data.warningData[2] & 0x1) == 0x1 ? 1 : 0;//变速箱油温高
  117. data.transmission_oil_pressure_is_low = (data.warningData[2] & 0x2) == 0x2 ? 1 : 0;//变速箱油压低
  118. data.oil_temperature_sensor_1_is_faulty = (data.warningData[2] & 0x4) == 0x4 ? 1 : 0; //油温传感器1故障
  119. data.oil_temperature_sensor_2_is_faulty = (data.warningData[2] & 0x8) == 0x8 ? 1 : 0;//油温传感器2故障
  120. data.oil_level_sensor_is_faulty = (data.warningData[2] & 0x10) == 0x10 ? 1 : 0;//油位传感器故障
  121. data.front_throttle_fault = (data.warningData[2] & 0x20) == 0x20 ? 1 : 0;//前油门故障
  122. data.rear_throttle_fault = (data.warningData[2] & 0x40) == 0x40 ? 1 : 0;//后油门故障
  123. data.handle_of_the_upper_arm_is_faulty = (data.warningData[2] & 0x80) == 0x80 ? 1 : 0;//大臂手柄故障
  124. data.left_leg_handle_is_faulty = (data.warningData[3] & 0x8) == 0x8 ? 1 : 0;//左支腿手柄故障
  125. data.right_leg_handle_is_faulty = (data.warningData[3] & 0x10) == 0x10 ? 1 : 0;//右支腿手柄故障
  126. data.engine_bus_fault = (data.warningData[3] & 0x20) == 0x20 ? 1 : 0;//发动机总线故障
  127. data.transmission_bus_fault = (data.warningData[3] & 0x40) == 0x40 ? 1 : 0;//变速箱总线故障
  128. data.remote_control_bus_fault = (data.warningData[3] & 0x80) == 0x80 ? 1 : 0;//遥控器总线故
  129. data.rod_chamber_pressure_sensor_is_faulty = (data.warningData[4] & 0x1) == 0x1 ? 1 : 0;//有杆腔压力传感器故障
  130. data.no_rod_chamber_pressure_sensor_is_faulty = (data.warningData[4] & 0x2) == 0x2 ? 1 : 0;//无杆腔压力传感器故障
  131. data.brake_pressure_sensor_is_faulty = (data.warningData[4] & 0x4) == 0x4 ? 1 : 0;//制动压力传感器故障
  132. data.system_pressure_sensor_is_faulty = (data.warningData[4] & 0x8) == 0x8 ? 1 : 0;//系统压力传感器故障
  133. data.control_pressure_sensor_is_faulty = (data.warningData[4] & 0x10) == 0x10 ? 1 : 0;//控制压力传感器故障
  134. data.steering_pressure_sensor_is_faulty = (data.warningData[4] & 0x20) == 0x20 ? 1 : 0;//转向压力传感器故障
  135. data.pressure_sensor_of_the_lower_arm_is_faulty = (data.warningData[4] & 0x40) == 0x40 ? 1 : 0;//小臂压力传感器故障
  136. data.locking_pin_displacement_sensor_L_is_faulty = (data.warningData[5] & 0x1) == 0x1 ? 1 : 0;//锁销位移传感器L故障
  137. data.locking_pin_displacement_sensor_R_is_faulty = (data.warningData[5] & 0x2) == 0x2 ? 1 : 0;//锁销位移传感器R故障
  138. data.leg_displacement_sensor_L_is_faulty = (data.warningData[5] & 0x4) == 0x4 ? 1 : 0;//支腿位移传感器L故障
  139. data.leg_displacement_sensor_R_is_faulty = (data.warningData[5] & 0x8) == 0x8 ? 1 : 0;//支腿位移传感器R故障
  140. data.steering_encoder_failure = (data.warningData[5] & 0x10) == 0x10 ? 1 : 0;//转向编码器故障
  141. _window->DisplayStatue(data);
  142. }
  143. else if (m_cmd == RemoNet::CC_Ping)
  144. {
  145. for (int i = 0; i < 10; i++);
  146. RemoNet::CCPing Req;
  147. Req.ParseFromArray(buffer + 4, size - 4);
  148. int64_t tick = GetTickCount64();
  149. int32_t diff = tick - Req.tick();
  150. double temp = Req.temp();
  151. _window->DisplayPing(diff, temp);
  152. }
  153. else if (m_cmd == RemoNet::CC_Radar)
  154. {
  155. RemoNet::CCRadarMessage ReqRadar;
  156. ReqRadar.ParseFromArray(buffer + 4, size - 4);
  157. _window->DisplayRadar(ReqRadar.radar0(), ReqRadar.radar1(), ReqRadar.radar2(), ReqRadar.radar3(), ReqRadar.radar4(), ReqRadar.radar5(), ReqRadar.radar6(), ReqRadar.radar7(), ReqRadar.radar8());
  158. }
  159. }