tem.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "tem.h"
  2. #include "usart.h"
  3. Tem_Inf tem_inf[SENSOR_DEEP];
  4. //extern void Pressure_state(uint16_t CangNum);
  5. void Tem_Init(void)
  6. {
  7. }
  8. uint8_t Parse_Tem(uint8_t* data, Tem_Tpye type)
  9. {
  10. uint8_t cang_id = data[0] - TEM_STARTADDR;
  11. uint8_t error = 0;
  12. uint16_t ModbusCRC;
  13. uint16_t ADC_Value1, ADC_Value2;
  14. float voltage1, voltage2;
  15. uint8_t i=0;
  16. typedef union{
  17. float value;
  18. uint8_t arr[4];
  19. }Hex_to_float;
  20. static Hex_to_float hex_to_float;
  21. //tem_inf[cang_id].RTData_Num = 1;//三点测温
  22. //tem_inf[cang_id].ErrorCnt = 0;
  23. if(Tem_FST100_611 == type){ //一次获取两个仓的温度数据
  24. ModbusCRC = data[7]<<8;
  25. ModbusCRC |= data[8];
  26. if(data[1]!=0x03) //校验读写属性
  27. {
  28. error = 1;
  29. }
  30. else if(data[2] != 0x04) //校验数据长度
  31. {
  32. error = 1;
  33. }
  34. else if(ModbusCRC != LIB_CRC_MODBUS(data,7)) //校验CRC
  35. {
  36. error = 1;
  37. }
  38. if(0 == error){
  39. tem_inf[cang_id].RTData_Num = 1;
  40. tem_inf[cang_id].ErrorCnt = 0;
  41. tem_inf[cang_id].Error = 0;
  42. tem_inf[cang_id+1].RTData_Num = 1;
  43. tem_inf[cang_id+1].ErrorCnt = 0;
  44. tem_inf[cang_id+1].Error = 0;
  45. ADC_Value1 = data[3]<<8|data[4];
  46. ADC_Value2 = data[5]<<8|data[6];
  47. voltage1 = ADC_Value1*(1.0)*5/4096; // 0-5V 12bit
  48. voltage2 = ADC_Value2*(1.0)*5/4096;
  49. tem_inf[cang_id].temperature1 = (voltage1-1.5)*(120+40)/(4.5-0.5);
  50. tem_inf[cang_id+1].temperature1 = (voltage2-1.5)*(120+40)/(4.5-0.5);
  51. }
  52. }else if(Tem_HUATIAN_THREE == type){
  53. if(data[1]!=0x03) //校验读写属性
  54. {
  55. error = 1;
  56. }
  57. else if(data[2] != 0x02) //校验数据长度
  58. {
  59. error = 1;
  60. }
  61. else if(ModbusCRC != LIB_CRC_MODBUS(data,5)) //校验CRC
  62. {
  63. error = 1;
  64. }
  65. if(0 == error){
  66. //do nothing
  67. }
  68. }else if(Tem_FST100_1007 == type){
  69. ModbusCRC = data[7]<<8;
  70. ModbusCRC |= data[8];
  71. if(data[1]!=0x03) //校验读写属性
  72. {
  73. error = 1;
  74. }
  75. else if(data[2] != 0x04) //校验数据长度
  76. {
  77. error = 1;
  78. }
  79. else if(ModbusCRC != LIB_CRC_MODBUS(data,7)) //校验CRC
  80. {
  81. error = 1;
  82. }
  83. if(0 == error){
  84. tem_inf[cang_id].ErrorCnt = 0;
  85. tem_inf[cang_id].Error = 0;
  86. tem_inf[cang_id].RTData_Num = 1;
  87. hex_to_float.arr[0] = data[6];
  88. hex_to_float.arr[1] = data[5];
  89. hex_to_float.arr[2] = data[4];
  90. hex_to_float.arr[3] = data[3];
  91. tem_inf[cang_id].temperature1 = hex_to_float.value;
  92. }else{
  93. Tem_Error(data[0]);
  94. }
  95. }
  96. return error;
  97. }
  98. void Tem_Error(uint8_t addr)
  99. {
  100. uint8_t id = addr - TEM_STARTADDR;
  101. Cang_Inf* pcang = &cang_inf;
  102. if(Tem_FST100_611 == pcang->Temperture){
  103. tem_inf[id].ErrorCnt++;
  104. tem_inf[id+1].ErrorCnt++;
  105. if (tem_inf[id].ErrorCnt > pcang->sensorBusMaxReTry)
  106. {
  107. tem_inf[id].ErrorCnt = pcang->sensorBusMaxReTry + 1;
  108. tem_inf[id].Error = 1;
  109. tem_inf[id+1].ErrorCnt = pcang->sensorBusMaxReTry + 1;
  110. tem_inf[id+1].Error = 1;
  111. }
  112. }
  113. }