ResolvingMqtt.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "ResolvingMqtt.h"
  2. //int ResolvingMqtt::sumTest2(int dataA, int dataB)
  3. //{
  4. // return dataA+ dataB;
  5. //}
  6. #include <iostream>
  7. #include <sstream>
  8. typedef int BYTE;
  9. int htod(int x)
  10. {
  11. int Hex = x;
  12. int Dec = 0;
  13. for (int j = 0; j < 16; j++)
  14. {
  15. if (Hex & 0x0001)
  16. {
  17. Dec += pow(2, j);
  18. Hex >>= 1;
  19. }
  20. else
  21. {
  22. Dec += 0;
  23. Hex >>= 1;
  24. }
  25. }
  26. return Dec;
  27. }
  28. float Temp(int a, int b)
  29. {
  30. float t;
  31. int c;
  32. c = b * 100 + a;
  33. t = htod(c);
  34. float temp = 0.1;
  35. t = t * temp;
  36. return t;
  37. }
  38. float Hum(int a)
  39. {
  40. float h;
  41. int b;
  42. b = a;
  43. h = htod(b);
  44. float temp = 0.5;
  45. h = h * temp;
  46. return h;
  47. }
  48. void floatToBytesLittle(float value, char* cSendBuff, int pos)
  49. {
  50. short i = 0;
  51. float floatVariable = value;
  52. char* pdata = (char*)&floatVariable;
  53. for (i = 0; i < 4; i++)
  54. {
  55. cSendBuff[i + pos] = *pdata++;//float转BYTE
  56. std::cout << *pdata << std::endl;
  57. }
  58. }
  59. int sumTest2(BYTE* ptrArr, int arrLenght, int resolveType, char* data)
  60. {
  61. float temp, hum;
  62. int i = 0;
  63. char buf[10];
  64. for (i; i < arrLenght; i++)
  65. {
  66. std::cout << "数据:" << ptrArr[i] << " ";
  67. }
  68. i = 0;
  69. std::cout << std::endl;
  70. //协议 -- 1
  71. if (resolveType == 1)
  72. {
  73. while (i < arrLenght)
  74. {
  75. if (ptrArr[0] == 03)
  76. {
  77. i++;
  78. if (ptrArr[i] == 67)
  79. {
  80. BYTE z = ptrArr[++i];
  81. BYTE x = ptrArr[++i];
  82. temp = Temp(z, x); //处理温度数据
  83. floatToBytesLittle(temp, buf, 0);
  84. std::cout << temp << buf[0] << buf[1] << buf[2] << buf[3] << std::endl;
  85. }
  86. else
  87. {
  88. return -1;
  89. }
  90. }
  91. if (ptrArr[++i] == 04)
  92. {
  93. if (ptrArr[++i] == 68)
  94. {
  95. hum = Hum(ptrArr[++i]);//处理湿度数据
  96. }
  97. else
  98. {
  99. return -1;
  100. }
  101. }
  102. }
  103. }
  104. return 0;
  105. }