@ -1,12 +1,12 @@
<?php
<?php
/**
/**
* Phpmodbus Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* Phpmodbus Copyright (c) 2004, 201 0 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
*
*
* This source file is subject to the "PhpModbus license" that is bundled
* This source file is subject to the "PhpModbus license" that is bundled
* with this package in the file license.txt.
* with this package in the file license.txt.
*
*
*
*
* @copyright Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @copyright Copyright (c) 2004, 201 0 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @license PhpModbus license
* @license PhpModbus license
* @category Phpmodbus
* @category Phpmodbus
* @tutorial Phpmodbus.pkg
* @tutorial Phpmodbus.pkg
@ -29,7 +29,7 @@ require_once dirname(__FILE__) . '/PhpType.php';
* - FC 23: read write registers
* - FC 23: read write registers
*
*
* @author Jan Krakora
* @author Jan Krakora
* @copyright Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @copyright Copyright (c) 2004, 201 0 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @package Phpmodbus
* @package Phpmodbus
*
*
*/
*/
@ -37,7 +37,6 @@ class ModbusMasterUdp {
var $sock;
var $sock;
var $port = "502";
var $port = "502";
var $host = "192.168.1.1";
var $host = "192.168.1.1";
var $errstr;
var $status;
var $status;
var $timeout_sec = 5; // 5 sec
var $timeout_sec = 5; // 5 sec
var $endianess = 0; // defines endian codding (little endian == 0, big endian == 1)
var $endianess = 0; // defines endian codding (little endian == 0, big endian == 1)
@ -53,6 +52,15 @@ class ModbusMasterUdp {
$this->host = $host;
$this->host = $host;
}
}
/**
* __toString
*
* Magic method
*/
function __toString() {
return "< pre > " . $this->status . "< / pre > ";
}
/**
/**
* connect
* connect
*
*
@ -66,11 +74,10 @@ class ModbusMasterUdp {
// connect
// connect
$result = @socket_connect($this->sock, $this->host, $this->port);
$result = @socket_connect($this->sock, $this->host, $this->port);
if ($result === false) {
if ($result === false) {
$this->errstr .= "socket_connect() failed.< / br > Reason: ($result) " .
throw new Exception("socket_connect() failed.< / br > Reason: ($result)".
socket_strerror(socket_last_error($this->sock));
socket_strerror(socket_last_error($this->sock)));
return false;
} else {
} else {
$this->status .= "Connected< / br > ";
$this->status .= "Connected\n ";
return true;
return true;
}
}
}
}
@ -82,7 +89,7 @@ class ModbusMasterUdp {
*/
*/
private function disconnect(){
private function disconnect(){
socket_close($this->sock);
socket_close($this->sock);
$this->status .= "Disconnected< / br > ";
$this->status .= "Disconnected\n ";
}
}
/**
/**
@ -94,7 +101,7 @@ class ModbusMasterUdp {
*/
*/
private function send($packet){
private function send($packet){
socket_write($this->sock, $packet, strlen($packet));
socket_write($this->sock, $packet, strlen($packet));
$this->status .= "Send< / br > ";
$this->status .= "Send\n";
}
}
/**
/**
@ -116,19 +123,18 @@ class ModbusMasterUdp {
$exceptsocks,
$exceptsocks,
0,
0,
300000) !== FALSE) {
300000) !== FALSE) {
$this->status .= "Wait received data< / br > ";
$this->status .= "Wait data ... \n";
if (in_array($this->sock, $readsocks)) {
if (in_array($this->sock, $readsocks)) {
while (@socket_recv($this->sock, $rec, 2000, 0)) {
while (@socket_recv($this->sock, $rec, 2000, 0)) {
$this->status .= "Received< / br > ";
$this->status .= "Data received\n ";
return $rec;
return $rec;
}
}
$lastAccess = time();
$lastAccess = time();
} else {
} else {
if (time()-$lastAccess >= $this->timeout_sec) {
if (time()-$lastAccess >= $this->timeout_sec) {
$this->errstr .= "Watchdog time expired [ " .
throw new Exception( "Watchdog time expired [ " .
$this->timeout_sec . " sec]!!! Connection to " .
$this->timeout_sec . " sec]!!! Connection to " .
$this->host . " is not established.";
$this->host . " is not established.");
return false;
}
}
}
}
$readsocks[] = $this->sock;
$readsocks[] = $this->sock;
@ -145,12 +151,9 @@ class ModbusMasterUdp {
*/
*/
private function responseCode($packet){
private function responseCode($packet){
if(($packet[7] & 0x80) > 0) {
if(($packet[7] & 0x80) > 0) {
$this->errstr .= "Modbus response error code:" . ord($packet[8]);
throw new Exception("Modbus response error code:" . ord($packet[8]));
return false;
} else {
}
$this->status .= "Modbus response error code: NOERROR\n";
else
{
$this->status .= "Modbus response error code: NOERROR< / br > ";
return true;
return true;
}
}
}
}
@ -171,27 +174,21 @@ class ModbusMasterUdp {
* @return false|Array Success flag or array of received data.
* @return false|Array Success flag or array of received data.
*/
*/
function readMultipleRegisters($unitId, $reference, $quantity){
function readMultipleRegisters($unitId, $reference, $quantity){
$this->errstr = "";
$this->status .= "readMultipleRegisters: START\n";
$this->status = "readMultipleRegisters: START< / br > ";
// connect
// connect
if(!$this->connect())
$this->connect();
return false;
// send FC 3
// send FC 3
$packet = $this->readMultipleRegistersPacketBuilder($unitId, $reference, $quantity);
$packet = $this->readMultipleRegistersPacketBuilder($unitId, $reference, $quantity);
$this->status .= $this->printPacket($packet);
$this->status .= $this->printPacket($packet);
$this->send($packet);
$this->send($packet);
// receive response
// receive response
$rpacket = $this->rec();
$rpacket = $this->rec();
if(!$rpacket)
return false;
$this->status .= $this->printPacket($rpacket);
$this->status .= $this->printPacket($rpacket);
// parse packet
// parse packet
$receivedData = $this->readMultipleRegistersParser($rpacket);
$receivedData = $this->readMultipleRegistersParser($rpacket);
if(!$receivedData)
return false;
// disconnect
// disconnect
$this->disconnect();
$this->disconnect();
$this->status .= "readMultipleRegisters: DONE< / br > ";
$this->status .= "readMultipleRegisters: DONE\n";
// return
// return
return $receivedData;
return $receivedData;
}
}
@ -222,6 +219,8 @@ class ModbusMasterUdp {
*/
*/
private function readMultipleRegistersPacketBuilder($unitId, $reference, $quantity){
private function readMultipleRegistersPacketBuilder($unitId, $reference, $quantity){
$dataLen = 0;
$dataLen = 0;
// build data section
$buffer1 = "";
// build body
// build body
$buffer2 = "";
$buffer2 = "";
$buffer2 .= iecType::iecBYTE(3); // FC 3 = 3(0x03)
$buffer2 .= iecType::iecBYTE(3); // FC 3 = 3(0x03)
@ -249,9 +248,8 @@ class ModbusMasterUdp {
*/
*/
private function readMultipleRegistersParser($packet){
private function readMultipleRegistersParser($packet){
$data = array();
$data = array();
// if not exception
// check Response code
if(!$this->responseCode($packet))
$this->responseCode($packet);
return false;
// get data
// get data
for($i=0;$i< ord ( $ packet [ 8 ] ) ; $ i + + ) {
for($i=0;$i< ord ( $ packet [ 8 ] ) ; $ i + + ) {
$data[$i] = ord($packet[9+$i]);
$data[$i] = ord($packet[9+$i]);
@ -275,26 +273,21 @@ class ModbusMasterUdp {
* @return bool Success flag
* @return bool Success flag
*/
*/
function writeMultipleRegister($unitId, $reference, $data, $dataTypes){
function writeMultipleRegister($unitId, $reference, $data, $dataTypes){
$this->errstr = "";
$this->status .= "writeMultipleRegister: START\n";
$this->status = "writeMultipleRegister: START< / br > ";
// connect
// connect
if(!$this->connect())
$this->connect();
return false;
// send FC16
// send FC16
$packet = $this->writeMultipleRegisterPacketBuilder($unitId, $reference, $data, $dataTypes);
$packet = $this->writeMultipleRegisterPacketBuilder($unitId, $reference, $data, $dataTypes);
$this->status .= $this->printPacket($packet);
$this->status .= $this->printPacket($packet);
$this->send($packet);
$this->send($packet);
// receive response
// receive response
$rpacket = $this->rec();
$rpacket = $this->rec();
if(!$rpacket)
return false;
$this->status .= $this->printPacket($rpacket);
$this->status .= $this->printPacket($rpacket);
// parse packet
// parse packet
if(!$this->writeMultipleRegisterParser($rpacket))
$this->writeMultipleRegisterParser($rpacket);
return false;
// disconnect
// disconnect
$this->disconnect();
$this->disconnect();
$this->status .= "writeMultipleRegister: DONE< / br > ";
$this->status .= "writeMultipleRegister: DONE\n ";
return true;
return true;
}
}
@ -376,8 +369,7 @@ class ModbusMasterUdp {
* @return bool
* @return bool
*/
*/
private function writeMultipleRegisterParser($packet){
private function writeMultipleRegisterParser($packet){
if(!$this->responseCode($rpacket))
$this->responseCode($rpacket);
return false;
return true;
return true;
}
}
@ -400,27 +392,21 @@ class ModbusMasterUdp {
* @return false|Array Success flag or array of data.
* @return false|Array Success flag or array of data.
*/
*/
function readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes){
function readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes){
$this->errstr = "";
$this->status .= "readWriteRegisters: START\n";
$this->status = "readWriteRegisters: START< / br > ";
// connect
// connect
if(!$this->connect())
$this->connect();
return false;
// send FC23
// send FC23
$packet = $this->readWriteRegistersPacketBuilder($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes);
$packet = $this->readWriteRegistersPacketBuilder($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes);
$this->status .= $this->printPacket($packet);
$this->status .= $this->printPacket($packet);
$this->send($packet);
$this->send($packet);
// receive response
// receive response
$rpacket = $this->rec();
$rpacket = $this->rec();
if(!$rpacket)
return false;
$this->status .= $this->printPacket($rpacket);
$this->status .= $this->printPacket($rpacket);
// parse packet
// parse packet
$receivedData = $this->readWriteRegistersParser($rpacket);
$receivedData = $this->readWriteRegistersParser($rpacket);
if(!$receivedData)
return false;
// disconnect
// disconnect
$this->disconnect();
$this->disconnect();
$this->status .= "writeMultipleRegister: DONE< / br > ";
$this->status .= "writeMultipleRegister: DONE\n";
// return
// return
return $receivedData;
return $receivedData;
}
}
@ -538,7 +524,7 @@ class ModbusMasterUdp {
/**
/**
* printPacket
* printPacket
*
*
* Print whole packet in the hex form
* Print a packet in the hex form
*
*
* @param string $packet
* @param string $packet
* @return string
* @return string
@ -549,7 +535,7 @@ class ModbusMasterUdp {
for($i=0;$i< strlen ( $ packet ) ; $ i + + ) {
for($i=0;$i< strlen ( $ packet ) ; $ i + + ) {
$str .= $this->byte2hex(ord($packet[$i]));
$str .= $this->byte2hex(ord($packet[$i]));
}
}
$str .= "< / br > ";
$str .= "\n ";
return $str;
return $str;
}
}
}
}