SOMS/test/MqttDll/ResolvingMqtt.cpp

116 lines
1.6 KiB
C++
Raw Permalink Normal View History

2024-07-15 10:31:26 +08:00
#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 << "<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>" << ptrArr[i] << " ";
}
i = 0;
std::cout << std::endl;
//Э<><D0AD> -- 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); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><C2B6><EFBFBD><EFBFBD><EFBFBD>
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]);//<2F><><EFBFBD><EFBFBD>ʪ<EFBFBD><CAAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
else
{
return -1;
}
}
}
}
return 0;
}