如何将modbus数据数组的数据类型转换为REAL数据类型

How to covert data type of mobus data array into REAL datatype

我正在研究 mod-总线寄存器。我是这个领域的新手,所以我面临着一些问题。 我已经从 mod-bus 获取数组中的数据,它正在将我的值显示为 int,但我想要真实数据类型的数据。 等待您的友好回应。 P.S: 请忽略我的错误

// for automatically refreshing the page every one second
header('Refresh: 1'); 
//setting the time zone and getting the date and time
$timezone = "Asia/Calcutta";
if(function_exists('date_default_timezone_set')){
   date_default_timezone_set($timezone);
}
echo date('d-m-Y'). "</br>";
echo date('H:i:s'). "</br>";
//reference to ModbusMaster.php file where the modbus php protocol is defined
require_once dirname(__FILE__) . '/phpmodbus/Phpmodbus/ModbusMaster.php';
// Create Modbus object
$modbus = new ModbusMaster("192.168.1.49", "TCP");

//Energy Meter
// FC3 = Function Code 3 to read holding registers
/*Setting device ID = 5, Starting address as 100 and 
  number of registers to be read as 120
*/
try {
    // FC 3
    $recData = $modbus->readMultipleRegisters(2, 4000, 6);
}
catch (Exception $e) {
    // Print error information if any
    echo $modbus;
    echo $e;
    exit;
}

// Print status information
echo "</br>Status:</br>" . $modbus;

// Conversion
echo "<h2>EN8400</h2>\n";
print_r($recData);

// Chunk the data array to set of 4 bytes
$values = array_chunk($recData, 4);
print_r($values);
$energymeter_param = array();
$count = 1;
foreach($values as $bytes){   
      $temp = PhpType::bytes2unsignedint($bytes);
      $energymeter_param[$count] = $temp;

    $count++;
}
//Store the number of energy meter parameters in a variable
$num_energymeter_param = $count;
echo "<h3>Energy meter array</h3>\n";
//print array 
print_r ($energymeter_param)." </br>";

这取决于您的数据的表示方式。在 Modbus 中,您需要两个寄存器来存储浮点数。然后你需要检查你的设备手册这些寄存器的 endianness 和其中的位顺序。

如果您一次读取两个寄存器,那么您可以使用 phpModbus 提供的辅助函数:

PhpType::bytes2float($recData, $endianness)

或者:

PhpType::bytes2float(array_reverse($recData), $endianness)

其中 $endianness 可以是 0 或 1。

编辑: 了解您的 device 并且您正在尝试读取 long 类型,格式为 Unsigned 32-bit integer Upper 16-bits (MSW) 在 lowest-numbered 寄存器 (4001/4000 = MSW/LSW) 所以你根本不能使用 bytes2float() 功能。

PhpType::bytes2unsignedint($bytes) 走在正确的道路上,但似乎您必须更改默认字节顺序:

PhpType::bytes2unsignedint($bytes, 1)

很抱歉让您感到困惑,我以为您正在阅读花车。