123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #include "ResolvingMqtt.h"
- //int ResolvingMqtt::sumTest2(int dataA, int dataB)
- //{
- // return dataA+ dataB;
- //}
- #include <iostream>
- #include <sstream>
- typedef int BYTE;
- int htod(int x)
- {
- int Hex = x;
- int Dec = 0;
- for (int j = 0; j < 16; j++)
- {
- if (Hex & 0x0001)
- {
- Dec += pow(2, j);
- Hex >>= 1;
- }
- else
- {
- Dec += 0;
- Hex >>= 1;
- }
- }
- return Dec;
- }
- float Temp(int a, int b)
- {
- float t;
- int c;
- c = b * 100 + a;
- t = htod(c);
- float temp = 0.1;
- t = t * temp;
- return t;
- }
- float Hum(int a)
- {
- float h;
- int b;
- b = a;
- h = htod(b);
- float temp = 0.5;
- h = h * temp;
- return h;
- }
- void floatToBytesLittle(float value, char* cSendBuff, int pos)
- {
- short i = 0;
- float floatVariable = value;
- char* pdata = (char*)&floatVariable;
- for (i = 0; i < 4; i++)
- {
- cSendBuff[i + pos] = *pdata++;//float转BYTE
- std::cout << *pdata << std::endl;
- }
- }
- int sumTest2(BYTE* ptrArr, int arrLenght, int resolveType, char* data)
- {
- float temp, hum;
- int i = 0;
- char buf[10];
- for (i; i < arrLenght; i++)
- {
- std::cout << "数据:" << ptrArr[i] << " ";
- }
- i = 0;
- std::cout << std::endl;
- //协议 -- 1
- if (resolveType == 1)
- {
- while (i < arrLenght)
- {
- if (ptrArr[0] == 03)
- {
- i++;
- if (ptrArr[i] == 67)
- {
- BYTE z = ptrArr[++i];
- BYTE x = ptrArr[++i];
- temp = Temp(z, x); //处理温度数据
- floatToBytesLittle(temp, buf, 0);
- std::cout << temp << buf[0] << buf[1] << buf[2] << buf[3] << std::endl;
- }
- else
- {
- return -1;
- }
- }
- if (ptrArr[++i] == 04)
- {
- if (ptrArr[++i] == 68)
- {
- hum = Hum(ptrArr[++i]);//处理湿度数据
- }
- else
- {
- return -1;
- }
- }
- }
- }
- return 0;
- }
|