<?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 - TRUE, FALSE $data_true = array(TRUE); $data_false = array(FALSE); // Reset target WORD $modbus->writeSingleRegister(0, 12288, array(0), array('WORD')); // Write single coil - FC5 $modbus->writeSingleCoil(0, 12288, $data_true); $modbus->writeSingleCoil(0, 12289, $data_false); $modbus->writeSingleCoil(0, 12290, $data_true); $modbus->writeSingleCoil(0, 12291, $data_false); // Read data - FC3 $recData = $modbus->readMultipleRegisters(0, 12288, 1); print_r($recData);
/** * 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'); }