mdl.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "plt.h"
  2. #include "mdl.h"
  3. int sys_timer = 0;
  4. int canAddr = 1;
  5. int chanidx = 1;
  6. void mdl_sim()
  7. {
  8. while (chan_socketcan_rxrb_num_items(chanidx) > 0)
  9. {
  10. chan_socketcan_ringbuffer_element_t e;//
  11. if (chan_socketcan_rxrb_dequeue(chanidx, &e) > 0)
  12. {
  13. if(e.frame.can_id&0xffffffff == 0x04904000)
  14. {
  15. MDL.runMode = (e.frame.data[0] & 0x3); //0停机 1 制冷 2加热 3自动
  16. if (MDL.runMode > 0)
  17. {
  18. MDL.temp_setting = e.frame.data[4];
  19. }
  20. }
  21. }
  22. }
  23. if (sys_timer++ >= 100) // 10 means 1s
  24. {
  25. sys_timer = 0;
  26. //发送帧2
  27. chan_socketcan_ringbuffer_element_t send;//
  28. memset((void *)&send,0,sizeof(chan_socketcan_ringbuffer_element_t));
  29. send.frame.can_id = 0x18008040;
  30. send.frame.can_id |= CAN_EFF_FLAG;
  31. send.frame.can_dlc = 8; //数据长度
  32. send.frame.data[0] = (char)MDL.runMode;
  33. send.frame.data[1] = (char)MDL.out_water_temp;
  34. send.frame.data[2] = (char)MDL.in_water_temp;
  35. send.frame.data[3] = (char)MDL.ev_temp;
  36. send.frame.data[4] = (char)MDL.in_water_press;
  37. send.frame.data[5] = (char)MDL.out_water_press;
  38. send.frame.data[6] = 0;
  39. send.frame.data[7] = (char)(MDL.err_lvl<<6 & MDL.err_code);
  40. chan_lock(chanidx);
  41. chan_socketcan_txrb_queue(chanidx, send);
  42. chan_unlock(chanidx);
  43. //发送帧3
  44. memset((void *)&send,0,sizeof(chan_socketcan_ringbuffer_element_t));
  45. send.frame.can_id = 0x18018040;
  46. send.frame.can_id |= CAN_EFF_FLAG;
  47. send.frame.can_dlc = 8;
  48. send.frame.data[0] = (char)(MDL.comp_state | MDL.comp_heat_state<<1 | MDL.elec_heat_state<<2 | MDL.comp_state<<3 | MDL.fan1_state<<4 | MDL.fan2_state<<5 | MDL.fan3_state<<6);
  49. send.frame.data[1] = (char)MDL.comp_rpm/100;
  50. send.frame.data[2] = (char)MDL.pump_rpm/100;
  51. chan_lock(chanidx);
  52. chan_socketcan_txrb_queue(chanidx, send);
  53. chan_unlock(chanidx);
  54. //发送帧4
  55. memset((void *)&send,0,sizeof(chan_socketcan_ringbuffer_element_t));
  56. send.frame.can_id = 0x18068040;
  57. send.frame.can_id |= CAN_EFF_FLAG;
  58. send.frame.can_dlc = 8;
  59. send.frame.data[0] = (char)(MDL.in_out_water_press_sensor_err | MDL.ov_err<<1 | MDL.uv_err<<2 | MDL.lack_water_err<<3 | MDL.pump_err<<4 | MDL.pump_err_lock<<5);
  60. chan_lock(chanidx);
  61. chan_socketcan_txrb_queue(chanidx, send);
  62. chan_unlock(chanidx);
  63. //发送帧5
  64. memset((void *)&send,0,sizeof(chan_socketcan_ringbuffer_element_t));
  65. send.frame.can_id = 0x18078040;
  66. send.frame.can_id |= CAN_EFF_FLAG;
  67. send.frame.can_dlc = 8;
  68. send.frame.data[0] = (char)(MDL.out_water_press_sensor_err | MDL.in_water_press_sensor_err<<1 | MDL.up_sensor_err<<2
  69. | MDL.out_water_temp_sensor_err<<3 | MDL.sys_low_pressure_err<<4 | MDL.in_air_over_temp_err<<5
  70. | MDL.out_air_over_temp_err<<6 | MDL.can_comm_err<<7);
  71. send.frame.data[1] = (char)(MDL.in_air_temp_sensor_err | MDL.cooling_sys_err<<1 | MDL.up_sensor_err<<2
  72. | MDL.comp_VFD_ov_err<<3 | MDL.comp_VFD_uv_err<<4 | MDL.comp_VFD_oc_err<<5
  73. | MDL.comp_VFD_ot_err<<6 | MDL.comp_VFD_comm_err<<7);
  74. send.frame.data[2] = (char)(MDL.comp_VFD_miss_phrase_err | MDL.comp_VFD_others_err<<1 | MDL.pump_press_diff_low_err<<2
  75. | MDL.out_air_over_temp_lock<<3 | MDL.sys_high_pressure_lock<<4 | MDL.sys_low_pressure_lock<<5);
  76. send.frame.data[3] = (char)(MDL.comp_VFD_over_current_lock | MDL.comp_VFD_over_voltage_lock<<1 | MDL.comp_VFD_under_voltage_lock<<2
  77. | MDL.comp_VFD_over_temp_lock<<3 | MDL.comp_VFD_comm_lock<<4 | MDL.comp_VFD_miss_phrase_lock<<5
  78. | MDL.comp_VFD_others_lock<<6);
  79. chan_lock(chanidx);
  80. chan_socketcan_txrb_queue(chanidx, send);
  81. chan_unlock(chanidx);
  82. //发送帧6
  83. send.frame.can_id = 0x18088040;
  84. send.frame.can_id |= CAN_EFF_FLAG;
  85. send.frame.can_dlc = 8;
  86. send.frame.data[0] = (char)(MDL.high_press_sensor_err | MDL.env_temp_sensor_err<<1 | MDL.in_water_temp_sensor_err<<2 \
  87. | MDL.out_air_temp_sensor_err<<3 | MDL.condensate_sensor_err<<4 | MDL.unexpected_power_off_err<<5 \
  88. | MDL.out_water_low_temp_err<<6 | MDL.out_water_high_temp_err<<7);
  89. send.frame.data[1] = (char) MDL.out_water_over_press_err;
  90. chan_lock(chanidx);
  91. chan_socketcan_txrb_queue(chanidx, send);
  92. chan_unlock(chanidx);
  93. }
  94. }
  95. static void *mdl_thrd_main(void *param)
  96. {
  97. while (1)
  98. {
  99. mdl_sim();
  100. usleep(10000); /* 10ms */
  101. }
  102. return NULL;
  103. }
  104. int mdl_init()
  105. {
  106. log_dbg("%s, ++", __func__);
  107. int ret = 0;
  108. pthread_t xthrd;
  109. if (pthread_create(&xthrd, NULL, mdl_thrd_main, NULL) != 0)
  110. {
  111. log_dbg("%s, pthread_create fail", __func__);
  112. ret = -1;
  113. }
  114. log_dbg("%s, --, ret: %d", __func__, ret);
  115. return ret;
  116. }