0.8 release

pull/1/head 0.8
John Long 11 years ago
parent 9ef8e72f3e
commit bb0cdd467a
  1. 209
      Phpmodbus/ModbusMaster.php
  2. 13
      README.md
  3. 19
      examples/example_fc4.php
  4. 24
      examples/example_fc5.php
  5. 11
      tests/ModbusMasterUdp/ref/test.fc4.php.html
  6. 5
      tests/ModbusMasterUdp/ref/test.fc5.php.html
  7. 14
      tests/ModbusMasterUdp/test.fc4.php
  8. 23
      tests/ModbusMasterUdp/test.fc5.php
  9. 4
      tests/config.php
  10. 20
      tutorials/Phpmodbus/Phpmodbus.pkg

@ -1,12 +1,12 @@
<?php <?php
/** /**
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora * Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
* *
* 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, 2012 Jan Krakora * @copyright Copyright (c) 2004, 2013 Jan Krakora
* @license PhpModbus license * @license PhpModbus license
* @category Phpmodbus * @category Phpmodbus
* @tutorial Phpmodbus.pkg * @tutorial Phpmodbus.pkg
@ -27,13 +27,15 @@ require_once dirname(__FILE__) . '/PhpType.php';
* - FC 1: read coils * - FC 1: read coils
* - FC 2: read input discretes * - FC 2: read input discretes
* - FC 3: read multiple registers * - FC 3: read multiple registers
* - FC 4: read multiple input registers
* - FC 5: write single coil
* - FC 6: write single register * - FC 6: write single register
* - FC 15: write multiple coils * - FC 15: write multiple coils
* - FC 16: write multiple registers * - FC 16: write multiple registers
* - FC 23: read write registers * - FC 23: read write registers
* *
* @author Jan Krakora * @author Jan Krakora
* @copyright Copyright (c) 2004, 2012 Jan Krakora * @copyright Copyright (c) 2004, 2013 Jan Krakora
* @package Phpmodbus * @package Phpmodbus
* *
*/ */
@ -512,6 +514,207 @@ class ModbusMaster {
return $data; return $data;
} }
/**
* readMultipleInputRegisters
*
* Modbus function FC 4(0x04) - Read Multiple Input Registers.
*
* This function reads {@link $quantity} of Words (2 bytes) from reference
* {@link $referenceRead} of a memory of a Modbus device given by
* {@link $unitId}.
*
*
* @param int $unitId usually ID of Modbus device
* @param int $reference Reference in the device memory to read data.
* @param int $quantity Amounth of the data to be read from device.
* @return false|Array Success flag or array of received data.
*/
function readMultipleInputRegisters($unitId, $reference, $quantity){
$this->status .= "readMultipleInputRegisters: START\n";
// connect
$this->connect();
// send FC 4
$packet = $this->readMultipleInputRegistersPacketBuilder($unitId, $reference, $quantity);
$this->status .= $this->printPacket($packet);
$this->send($packet);
// receive response
$rpacket = $this->rec();
$this->status .= $this->printPacket($rpacket);
// parse packet
$receivedData = $this->readMultipleInputRegistersParser($rpacket);
// disconnect
$this->disconnect();
$this->status .= "readMultipleInputRegisters: DONE\n";
// return
return $receivedData;
}
/**
* fc4
*
* Alias to {@link readMultipleInputRegisters} method.
*
* @param int $unitId
* @param int $reference
* @param int $quantity
* @return false|Array
*/
function fc4($unitId, $reference, $quantity){
return $this->readMultipleInputRegisters($unitId, $reference, $quantity);
}
/**
* readMultipleInputRegistersPacketBuilder
*
* Packet FC 4 builder - read multiple input registers
*
* @param int $unitId
* @param int $reference
* @param int $quantity
* @return string
*/
private function readMultipleInputRegistersPacketBuilder($unitId, $reference, $quantity){
$dataLen = 0;
// build data section
$buffer1 = "";
// build body
$buffer2 = "";
$buffer2 .= iecType::iecBYTE(4); // FC 4 = 4(0x04)
// build body - read section
$buffer2 .= iecType::iecINT($reference); // refnumber = 12288
$buffer2 .= iecType::iecINT($quantity); // quantity
$dataLen += 5;
// build header
$buffer3 = '';
$buffer3 .= iecType::iecINT(rand(0,65000)); // transaction ID
$buffer3 .= iecType::iecINT(0); // protocol ID
$buffer3 .= iecType::iecINT($dataLen + 1); // lenght
$buffer3 .= iecType::iecBYTE($unitId); // unit ID
// return packet string
return $buffer3. $buffer2. $buffer1;
}
/**
* readMultipleInputRegistersParser
*
* FC 4 response parser
*
* @param string $packet
* @return array
*/
private function readMultipleInputRegistersParser($packet){
$data = array();
// check Response code
$this->responseCode($packet);
// get data
for($i=0;$i<ord($packet[8]);$i++){
$data[$i] = ord($packet[9+$i]);
}
return $data;
}
/**
* writeSingleCoil
*
* Modbus function FC5(0x05) - Write Single Register.
*
* This function writes {@link $data} single coil at {@link $reference} position of
* memory of a Modbus device given by {@link $unitId}.
*
*
* @param int $unitId usually ID of Modbus device
* @param int $reference Reference in the device memory (e.g. in device WAGO 750-841, memory MW0 starts at address 12288)
* @param array $data value to be written (TRUE|FALSE).
* @return bool Success flag
*/
function writeSingleCoil($unitId, $reference, $data){
$this->status .= "writeSingleCoil: START\n";
// connect
$this->connect();
// send FC5
$packet = $this->writeSingleCoilPacketBuilder($unitId, $reference, $data);
$this->status .= $this->printPacket($packet);
$this->send($packet);
// receive response
$rpacket = $this->rec();
$this->status .= $this->printPacket($rpacket);
// parse packet
$this->writeSingleCoilParser($rpacket);
// disconnect
$this->disconnect();
$this->status .= "writeSingleCoil: DONE\n";
return true;
}
/**
* fc5
*
* Alias to {@link writeSingleCoil} method
*
* @param int $unitId
* @param int $reference
* @param array $data
* @param array $dataTypes
* @return bool
*/
function fc5($unitId, $reference, $data, $dataTypes){
return $this->writeSingleCoil($unitId, $reference, $data, $dataTypes);
}
/**
* writeSingleCoilPacketBuilder
*
* Packet builder FC5 - WRITE single register
*
* @param int $unitId
* @param int $reference
* @param array $data
* @param array $dataTypes
* @return string
*/
private function writeSingleCoilPacketBuilder($unitId, $reference, $data){
$dataLen = 0;
// build data section
$buffer1 = "";
foreach($data as $key=>$dataitem) {
if($dataitem == TRUE){
$buffer1 = iecType::iecINT(0xFF00);
} else {
$buffer1 = iecType::iecINT(0x0000);
};
};
$dataLen += 2;
// build body
$buffer2 = "";
$buffer2 .= iecType::iecBYTE(5); // FC5 = 5(0x05)
$buffer2 .= iecType::iecINT($reference); // refnumber = 12288
$dataLen += 3;
// build header
$buffer3 = '';
$buffer3 .= iecType::iecINT(rand(0,65000)); // transaction ID
$buffer3 .= iecType::iecINT(0); // protocol ID
$buffer3 .= iecType::iecINT($dataLen + 1); // lenght
$buffer3 .= iecType::iecBYTE($unitId); //unit ID
// return packet string
return $buffer3. $buffer2. $buffer1;
}
/**
* writeSingleCoilParser
*
* FC5 response parser
*
* @param string $packet
* @return bool
*/
private function writeSingleCoilParser($packet){
$this->responseCode($packet);
return true;
}
/** /**
* writeSingleRegister * writeSingleRegister
* *

@ -1,7 +1,7 @@
phpmodbus phpmodbus
========= =========
This project deals with an implementation of the basic functionality of the Modbus TCP and UDP based protocol using PHP. This project deals with an implementation of the basic functionality of the Modbus TCP and UDP based protocol using PHP.
It's a copy of the releases from the project page over at [Google Code](https://code.google.com/p/phpmodbus/) with It's a copy of the releases from the project page over at [Google Code](https://code.google.com/p/phpmodbus/) with
composer support added. composer support added.
Features Features
@ -11,6 +11,8 @@ Features
* FC1 - Read coils * FC1 - Read coils
* FC2 - Read input discretes * FC2 - Read input discretes
* FC3 - Read holding registers * FC3 - Read holding registers
* FC4 - Read holding input registers
* FC5 - Write single coil
* FC6 - Write single register * FC6 - Write single register
* FC15 - Write multiple coils * FC15 - Write multiple coils
* FC16 - Write multiple registers * FC16 - Write multiple registers
@ -21,10 +23,10 @@ Example
```php ```php
// Modbus master UDP // Modbus master UDP
$modbus = new ModbusMaster("192.168.1.1", "UDP"); $modbus = new ModbusMaster("192.168.1.1", "UDP");
// Read multiple registers // Read multiple registers
try { try {
$recData = $modbus->readMultipleRegisters(0, 12288, 5); $recData = $modbus->readMultipleRegisters(0, 12288, 5);
} }
catch (Exception $e) { catch (Exception $e) {
// Print error information if any // Print error information if any
@ -35,3 +37,8 @@ Example
// Print data in string format // Print data in string format
echo PhpType::bytes2string($recData); echo PhpType::bytes2string($recData);
``` ```
For more see [documentation][] or [FAQ][].
[documentation]: https://code.google.com/p/phpmodbus/downloads/list
[FAQ]: https://code.google.com/p/phpmodbus/wiki/FAQ

@ -0,0 +1,19 @@
<?php
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object
$modbus = new ModbusMasterUdp("192.192.15.51");
try {
// Read input discretes - FC 4
$recData = $modbus->readMultipleInputRegisters(0, 0, 2);
}
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
}
var_dump($recData);

@ -0,0 +1,24 @@
<?php
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object
$modbus = new ModbusMasterUdp("192.192.15.51");
// Data to be writen - TRUE, FALSE
$data_true = array(TRUE);
$data_false = array(FALSE);
try {
// 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);
}
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
}

@ -0,0 +1,11 @@
Test should pass when %IX0.0==FALSE and %IX0.1==TRUE
array(4) {
[0]=>
int(0)
[1]=>
int(2)
[2]=>
int(0)
[3]=>
int(0)
}

@ -0,0 +1,5 @@
Array
(
[0] => 0
[1] => 5
)

@ -0,0 +1,14 @@
<?php
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
require_once dirname(__FILE__) . '/../config.php';
// Create Modbus object
$modbus = new ModbusMasterUdp($test_host_ip);
// Test requirements
echo "Test should pass when %IX0.0==FALSE and %IX0.1==TRUE\n";
// Read input discretes - FC 4
$recData = $modbus->readMultipleInputRegisters(0, 0, 2);
var_dump($recData);

@ -0,0 +1,23 @@
<?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);

@ -1,3 +1,3 @@
<?php <?php
$test_host_ip = "192.168.1.105"; $test_host_ip = "192.192.1.10";
$test_bind_client_ip = "192.168.1.106"; $test_bind_client_ip = "192.192.1.100";

@ -24,6 +24,8 @@
<listitem><para>FC 1: read multiple coils</para></listitem> <listitem><para>FC 1: read multiple coils</para></listitem>
<listitem><para>FC 2: read input discretes</para></listitem> <listitem><para>FC 2: read input discretes</para></listitem>
<listitem><para>FC 3: read multiple registers</para></listitem> <listitem><para>FC 3: read multiple registers</para></listitem>
<listitem><para>FC 4: read multiple input registers</para></listitem>
<listitem><para>FC 5: write single coil</para></listitem>
<listitem><para>FC 6: write single register</para></listitem> <listitem><para>FC 6: write single register</para></listitem>
<listitem><para>FC 15: write multiple coils</para></listitem> <listitem><para>FC 15: write multiple coils</para></listitem>
<listitem><para>FC 16: write multiple registers</para></listitem> <listitem><para>FC 16: write multiple registers</para></listitem>
@ -121,6 +123,24 @@ catch (Exception $e) {
{@example example_fc3.php} {@example example_fc3.php}
</para> </para>
</refsect2> </refsect2>
<refsect2 id="{@id example_fc4}">
<title>FC4 - read multiple input registers </title>
<para>
FC4 functionality example
</para>
<para>
{@example example_fc4.php}
</para>
</refsect2>
<refsect2 id="{@id example_fc5}">
<title>FC5 - write single coil</title>
<para>
FC5 functionality example
</para>
<para>
{@example example_fc5.php}
</para>
</refsect2>
<refsect2 id="{@id example_fc6}"> <refsect2 id="{@id example_fc6}">
<title>FC6 - write single register</title> <title>FC6 - write single register</title>
<para> <para>

Loading…
Cancel
Save