parent
fbb70f23e5
commit
da9731662c
@ -1,219 +1,249 @@ |
||||
<? |
||||
<?php |
||||
/** |
||||
* Phpmodbus Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) |
||||
* |
||||
* Phpmodbus Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) |
||||
* |
||||
* 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. |
||||
* |
||||
* @author Jan Krakora |
||||
* @copyright Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) |
||||
* @license PhpModbus license |
||||
* @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) |
||||
* @license PhpModbus license |
||||
* @category Phpmodbus |
||||
* @package Phpmodbus |
||||
* @version $id$ |
||||
* |
||||
* |
||||
*/ |
||||
|
||||
/** |
||||
* PhpType |
||||
* |
||||
* |
||||
* The class includes set of methods that convert the received Modbus data |
||||
* (array of bytes) to the PHP data type, i.e. signed int, unsigned int and float. |
||||
* |
||||
* @author Jan Krakora |
||||
* @copyright Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) |
||||
* @package Phpmodbus |
||||
* |
||||
* @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) |
||||
* @package Phpmodbus |
||||
* |
||||
*/ |
||||
class PhpType { |
||||
|
||||
/** |
||||
* bytes2float |
||||
* |
||||
* The function converts array of 4 bytes to float. The return value |
||||
* depends on order of the input bytes (endianning). |
||||
* |
||||
* @param array $values |
||||
* @param bool $endianness |
||||
* @return float |
||||
*/ |
||||
public static function bytes2float($values, $endianness = 0){ |
||||
$data = array(); |
||||
$real = 0; |
||||
|
||||
// Set the array to correct form |
||||
$data = self::checkData($values); |
||||
// Combine bytes |
||||
$real = self::combineBytes($data, $endianness); |
||||
// Convert the real value to float |
||||
return (float) self::real2float($real); |
||||
} |
||||
|
||||
/** |
||||
* bytes2signedInt |
||||
* |
||||
* The function converts array of 2 or 4 bytes to signed integer. |
||||
* The return value depends on order of the input bytes (endianning). |
||||
* |
||||
* @param array $values |
||||
* @param bool $endianness |
||||
* @return int |
||||
*/ |
||||
public static function bytes2signedInt($values, $endianness = 0){ |
||||
$data = array(); |
||||
$int = 0; |
||||
// Set the array to correct form |
||||
$data = self::checkData($values); |
||||
// Combine bytes |
||||
$int = self::combineBytes($data, $endianness); |
||||
// In the case of signed 2 byte value convert it to 4 byte one |
||||
if ((count($values) == 2) && ((0x8000 & $int) > 0)){ |
||||
$int = 0xFFFF8000 | $int; |
||||
|
||||
/** |
||||
* bytes2float |
||||
* |
||||
* The function converts array of 4 bytes to float. The return value |
||||
* depends on order of the input bytes (endianning). |
||||
* |
||||
* @param array $values |
||||
* @param bool $endianness |
||||
* @return float |
||||
*/ |
||||
public static function bytes2float($values, $endianness = 0) { |
||||
$data = array(); |
||||
$real = 0; |
||||
|
||||
// Set the array to correct form |
||||
$data = self::checkData($values); |
||||
// Combine bytes |
||||
$real = self::combineBytes($data, $endianness); |
||||
// Convert the real value to float |
||||
return (float) self::real2float($real); |
||||
} |
||||
|
||||
/** |
||||
* bytes2signedInt |
||||
* |
||||
* The function converts array of 2 or 4 bytes to signed integer. |
||||
* The return value depends on order of the input bytes (endianning). |
||||
* |
||||
* @param array $values |
||||
* @param bool $endianness |
||||
* @return int |
||||
*/ |
||||
public static function bytes2signedInt($values, $endianness = 0) { |
||||
$data = array(); |
||||
$int = 0; |
||||
// Set the array to correct form |
||||
$data = self::checkData($values); |
||||
// Combine bytes |
||||
$int = self::combineBytes($data, $endianness); |
||||
// In the case of signed 2 byte value convert it to 4 byte one |
||||
if ((count($values) == 2) && ((0x8000 & $int) > 0)) { |
||||
$int = 0xFFFF8000 | $int; |
||||
} |
||||
// Convert the value |
||||
return (int) self::dword2signedInt($int); |
||||
} |
||||
|
||||
/** |
||||
* bytes2unsignedInt |
||||
* |
||||
* The function converts array of 2 or 4 bytes to unsigned integer. |
||||
* The return value depends on order of the input bytes (endianning). |
||||
* |
||||
* @param array $values |
||||
* @param bool $endianness |
||||
* @return int|float |
||||
*/ |
||||
public static function bytes2unsignedInt($values, $endianness = 0) { |
||||
$data = array(); |
||||
$int = 0; |
||||
// Set the array to correct form |
||||
$data = self::checkData($values); |
||||
// Combine bytes |
||||
$int = self::combineBytes($data, $endianness); |
||||
// Convert the value |
||||
return self::dword2unsignedInt($int); |
||||
} |
||||
// Convert the value |
||||
return (int) self::dword2signedInt($int); |
||||
} |
||||
|
||||
/** |
||||
* bytes2unsignedInt |
||||
* |
||||
* The function converts array of 2 or 4 bytes to unsigned integer. |
||||
* The return value depends on order of the input bytes (endianning). |
||||
* |
||||
* @param array $values |
||||
* @param bool $endianness |
||||
* @return int|float |
||||
*/ |
||||
public static function bytes2unsignedInt($values, $endianness = 0){ |
||||
$data = array(); |
||||
$int = 0; |
||||
// Set the array to correct form |
||||
$data = self::checkData($values); |
||||
// Combine bytes |
||||
$int = self::combineBytes($data, $endianness); |
||||
// Convert the value |
||||
return self::dword2unsignedInt($int); |
||||
} |
||||
|
||||
/** |
||||
* real2float |
||||
* |
||||
* This function converts a value in IEC-1131 REAL single precision form to float. |
||||
* |
||||
* For more see [{@link http://en.wikipedia.org/wiki/Single_precision Single precision on Wiki}] or |
||||
* [{@link http://de.php.net/manual/en/function.base-convert.php PHP base_convert function commentary}, Todd Stokes @ Georgia Tech 21-Nov-2007] |
||||
* |
||||
* @param value value in IEC REAL data type to be converted |
||||
* @return float float value |
||||
*/ |
||||
private static function real2float($value){ |
||||
$two_pow_minus_x = array( |
||||
1, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, |
||||
0.0078125, 0.00390625, 0.001953125, 0.0009765625, |
||||
0.00048828125, 0.000244140625, 0.0001220703125, |
||||
0.00006103515625, 0.000030517578125, 0.0000152587890625, |
||||
0.00000762939453125, 0.000003814697265625, 0.0000019073486328125, |
||||
0.00000095367431640625, 0.000000476837158203125, |
||||
0.0000002384185791015625, 0.00000011920928955078125); |
||||
// get sign, mantisa, exponent |
||||
$real_mantisa = $value & 0x7FFFFF | 0x800000; |
||||
$real_exponent = ($value>>23) & 0xFF; |
||||
$real_sign = ($value>>31) & 0x01; |
||||
$bin_exponent = $real_exponent - 127; |
||||
// decode value |
||||
if (( $bin_exponent >= -126) && ($bin_exponent <= 127)) { |
||||
// Mantissa decoding |
||||
for ($i=0; $i<24; $i++) { |
||||
if ($real_mantisa & 0x01) |
||||
$val += $two_pow_minus_x[23-$i]; |
||||
$real_mantisa = $real_mantisa >> 1; |
||||
} |
||||
// Base |
||||
$val = $val * pow(2,$bin_exponent); |
||||
if (($real_sign == 1)) $val = -$val; |
||||
} |
||||
return (float)$val; |
||||
} |
||||
|
||||
/** |
||||
* dword2signedInt |
||||
* |
||||
* Switch double word to signed integer |
||||
* |
||||
* @param int $value |
||||
* @return int |
||||
*/ |
||||
private static function dword2signedInt($value){ |
||||
if ((0x80000000 & $value) != 0) { |
||||
return -(0x7FFFFFFF & ~$value)-1; |
||||
} else { |
||||
return (0x7FFFFFFF & $value); |
||||
|
||||
/** |
||||
* bytes2string |
||||
* |
||||
* The function converts an values array to the string. The function detects |
||||
* the end of the string by 0x00 character as defined by string standards. |
||||
* |
||||
* @param array $values |
||||
* @param bool $endianness |
||||
* @return string |
||||
*/ |
||||
public static function bytes2string($values, $endianness = 0) { |
||||
// Prepare string variable |
||||
$str = ""; |
||||
// Parse the received data word array |
||||
for($i=0;$i<count($values);$i+=2) { |
||||
if ($endianness) { |
||||
if($values[$i] != 0) |
||||
$str .= chr($values[$i]); |
||||
else |
||||
break; |
||||
if($values[$i+1] != 0) |
||||
$str .= chr($values[$i+1]); |
||||
else |
||||
break; |
||||
} |
||||
else { |
||||
if($values[$i+1] != 0) |
||||
$str .= chr($values[$i+1]); |
||||
else |
||||
break; |
||||
if($values[$i] != 0) |
||||
$str .= chr($values[$i]); |
||||
else |
||||
break; |
||||
} |
||||
} |
||||
// return string |
||||
return $str; |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* real2float |
||||
* |
||||
* This function converts a value in IEC-1131 REAL single precision form to float. |
||||
* |
||||
* For more see [{@link http://en.wikipedia.org/wiki/Single_precision Single precision on Wiki}] or |
||||
* [{@link http://de.php.net/manual/en/function.base-convert.php PHP base_convert function commentary}, Todd Stokes @ Georgia Tech 21-Nov-2007] or |
||||
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}] |
||||
* |
||||
* @param value value in IEC REAL data type to be converted |
||||
* @return float float value |
||||
*/ |
||||
private static function real2float($value) { |
||||
// get unsigned long |
||||
$ulong = pack("L", $value); |
||||
// set float |
||||
$float = unpack("f", $ulong); |
||||
|
||||
return $float[1]; |
||||
} |
||||
|
||||
/** |
||||
* dword2signedInt |
||||
* |
||||
* Switch double word to unsigned integer |
||||
* |
||||
* @param int $value |
||||
* @return int|float |
||||
*/ |
||||
private static function dword2unsignedInt($value){ |
||||
if ((0x80000000 & $value) != 0) { |
||||
return ((float) (0x7FFFFFFF & $value)) + 2147483648; |
||||
} else { |
||||
return (int) (0x7FFFFFFF & $value); |
||||
* dword2signedInt |
||||
* |
||||
* Switch double word to signed integer |
||||
* |
||||
* @param int $value |
||||
* @return int |
||||
*/ |
||||
private static function dword2signedInt($value) { |
||||
if ((0x80000000 & $value) != 0) { |
||||
return -(0x7FFFFFFF & ~$value)-1; |
||||
} else { |
||||
return (0x7FFFFFFF & $value); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* checkData |
||||
* |
||||
* Check if the data variable is array, and check if the values are numeric |
||||
* |
||||
* @param int $data |
||||
* @return int |
||||
*/ |
||||
private static function checkData($data){ |
||||
// Check the data |
||||
if (!is_array($data)) { |
||||
throw new Exception('The input data should be an array of bytes.'); |
||||
/** |
||||
* dword2signedInt |
||||
* |
||||
* Switch double word to unsigned integer |
||||
* |
||||
* @param int $value |
||||
* @return int|float |
||||
*/ |
||||
private static function dword2unsignedInt($value) { |
||||
if ((0x80000000 & $value) != 0) { |
||||
return ((float) (0x7FFFFFFF & $value)) + 2147483648; |
||||
} else { |
||||
return (int) (0x7FFFFFFF & $value); |
||||
} |
||||
} |
||||
// Check the values to be number - must be |
||||
if (!is_numeric($data[0]) || !is_numeric($data[1])) { |
||||
throw new Exception('Data are not numeric.'); |
||||
|
||||
/** |
||||
* checkData |
||||
* |
||||
* Check if the data variable is array, and check if the values are numeric |
||||
* |
||||
* @param int $data |
||||
* @return int |
||||
*/ |
||||
private static function checkData($data) { |
||||
// Check the data |
||||
if (!is_array($data) || |
||||
count($data)<2 || |
||||
count($data)>4 || |
||||
count($data)==3) { |
||||
throw new Exception('The input data should be an array of 2 or 4 bytes.'); |
||||
} |
||||
// Fill the rest of array by zeroes |
||||
if (count($data) == 2) { |
||||
$data[2] = 0; |
||||
$data[3] = 0; |
||||
} |
||||
// Check the values to be number |
||||
if (!is_numeric($data[0]) || |
||||
!is_numeric($data[1]) || |
||||
!is_numeric($data[2]) || |
||||
!is_numeric($data[3])) { |
||||
throw new Exception('Data are not numeric or the array keys are not indexed by 0,1,2 and 3'); |
||||
} |
||||
|
||||
return $data; |
||||
} |
||||
if (!is_numeric($data[2])) $data[2] = 0; |
||||
if (!is_numeric($data[3])) $data[3] = 0; |
||||
|
||||
return $data; |
||||
} |
||||
|
||||
/** |
||||
* combineBytes |
||||
* |
||||
* Combine bytes together |
||||
* |
||||
* @param int $data |
||||
* @param bool $endianness |
||||
* @return int |
||||
*/ |
||||
private static function combineBytes($data, $endianness){ |
||||
$value = 0; |
||||
// Combine bytes |
||||
if ($endianness == 0) |
||||
$value = (($data[3] & 0xFF)<<16) | |
||||
(($data[2] & 0xFF)<<24) | |
||||
(($data[1] & 0xFF)) | |
||||
(($data[0] & 0xFF)<<8); |
||||
else |
||||
$value = (($data[3] & 0xFF)<<24) | |
||||
(($data[2] & 0xFF)<<16) | |
||||
(($data[1] & 0xFF)<<8) | |
||||
(($data[0] & 0xFF)); |
||||
|
||||
return $value; |
||||
} |
||||
/** |
||||
* combineBytes |
||||
* |
||||
* Combine bytes together |
||||
* |
||||
* @param int $data |
||||
* @param bool $endianness |
||||
* @return int |
||||
*/ |
||||
private static function combineBytes($data, $endianness) { |
||||
$value = 0; |
||||
// Combine bytes |
||||
if ($endianness == 0) |
||||
$value = (($data[3] & 0xFF)<<16) | |
||||
(($data[2] & 0xFF)<<24) | |
||||
(($data[1] & 0xFF)) | |
||||
(($data[0] & 0xFF)<<8); |
||||
else |
||||
$value = (($data[3] & 0xFF)<<24) | |
||||
(($data[2] & 0xFF)<<16) | |
||||
(($data[1] & 0xFF)<<8) | |
||||
(($data[0] & 0xFF)); |
||||
|
||||
return $value; |
||||
} |
||||
} |
||||
?> |
@ -1,73 +1,69 @@ |
||||
<?php |
||||
|
||||
require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; |
||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php'; |
||||
|
||||
// Create Modbus object |
||||
$ip = "192.168.1.99"; |
||||
// $ip = "192.168.1.1"; |
||||
$ip = "192.192.15.51"; |
||||
$modbus = new ModbusMasterUdp($ip); |
||||
|
||||
// FC 3 |
||||
$moduleId = 0; |
||||
$reference = 12288; |
||||
$mw0address = 12288; |
||||
$quantity = 6; |
||||
$recData = $modbus->readMultipleRegisters($moduleId, $reference, $quantity); |
||||
|
||||
try { |
||||
// FC 3 |
||||
$moduleId = 0; |
||||
$reference = 12288; |
||||
$mw0address = 12288; |
||||
$quantity = 6; |
||||
$recData = $modbus->readMultipleRegisters($moduleId, $reference, $quantity); |
||||
print_r($recData); |
||||
} |
||||
catch (Exception $e) { |
||||
echo $modbus; |
||||
echo $e; |
||||
exit; |
||||
} |
||||
|
||||
?> |
||||
<html> |
||||
<head> |
||||
<meta http-equiv="content-type" content="text/html; charset=windows-1250"> |
||||
<meta name="generator" content="PSPad editor, www.pspad.com"> |
||||
<title>WAGO 750-841 M-memory dump</title> |
||||
</head> |
||||
<body> |
||||
<h1>Dump of M-memory from WAGO 750-84x series coupler.</h1> |
||||
<ul> |
||||
<li>PLC: 750-84x series</li> |
||||
<li>IP: <?=$ip?></li>
|
||||
<li>Modbus module ID: <?=$moduleId?></li>
|
||||
<li>Modbus memory reference: <?=$reference?></li>
|
||||
<li>Modbus memory quantity: <?=$quantity?></li>
|
||||
</ul> |
||||
|
||||
<h2>M-memory dump</h2> |
||||
|
||||
<?php |
||||
if(!$recData) { |
||||
// Print error information if any |
||||
echo "</br>Error:</br>" . $modbus->errstr . "</br>"; |
||||
} |
||||
else |
||||
{ |
||||
?> |
||||
|
||||
<table border="1px" width="400px"> |
||||
<tr> |
||||
<td>Modbus address</td> |
||||
<td>MWx</td> |
||||
<td>value</td> |
||||
</tr> |
||||
<?php |
||||
for($i=0;$i<count($recData);$i+=2){ |
||||
?> |
||||
<tr> |
||||
<td><?=$i+$reference?></td>
|
||||
<td>MW<?=($i + $reference - $mw0address)/2?></td>
|
||||
<td><?=($recData[$i] << 8)+ $recData[$i+1]?></td>
|
||||
</tr> |
||||
<?php |
||||
} |
||||
?> |
||||
</table> |
||||
|
||||
<?php |
||||
} |
||||
?> |
||||
|
||||
<h2>Modbus class status</h2> |
||||
<?php |
||||
echo $modbus->status; |
||||
?> |
||||
|
||||
</body> |
||||
<head> |
||||
<meta http-equiv="content-type" content="text/html; charset=windows-1250"> |
||||
<meta name="generator" content="PSPad editor, www.pspad.com"> |
||||
<title>WAGO 750-841 M-memory dump</title> |
||||
</head> |
||||
<body> |
||||
<h1>Dump of M-memory from WAGO 750-84x series coupler.</h1> |
||||
<ul> |
||||
<li>PLC: 750-84x series</li> |
||||
<li>IP: <?php echo $ip?></li>
|
||||
<li>Modbus module ID: <?php echo $moduleId?></li>
|
||||
<li>Modbus memory reference: <?php echo $reference?></li>
|
||||
<li>Modbus memory quantity: <?php echo $quantity?></li>
|
||||
</ul> |
||||
|
||||
<h2>M-memory dump</h2> |
||||
|
||||
<table border="1px" width="400px"> |
||||
<tr> |
||||
<td>Modbus address</td> |
||||
<td>MWx</td> |
||||
<td>value</td> |
||||
</tr> |
||||
<?php |
||||
for($i=0;$i<count($recData);$i+=2) { |
||||
?> |
||||
<tr> |
||||
<td><?php echo $i+$reference?></td>
|
||||
<td>MW<?php echo ($i + $reference - $mw0address)/2?></td>
|
||||
<td><?php echo ($recData[$i] << 8)+ $recData[$i+1]?></td>
|
||||
</tr> |
||||
<?php |
||||
} |
||||
?> |
||||
</table> |
||||
|
||||
<h2>Modbus class status</h2> |
||||
<?php |
||||
echo $modbus; |
||||
?> |
||||
|
||||
</body> |
||||
</html> |
||||
|
@ -1,55 +1,61 @@ |
||||
<?php |
||||
|
||||
require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; |
||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php'; |
||||
|
||||
// Create Modbus object |
||||
$modbus = new ModbusMasterUdp("192.168.1.99"); |
||||
$modbus = new ModbusMasterUdp("192.192.15.51"); |
||||
|
||||
// FC 3 |
||||
// read 10 words (20 bytes) from device ID=0, address=12288 |
||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10); |
||||
|
||||
if(!$recData) { |
||||
// Print error information if any |
||||
echo "</br>Error:</br>" . $modbus->errstr . "</br>"; |
||||
exit; |
||||
try { |
||||
// FC 3 |
||||
// read 10 words (20 bytes) from device ID=0, address=12288 |
||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10); |
||||
} |
||||
catch (Exception $e) { |
||||
// Print error information if any |
||||
echo $modbus; |
||||
echo $e; |
||||
exit; |
||||
} |
||||
|
||||
// Received data |
||||
echo "<h1>Received Data</h1>"; |
||||
echo "<h1>Received Data</h1>\n"; |
||||
print_r($recData); |
||||
|
||||
// Conversion |
||||
echo "<h2>32 bits types</h2>"; |
||||
echo "<h2>32 bits types</h2>\n"; |
||||
// Chunk the data array to set of 4 bytes |
||||
$values = array_chunk($recData, 4); |
||||
|
||||
// Get float from REAL interpretation |
||||
echo "<h3>REAL to Float</h3>"; |
||||
echo "<h3>REAL to Float</h3>\n"; |
||||
foreach($values as $bytes) |
||||
echo PhpType::bytes2float($bytes) . "</br>"; |
||||
echo PhpType::bytes2float($bytes) . "</br>"; |
||||
|
||||
// Get integer from DINT interpretation |
||||
echo "<h3>DINT to integer </h3>"; |
||||
echo "<h3>DINT to integer </h3>\n"; |
||||
foreach($values as $bytes) |
||||
echo PhpType::bytes2signedInt($bytes) . "</br>"; |
||||
echo PhpType::bytes2signedInt($bytes) . "</br>"; |
||||
|
||||
// Get integer of float from DINT interpretation |
||||
echo "<h3>DWORD to integer (or float) </h3>"; |
||||
echo "<h3>DWORD to integer (or float) </h3>\n"; |
||||
foreach($values as $bytes) |
||||
echo PhpType::bytes2unsignedInt($bytes) . "</br>"; |
||||
echo PhpType::bytes2unsignedInt($bytes) . "</br>"; |
||||
|
||||
echo "<h2>16 bit types</h2>"; |
||||
echo "<h2>16 bit types</h2>\n"; |
||||
// Chunk the data array to set of 4 bytes |
||||
$values = array_chunk($recData, 2); |
||||
|
||||
// Get signed integer from INT interpretation |
||||
echo "<h3>INT to integer </h3>"; |
||||
echo "<h3>INT to integer </h3>\n"; |
||||
foreach($values as $bytes) |
||||
echo PhpType::bytes2signedInt($bytes) . "</br>"; |
||||
echo PhpType::bytes2signedInt($bytes) . "</br>"; |
||||
|
||||
// Get unsigned integer from WORD interpretation |
||||
echo "<h3>WORD to integer </h3>"; |
||||
echo "<h3>WORD to integer </h3>\n"; |
||||
foreach($values as $bytes) |
||||
echo PhpType::bytes2unsignedInt($bytes) . "</br>"; |
||||
echo PhpType::bytes2unsignedInt($bytes) . "</br>"; |
||||
|
||||
// Get string from STRING interpretation |
||||
echo "<h3>STRING to string </h3>\n"; |
||||
echo PhpType::bytes2string($recData) . "</br>"; |
||||
?> |
@ -1,21 +1,26 @@ |
||||
<?php |
||||
|
||||
require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; |
||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php'; |
||||
|
||||
// Create Modbus object |
||||
$modbus = new ModbusMasterUdp("192.168.1.99"); |
||||
$modbus = new ModbusMasterUdp("192.192.15.51"); |
||||
|
||||
// Data to be writen |
||||
$data = array(1000, 2000, 3.0); |
||||
$dataTypes = array("INT", "DINT", "REAL"); |
||||
|
||||
// FC16 |
||||
if(!$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes)) { |
||||
// Print error information if any |
||||
echo "</br>Error:</br>" . $modbus->errstr . "</br>"; |
||||
try { |
||||
// FC16 |
||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes); |
||||
} |
||||
catch (Exception $e) { |
||||
// Print error information if any |
||||
echo $modbus; |
||||
echo $e; |
||||
exit; |
||||
} |
||||
|
||||
// Print status information |
||||
echo "</br>Status:</br>" . $modbus->status . "</br>"; |
||||
echo $modbus; |
||||
|
||||
?> |
@ -1,26 +1,31 @@ |
||||
<?php |
||||
|
||||
require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; |
||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php'; |
||||
|
||||
// Create Modbus object |
||||
$modbus = new ModbusMasterUdp("192.168.1.99"); |
||||
$modbus = new ModbusMasterUdp("192.192.15.51"); |
||||
|
||||
// Data to be writen |
||||
$data = array(1000, 2000, 3.0); |
||||
$dataTypes = array("INT", "DINT", "REAL"); |
||||
|
||||
// FC23 |
||||
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes); |
||||
|
||||
if(!$recData) { |
||||
// Print error information if any |
||||
echo "</br>Error:</br>" . $modbus->errstr . "</br>"; |
||||
try { |
||||
// FC23 |
||||
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes); |
||||
} |
||||
catch (Exception $e) { |
||||
// Print error information if any |
||||
echo $modbus; |
||||
echo $e; |
||||
exit; |
||||
} |
||||
|
||||
// Print status information |
||||
echo "</br>Status:</br>" . $modbus->status . "</br>"; |
||||
echo "</br>Status:</br>" . $modbus; |
||||
|
||||
// Print read data |
||||
echo "</br>Data:</br>"; print_r($recData); echo "</br>"; |
||||
echo "</br>Data:</br>"; |
||||
print_r($recData); |
||||
echo "</br>"; |
||||
|
||||
?> |
@ -1,21 +1,26 @@ |
||||
<?php |
||||
|
||||
require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; |
||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php'; |
||||
|
||||
// Create Modbus object |
||||
$modbus = new ModbusMasterUdp("192.168.1.99"); |
||||
$modbus = new ModbusMasterUdp("192.192.15.51"); |
||||
|
||||
// FC 3 |
||||
$recData = $modbus->readMultipleRegisters(0, 12288, 6); |
||||
|
||||
if(!$recData) { |
||||
// Print error information if any |
||||
echo "</br>Error:</br>" . $modbus->errstr . "</br>"; |
||||
try { |
||||
// FC 3 |
||||
$recData = $modbus->readMultipleRegisters(0, 12288, 6); |
||||
} |
||||
catch (Exception $e) { |
||||
// Print error information if any |
||||
echo $modbus; |
||||
echo $e; |
||||
exit; |
||||
} |
||||
|
||||
// Print status information |
||||
echo "</br>Status:</br>" . $modbus->status . "</br>"; |
||||
echo "</br>Status:</br>" . $modbus; |
||||
|
||||
// Print read data |
||||
echo "</br>Data:</br>"; print_r($recData); echo "</br>"; |
||||
echo "</br>Data:</br>"; |
||||
print_r($recData); |
||||
echo "</br>"; |
||||
?> |
@ -0,0 +1 @@ |
||||
eHll oowlr!da<br>Hello world!<br> |
@ -0,0 +1 @@ |
||||
Exception 'Data are not in array 2 or 4 bytes'<br>25602<br>Exception 'Data are not in array 2 or 4 bytes'<br>25602<br>Exception 'Data are not in array 2 or 4 bytes'<br> |
@ -0,0 +1 @@ |
||||
Exception 'Data are not numeric' |
@ -0,0 +1,28 @@ |
||||
<?php |
||||
|
||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php'; |
||||
|
||||
// Received bytes interpreting 3 REAL values (6 words) |
||||
$data = array( // String "Hello word!" |
||||
0x48, //H |
||||
0x65, //e |
||||
0x6c, //l |
||||
0x6c, //l |
||||
0x6f, //o |
||||
0x20, // |
||||
0x77, //w |
||||
0x6f, //o |
||||
0x72, //r |
||||
0x6c, //l |
||||
0x64, //d |
||||
0x21, //! |
||||
0x00, //\0 |
||||
0x61, //a |
||||
0x61 //a |
||||
); |
||||
|
||||
// Print string interpretation of the values |
||||
echo PhpType::bytes2string($data) . "<br>"; |
||||
echo PhpType::bytes2string($data, true) . "<br>"; |
||||
|
||||
?> |
@ -0,0 +1,44 @@ |
||||
<?php |
||||
/* |
||||
* To change this template, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php'; |
||||
|
||||
// Received bytes interpreting Mixed values |
||||
$data = Array ( |
||||
"0" => 100, // 32098 (DINT) |
||||
"1" => 2, |
||||
"2" => 0, |
||||
"3" => 0, |
||||
"4" => 100, // 32098 (DINT) |
||||
"5" => 2 |
||||
); |
||||
|
||||
// Print mixed values |
||||
try { |
||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 1)) . "<br>"; |
||||
} catch(Exception $e) { |
||||
echo "Exception 'Data are not in array 2 or 4 bytes'". "<br>"; |
||||
} |
||||
try { |
||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 2)). "<br>"; |
||||
} catch(Exception $e) { |
||||
echo "Exception 'Data are not in array 2 or 4 bytes'". "<br>"; |
||||
} |
||||
try { |
||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 3)). "<br>"; |
||||
} catch(Exception $e) { |
||||
echo "Exception 'Data are not in array 2 or 4 bytes'". "<br>"; |
||||
} |
||||
try { |
||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 4)). "<br>"; |
||||
} catch(Exception $e) { |
||||
echo "Exception 'Data are not in array 2 or 4 bytes'". "<br>"; |
||||
} |
||||
try { |
||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 5)). "<br>"; |
||||
} catch(Exception $e) { |
||||
echo "Exception 'Data are not in array 2 or 4 bytes'". "<br>"; |
||||
} |
||||
?> |
@ -0,0 +1,22 @@ |
||||
<?php |
||||
/* |
||||
* To change this template, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php'; |
||||
|
||||
// Received bytes interpreting Mixed values |
||||
$data = Array ( |
||||
"0" => 100, // 32098 (DINT) |
||||
"1" => "e", |
||||
"2" => 0, |
||||
"3" => 0 |
||||
); |
||||
|
||||
// Print mixed values |
||||
try { |
||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 4)); |
||||
} catch(Exception $e) { |
||||
echo "Exception 'Data are not numeric'"; |
||||
} |
||||
?> |
@ -1,5 +1,5 @@ |
||||
set php=C:\Programme\xampplite\php\php.exe |
||||
set php=c:\Program_Files\xampplite\php\php.exe |
||||
rem set php=C:\PHP\versions\php-5.2.6-Win32\php.exe -d auto_prepend_file=C:\PHP\locale.php |
||||
set diff="C:\Program Files\Octave\msys\bin\diff.exe" |
||||
set testUri=http://localHost/nette/_trunk/tests |
||||
set wget=wget.exe |
||||
set diff="diff.exe" |
||||
rem set testUri=http://localHost/nette/_trunk/tests |
||||
rem set wget=wget.exe |
||||
|
@ -1,3 +1,3 @@ |
||||
<?php |
||||
$testip = "192.168.1.15"; |
||||
$testip = "192.192.15.51"; |
||||
?> |
Loading…
Reference in new issue