I am using Arduino Uno+ MAX485 module to communicate with an digital power meter.
The communication is successfully established, but I have a problem with the register value.
For example, when I want to read Voltage, the Voltage data is actually store in 2 registers(4 bytes). After some research I think should be IEEE 754 format. What I need to do is just to translate it into float by using the following code:
float y = (float)&x;
However, my problem is, since my acquired data from meter is in unsigned int and from two separated registers. How do I combine the two value from two registers and convert them into HEX and employ the above function?
Thanks for your advice, I have tried pointer method but the result always shows 0.
Should I doubt the two register is not in IEEE 754 format?
Below is the data from Serial Monitor:
" Phase 1 Current:
regs[9]: 0
regs[10]: 16120 <----The actual current shows on power meter is 161.20Amp
d=16120
y=0.00
"
Someone suggested me to times a 0,01 to the regs[10] and ignore the regs[9] data. This method is acceptable if I eventually cannot solve this problem.
The code has attached below:
//regs[9] and regs1[10] is the Phase 1 Current data from two of the registers of the Power Meter.
Serial.println("Phase 1 Current:");
Serial.print("regs[9]: ");
Serial.println(regs[9]); //L1 Current
Serial.print("regs[10]: ");
Serial.println(regs[10]); //L1 Current
int c = regs[9]<<16;
uint32_t d = c + regs[10]; //bitshift and combine regs[9] and regs[10]
Serial.print("d=");
Serial.println(d);
float y = *(float*)&d;
Serial.print("y=");
Serial.println (y, 2);
delay(1000);
int c = regs[9]<<16;
uint32_t d = c + regs[10]; //bitshift and combine regs[9] and regs[10]
Should I ignore the regs[9] and take only regs[10] value?
Does this mean that this is actually doesn't matter with IEEE 754 standard?
Attach the IEEE 754 format:
[color=#222222]An IEEE 754 [i]format[/i] is a "set of representations of numerical values and symbols". A format may also include how the set is encoded.[/color]
[color=#222222]A format comprises:[/color]
Finite numbers, which may be either base 2 (binary) or base 10 (decimal). Each finite number is described by three integers: s = a sign (zero or one), c = a significand (or 'coefficient'), q = an exponent. The numerical value of a finite number is
(−1)s × c × bq
where b is the base (2 or 10), also called radix. For example, if the base is 10, the sign is 1 (indicating negative), the significand is 12345, and the exponent is −3, then the value of the number is (−1)1 × 12345 × 10−3 = −1 × 12345 × .001 = −12.345.Two infinities: +∞ and −∞.Two kinds of NaN: a quiet NaN (qNaN) and a signaling NaN (sNaN). A NaN may carry a payload that is intended for diagnostic information indicating the source of the NaN. The sign of a NaN has no meaning, but it may be predictable in some circumstances.