1

m use php to connect modbus\tcp by phpmodbus。

https://github.com/krakorj/phpmodbus/blob/master/Phpmodbus/ModbusMaster.php

but address only up to 126 my Address is 32969 to 34597

equire_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; 
$IP =  $_POST["IP"] ;
$RAM_location =  $_POST["RAM_location"] ;
$RAM_NUM =  $_POST["RAM_NUM"] ;
$modbus = new ModbusMaster($IP, "TCP");
try {
    // FC 3
    $recData = $modbus->readMultipleRegisters(0, $RAM_location, $RAM_NUM);
}
catch (Exception $e) {
    // Print error information if any
    echo $modbus;
    echo $e;
    exit;
}
 $DataCount = count($recData);

$new_redata = array();
$j = $RAM_location;
for($i = 0;$i < $DataCount;$i = $i+2){
//  $new_redata[$j] =strval($recData[$i]) + strval($recData[$i+1]) ;
if ($recData[$i]!= 0 ){
    //this modbus is 256 carry 
    $reI = $recData[$i] * 256;
    } else {
    $reI = 0;
    }
$new_redata[$j+1] = $reI + $recData[$i+1] ;
$j++;
}

this is my use modbusmaster.php code.

jacklee
  • 11
  • 5

0 Answers0