コード例 #1
0
 /**
  * MBMaster_Update();
  */
 public function Update()
 {
     $this->Log('Update ...');
     $tstart = microtime(true);
     include_once __DIR__ . "/lib/ModbusMaster.php";
     $URL = "http://" . $this->ReadPropertyString("IPAddress");
     if (!Sys_Ping($this->ReadPropertyString("IPAddress"), 1000)) {
         $this->SetStatus(201);
         trigger_error("Invalid IP-Address", E_USER_ERROR);
         exit;
     }
     if ($this->ReadPropertyInteger("GatewayMode") === 0) {
         $modbus = new ModbusMasterTcp($this->ReadPropertyString("IPAddress"));
     } else {
         $modbus = new ModbusMasterUdp($this->ReadPropertyString("IPAddress"));
     }
     $count = 2;
     if ($this->ReadPropertyInteger("Poller") < 1000) {
         $count = 1000 / $this->ReadPropertyInteger("Poller");
     }
     for ($index = 1; $index < $count; $index++) {
         $this->Log('Update (' . $count . ') | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
         $tstartfor = microtime(true);
         $data = array();
         // FC 1 Rücklesen mehrerer digitaler Ausgänge
         if ($this->ReadPropertyInteger("CoilsQuantity") > 0) {
             try {
                 if (IPS_SemaphoreEnter("ModbusMaster", 1000)) {
                     $this->Log('Update (FC1) | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
                     $recData = $modbus->readCoils($this->ReadPropertyInteger("DeviceID"), $this->ReadPropertyInteger("CoilsReference"), $this->ReadPropertyInteger("CoilsQuantity"));
                     IPS_SemaphoreLeave("ModbusMaster");
                 }
             } catch (Exception $e) {
                 $this->SetStatus(200);
                 trigger_error("ModbusMaster: " . $e->getMessage() . "!", E_USER_ERROR);
                 exit;
             }
             $Address = $this->ReadPropertyInteger("CoilsReference");
             foreach ($recData as $Value) {
                 $data["FC1"][$Address] = $Value;
                 $Address++;
             }
         }
         // FC 2 Lesen mehrerer digitaler Eingänge
         if ($this->ReadPropertyInteger("DiscretesQuantity") > 0) {
             try {
                 if (IPS_SemaphoreEnter("ModbusMaster", 1000)) {
                     $this->Log('Update (FC2) | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
                     $recData = $modbus->readInputDiscretes($this->ReadPropertyInteger("DeviceID"), $this->ReadPropertyInteger("DiscretesReference"), $this->ReadPropertyInteger("DiscretesQuantity"));
                     IPS_SemaphoreLeave("ModbusMaster");
                 }
             } catch (Exception $e) {
                 $this->SetStatus(200);
                 trigger_error("ModbusMaster: " . $e->getMessage() . "!", E_USER_ERROR);
                 exit;
             }
             $Address = $this->ReadPropertyInteger("DiscretesReference");
             foreach ($recData as $Value) {
                 $data["FC2"][$Address] = $Value;
                 $Address++;
             }
         }
         // FC 3 Lesen mehrerer analoger Eingänge(und Ausgänge)
         if ($this->ReadPropertyInteger("RegistersQuantity") > 0) {
             try {
                 if (IPS_SemaphoreEnter("ModbusMaster", 1000)) {
                     $this->Log('Update (FC3) | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
                     $recData = $modbus->readMultipleRegisters($this->ReadPropertyInteger("DeviceID"), $this->ReadPropertyInteger("RegistersReference"), $this->ReadPropertyInteger("RegistersQuantity"));
                     IPS_SemaphoreLeave("ModbusMaster");
                 }
             } catch (Exception $e) {
                 $this->SetStatus(200);
                 trigger_error("ModbusMaster: " . $e->getMessage() . "!", E_USER_ERROR);
                 exit;
             }
             $Address = $this->ReadPropertyInteger("RegistersReference");
             $Values = array_chunk($recData, count($recData) / $this->ReadPropertyInteger("RegistersQuantity"));
             foreach ($Values as $Value) {
                 $data["FC3"][$Address] = $Value;
                 $Address++;
             }
         }
         // FC 4 Lesen mehrerer analoger Eingänge(und Ausgänge)
         if ($this->ReadPropertyInteger("InputRegistersQuantity") > 0) {
             try {
                 if (IPS_SemaphoreEnter("ModbusMaster", 1000)) {
                     $this->Log('Update (FC4) | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
                     $recData = $modbus->readMultipleInputRegisters($this->ReadPropertyInteger("DeviceID"), $this->ReadPropertyInteger("InputRegistersReference"), $this->ReadPropertyInteger("InputRegistersQuantity"));
                     IPS_SemaphoreLeave("ModbusMaster");
                 }
             } catch (Exception $e) {
                 $this->SetStatus(200);
                 trigger_error("ModbusMaster: " . $e->getMessage() . "!", E_USER_ERROR);
                 exit;
             }
             $Address = $this->ReadPropertyInteger("InputRegistersReference");
             $Values = array_chunk($recData, count($recData) / $this->ReadPropertyInteger("InputRegistersQuantity"));
             foreach ($Values as $Value) {
                 $data["FC4"][$Address] = $Value;
                 $Address++;
             }
         }
         $this->SendDataToChildren(json_encode(array("DataID" => "{449015FB-6717-4BB6-9F95-F69945CE1272}", "Buffer" => json_encode($data))));
         $this->SetStatus(102);
         //                $this->Log(number_format(((microtime(true)-$tstart)*1000),2) . ' ms');
         if ($this->ReadPropertyInteger("Poller") < 1000) {
             $Sleep = $this->ReadPropertyInteger("Poller") - (microtime(true) - $tstartfor) * 1000;
             if ($Sleep > 0) {
                 IPS_Sleep($Sleep);
             }
         }
         //                $this->Log(number_format(((microtime(true)-$tstart)*1000),2) . ' ms');
     }
     $this->Log('Update! | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
 }
コード例 #2
0
ファイル: test.fc15fc1.php プロジェクト: bitclaw/phpmodbus-2
<?php

require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
require_once dirname(__FILE__) . '/../config.php';
// Create Modbus object
$modbus = new ModbusMasterUdp($test_host_ip);
// Data to be writen - BOOL array
$data = array(1, 0, TRUE, TRUE, 0, 1, TRUE, TRUE, 1, 1, TRUE, TRUE, 0, 0, FALSE, FALSE, 0, 0, FALSE, FALSE, 1, 1, TRUE, TRUE, 1, 1, TRUE, TRUE, 1, 1, TRUE, TRUE);
// Write data - FC 15
$modbus->writeMultipleCoils(0, 12288, $data);
//echo $modbus->status;
//$modbus->status = "";
//echo "\n\n";
// Read data - FC 1
$recData = $modbus->readCoils(0, 12288, 32);
//echo $modbus->status;
//echo "\n\n";
var_dump($recData);