Trying to send RS485 modbus command with PHP and get response into variable

  Kiến thức lập trình

I’m trying to use PHP to send a command to an RS485 modbus device and get the response. I know the device is responding correctly, cable connected correctly, etc, but I cannot seem to get any response from the command sent.

This is the code I am using:

<?php
class rapidserial{
  function _blocking($device,$mode){
      stream_set_blocking($device, $mode);
      return true;
     }
  }
$rapidserial = new rapidserial();
$device = "COM3";
exec("mode $device BAUD=9600 PARITY=n DATA=8 STOP=2");
$comport = fopen($device, "r+b");
if ($comport === false)
    {
    die("Failed opening com port<br/>");
    }
    else
    {
    echo "Com Port Open<br/>"; 
    }
//Set non-blocking mode for writing
$rapidserial->_blocking($comport,0);
$atcmd = "x0Ax04x00x00x00x49x30x87"; //
fputs($comport, $atcmd);
usleep(10);
// Set blocking mode for reading
$rapidserial->_blocking($comport,1);
echo 'Response:';
$res = fgets($comport, 4017);
fclose($comport);
echo $res;
?>

And attached is the response expected from that command (using a modbus piece of software).

I was expected the response in the echo $res; line but nothing is returned.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT