This entry has been published on 2016-12-07 and may be out of date.
Last Updated on 2016-12-07.
[:en]
Using a KNX server like eibd with PHP in combination, you might come to a point where you want to convert the DPT/EIS encoded telegram values to decimal format.
For some data types, it works by simply using PHP hexbin(), but it does not for 2-byte floating point values.
In many forum threads the following sample is used:
$first_byte = decbin(hexdec(substr($value, 0, 2))); $second_byte = decbin(hexdec(substr($value, 2, 2))); while (strlen($first_byte) != 8) { $first_byte= "0".$first_byte; } while (strlen($second_byte) != 8) { $second_byte= "0".$second_byte; } $tmpdata = $first_byte.$second_byte; $eib_base_value=bindec(substr($tmpdata,5,15)); $eib_exponent=bindec(substr($tmpdata,1,4)); return (0.01*$eib_base_value)*pow(2,$eib_exponent);
BUT it converts negative values wrong! This can lead to e.g. wrong temperature values which might affect the heating control.
Use this snippet instead:
$intValue = hexdec($value); $floatValue = ($intValue & 0x07ff); if (($intValue & 0x08000) != 0) { $floatValue = $floatValue | 0xfffffffffffff800; $floatValue = $floatValue *-1; } $floatValue = $floatValue << (($intValue & 0x07800) >> 11); if (($intValue & 0x08000) != 0) { $floatValue = $floatValue * -1; } return $floatValue / 100;
Reference[:]