I have a device that outputs various sensor data, the guy that made the firmware sent me a "sample response" and what it means, basically it has a SOF hex key, a EOF hex key and an ESC key that is added XOR to avoid confusions when the same hex value needs to be "in the middle". Now from that gigangic 0x... string that is my "sample response" each "value" is represented by 4 bytes (8 chars) which is why I made a method in which it's adding each 8-chars into an array and removing it from the received response, each 8-byte set goes into an array and then gets "translated", however from what I was told every number should be translating roughly to 0.24xxxx So at the end I should end up with a 50-sensor array all between 0.2 and 0.3, however when I parse this data my output shows values ranging from 1.95E-37 to 265209.9 . . .which is like completely crazy.
Currently I know my code sucks and as I am thinking more about it I feel I am completely ignoring that ESC key that is there fo something, I assume I should be parsing each byte as it comes rather than "grouping it" first? but I am at a loss on how to even arrange the data.
readonly string SOF = "0x02";
readonly string EOF = "7E";
readonly string ESC = "2D";
private void testParsingBtn(object sender, EventArgs e)
{
string exResponse = "0x0200C800013E7F4A643E7F766E3E7F8DFC3E7F6C603E7F59DC3E7F60663E7F57903E7F5F823E7F502E3E7F60303E7F612C3E7F61683E7F43923E7F4ABE3E7F7CDA3E7F5D783D392F383E7F4AF43E7F68703E7F59BE3E7F531C3E7F60783E7F62A03E7F6C903E7F7A883E7F4D523E7F4E783E7F3A983E7F6FBA3E7F52AA3E7F6D983E7F6F543E7F6AE03D392A703E7F750C3E7F6C423E7F64A43E7F4F323E7F61863E7F60A83E7F711C3E7F81483E7F68E83E7F56163E7F732C3E7F6CA83E7F65823E7F72963E7F668A3E7F48C0C05C7E";
parseResponse(exResponse);
}
private void parseResponse (string receivedString)
{
String[] receivedBytes = new String[50];
if (receivedString.Substring(0,4) == SOF)
{
Console.WriteLine("Start of File (SOF) detected");
receivedString = receivedString.Remove(0, 4);
}
if (receivedString.Substring(receivedString.Length - 2, 2) == EOF)
{
Console.WriteLine("End of File (EOF) detected");
receivedString = receivedString.Remove(receivedString.Length-2, 2);
}
//Convert the Hex to decimal and set it to the probe array and update the GUI
for (int i = 0; i < receivedBytes.Length; i++)
{
receivedBytes[i] = receivedString.Substring(0, 8);
receivedString = receivedString.Remove(0, 8);
}
foreach (String str in receivedBytes)
{
Console.WriteLine(str);
}
for (int i = 0; i < probeArray.Length; i++)
{
byte[] arr = StringToByteArray(receivedBytes[i]);
probeArray[i].Temp = BitConverter.ToSingle(arr,0);
}
foreach (Probe probe in probeArray)
{
Console.WriteLine(probe.Temp);
}
updateTemps();
}
private static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
}