Groove's code needs some additional checking to make sure that the character is in the range '0' to '9' or 'A' to 'F' (or 'a' to 'f'). Conversion of a string containing 'T' is sure to result in incorrect results.
Not my code, just the original made more legible.
Thanks for all the input, but I did not have any luck with the suggestions thus far.
Someone commented that the data was NOT Big Endian.
Perhaps someone can explain why it is not Big Endian.
Quote from the developer "This data is given as IEEE-754 32-bit float representation, with the MSB received first (Big Endian). A good reference for converting the bytes to the float is available at http://babbage.cs.qc.edu/IEEE-754/32bit.html"
Here is my Arduino code:
#include <NewSoftSerial.h>
NewSoftSerial mySerial(2,3);
int inchar,count;
unsigned long sResult;
byte bh,bl;
void setup(){
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
while(mySerial.available()>0) { // Data from mySerial Serial Port
inchar = mySerial.read();
if(inchar == 4 ) { // check to see if the header is correct
count=0;
while(mySerial.available() > 0) {
count ++;
inchar = mySerial.read();
if(count == 16 and inchar == 2) {
bh = mySerial.read(); // bh = 42
bl = mySerial.read(); // bl = A6
sResult = (int)word (bh,bl); // sResult = 42A6
Serial.println(sResult,HEX);
sResult = sResult << 16; // convert to Big Endian
sResult = ((float)&sResult);
Serial.println(sResult); // returns 83
}
}
}
}
}
This method appears to work, until receiving a value of C2300000
which should return a "-44" value.
I get a value of "4294967252" instead.
If i use the online calculator @ http://babbage.cs.qc.edu/IEEE-754/32bit.html and enter the value C2300000 I get the correct results.
Why is this not working with the about code? I can only assume because it is a negative number, but no idea how to correct it.
Loren,
your code is a big mess with many potential problems. You should fix them. Best start by going through the code and try to understand what you're doing instead of randomly pasting bits and pieces like Dr. Frankenstein.
The problem you mention last is yet another consequence of the lack of understanding. The result you get is absolutely correct and you get exactly what you ordered.
unsigned long sResult;
means, that sResult will not yield negative values. What a surprise! That's what "unsigned" means. Remove "unsigned" in the code line above and you'll get -44 instead of 4294967252.
Korman
Removing the "unsigned" doesn't fix the problem. returns "0" with a negative number.