/**
 * Title
 *
 * Description
 *
 * @access public
 */
 function poll_device($id)
 {
     $rec = SQLSelectOne("SELECT * FROM modbusdevices WHERE ID='" . (int) $id . "'");
     if (!$rec['ID']) {
         return;
     }
     $rec['CHECK_LATEST'] = date('Y-m-d H:i:s');
     $rec['CHECK_NEXT'] = date('Y-m-d H:i:s', time() + (int) $rec['POLLPERIOD']);
     SQLUpdate('modbusdevices', $rec);
     if ($rec['LINKED_OBJECT'] && $rec['LINKED_PROPERTY'] && ($rec['REQUEST_TYPE'] == 'FC5' || $rec['REQUEST_TYPE'] == 'FC6' || $rec['REQUEST_TYPE'] == 'FC15' || $rec['REQUEST_TYPE'] == 'FC16' || $rec['REQUEST_TYPE'] == 'FC23')) {
         $rec['DATA'] = getGlobal($rec['LINKED_OBJECT'] . '.' . $rec['LINKED_PROPERTY']);
     }
     require_once dirname(__FILE__) . '/ModbusMaster.php';
     $modbus = new ModbusMaster($rec['HOST'], $rec['PROTOCOL']);
     if ($rec['PORT']) {
         $modbus->port = $rec['PORT'];
     }
     if ($rec['REQUEST_TYPE'] == 'FC1') {
         //FC1 Read coils
         try {
             $recData = $modbus->readCoils($rec['DEVICE_ID'], $rec['REQUEST_START'], $rec['REQUEST_TOTAL']);
             if (is_array($recData)) {
                 foreach ($recData as $k => $v) {
                     $recData[$k] = (int) $v;
                 }
             }
         } catch (Exception $e) {
             // Print error information if any
             $rec['LOG'] = date('Y-m-d H:i:s') . " FC1 Error: {$modbus} {$e}\n" . $rec['LOG'];
         }
     } elseif ($rec['REQUEST_TYPE'] == 'FC2') {
         //FC2 Read input discretes
         try {
             $recData = $modbus->readInputDiscretes($rec['DEVICE_ID'], $rec['REQUEST_START'], $rec['REQUEST_TOTAL']);
             if (is_array($recData)) {
                 foreach ($recData as $k => $v) {
                     $recData[$k] = (int) $v;
                 }
             }
         } catch (Exception $e) {
             // Print error information if any
             $rec['LOG'] = date('Y-m-d H:i:s') . " FC2 Error: {$modbus} {$e}\n" . $rec['LOG'];
         }
     } elseif ($rec['REQUEST_TYPE'] == 'FC3') {
         //FC3 Read holding registers
         try {
             $recData = $modbus->readMultipleRegisters($rec['DEVICE_ID'], $rec['REQUEST_START'], $rec['REQUEST_TOTAL']);
         } catch (Exception $e) {
             // Print error information if any
             $rec['LOG'] = date('Y-m-d H:i:s') . " FC3 Error: {$modbus} {$e}\n" . $rec['LOG'];
         }
     } elseif ($rec['REQUEST_TYPE'] == 'FC4') {
         //FC4 Read holding input registers
         try {
             $recData = $modbus->readMultipleInputRegisters($rec['DEVICE_ID'], $rec['REQUEST_START'], $rec['REQUEST_TOTAL']);
         } catch (Exception $e) {
             // Print error information if any
             $rec['LOG'] = date('Y-m-d H:i:s') . " FC4 Error: {$modbus} {$e}\n" . $rec['LOG'];
         }
     } elseif ($rec['REQUEST_TYPE'] == 'FC5') {
         //FC5 Write single coil
         if ((int) $rec['DATA']) {
             $data_set = array(TRUE);
         } else {
             $data_set = array(FALSE);
         }
         try {
             $modbus->writeSingleCoil($rec['DEVICE_ID'], $rec['REQUEST_START'], $data_set);
         } catch (Exception $e) {
             $rec['LOG'] = date('Y-m-d H:i:s') . " FC5 Error: {$modbus} {$e}\n" . $rec['LOG'];
         }
     } elseif ($rec['REQUEST_TYPE'] == 'FC6') {
         //FC6 Write single register
         try {
             $data_set = array((int) $rec['DATA']);
             if ($rec['RESPONSE_CONVERT'] == 'r2f') {
                 $dataTypes = array("REAL");
                 $swapregs = false;
             } elseif ($rec['RESPONSE_CONVERT'] == 'r2fs') {
                 $dataTypes = array("REAL");
                 $swapregs = true;
             } elseif ($rec['RESPONSE_CONVERT'] == 'd2i' || $rec['RESPONSE_CONVERT'] == 'dw2i') {
                 $dataTypes = array("DINT");
                 $swapregs = false;
             } elseif ($rec['RESPONSE_CONVERT'] == 'd2is' || $rec['RESPONSE_CONVERT'] == 'dw2is') {
                 $dataTypes = array("DINT");
                 $swapregs = true;
             } else {
                 $dataTypes = array("INT");
                 $swapregs = false;
             }
             $recData = $modbus->writeSingleRegister($rec['DEVICE_ID'], $rec['REQUEST_START'], $data_set, $dataTypes, $swapregs);
         } catch (Exception $e) {
             // Print error information if any
             $rec['LOG'] = date('Y-m-d H:i:s') . " FC6 Error: {$modbus} {$e}\n" . $rec['LOG'];
         }
     } elseif ($rec['REQUEST_TYPE'] == 'FC15') {
         //FC15 Write multiple coils
         $data_set = explode(',', $rec['DATA']);
         foreach ($data_set as $k => $v) {
             $data_set[$k] = (bool) $v;
         }
         try {
             $modbus->writeMultipleCoils($rec['DEVICE_ID'], $rec['REQUEST_START'], $data_set);
         } catch (Exception $e) {
             $rec['LOG'] = date('Y-m-d H:i:s') . " FC15 Error: {$modbus} {$e}\n" . $rec['LOG'];
         }
     } elseif ($rec['REQUEST_TYPE'] == 'FC16') {
         //FC16 Write multiple registers
         try {
             $data_set = explode(',', $rec['DATA']);
             $dataTypes = array();
             foreach ($data_set as $k => $v) {
                 if ($rec['RESPONSE_CONVERT'] == 'r2f') {
                     $dataTypes[] = "REAL";
                     $data_set[$k] = (double) $v;
                     $swapregs = false;
                 } elseif ($rec['RESPONSE_CONVERT'] == 'r2fs') {
                     $dataTypes[] = "REAL";
                     $data_set[$k] = (double) $v;
                     $swapregs = true;
                 } elseif ($rec['RESPONSE_CONVERT'] == 'd2i' || $rec['RESPONSE_CONVERT'] == 'dw2i') {
                     $dataTypes[] = "DINT";
                     $data_set[$k] = (int) $v;
                     $swapregs = false;
                 } elseif ($rec['RESPONSE_CONVERT'] == 'd2is' || $rec['RESPONSE_CONVERT'] == 'dw2is') {
                     $dataTypes[] = "DINT";
                     $data_set[$k] = (int) $v;
                     $swapregs = true;
                 } else {
                     $data_set[$k] = (int) $v;
                     $dataTypes[] = "INT";
                     $swapregs = false;
                 }
             }
             $recData = $modbus->writeMultipleRegister($rec['DEVICE_ID'], $rec['REQUEST_START'], $data_set, $dataTypes, $swapregs);
         } catch (Exception $e) {
             // Print error information if any
             $rec['LOG'] = date('Y-m-d H:i:s') . " FC16 Error: {$modbus} {$e}\n" . $rec['LOG'];
         }
     } elseif ($rec['REQUEST_TYPE'] == 'FC23') {
         //FC23 Read/Write multiple registers
         //TO-DO
     }
     //echo $rec['LOG'];exit;
     if ($rec['REQUEST_TYPE'] == 'FC1' || $rec['REQUEST_TYPE'] == 'FC2' || $rec['REQUEST_TYPE'] == 'FC3' || $rec['REQUEST_TYPE'] == 'FC4' && is_array($recData)) {
         // PROCESS RESPONSE
         if ($rec['RESPONSE_CONVERT'] == 'r2f') {
             //REAL to Float
             $values = array_chunk($recData, 4);
             $recData = array();
             foreach ($values as $bytes) {
                 echo $recData[] = PhpType::bytes2float($bytes, false);
             }
         } elseif ($rec['RESPONSE_CONVERT'] == 'r2fs') {
             //REAL to Float (swap regs)
             $values = array_chunk($recData, 4);
             $recData = array();
             foreach ($values as $bytes) {
                 echo $recData[] = PhpType::bytes2float($bytes, true);
             }
         } elseif ($rec['RESPONSE_CONVERT'] == 'd2i') {
             //DINT to integer
             $values = array_chunk($recData, 4);
             $recData = array();
             foreach ($values as $bytes) {
                 echo $recData[] = PhpType::bytes2signedInt($bytes, false);
             }
         } elseif ($rec['RESPONSE_CONVERT'] == 'd2is') {
             //DINT to integer (swap regs)
             $values = array_chunk($recData, 4);
             $recData = array();
             foreach ($values as $bytes) {
                 echo $recData[] = PhpType::bytes2signedInt($bytes, true);
             }
         } elseif ($rec['RESPONSE_CONVERT'] == 'dw2i') {
             //DWORD to integer
             $values = array_chunk($recData, 4);
             $recData = array();
             foreach ($values as $bytes) {
                 $recData[] = PhpType::bytes2unsignedInt($bytes, false);
             }
         } elseif ($rec['RESPONSE_CONVERT'] == 'dw2is') {
             //DWORD to integer (swap regs)
             $values = array_chunk($recData, 4);
             $recData = array();
             foreach ($values as $bytes) {
                 $recData[] = PhpType::bytes2unsignedInt($bytes, true);
             }
         } elseif ($rec['RESPONSE_CONVERT'] == 'i2i') {
             //INT to integer
             $values = array_chunk($recData, 2);
             $recData = array();
             foreach ($values as $bytes) {
                 $recData[] = PhpType::bytes2signedInt($bytes, false);
             }
         } elseif ($rec['RESPONSE_CONVERT'] == 'w2i') {
             //WORD to integer
             $values = array_chunk($recData, 2);
             $recData = array();
             foreach ($values as $bytes) {
                 $recData[] = PhpType::bytes2unsignedInt($bytes, false);
             }
         } elseif ($rec['RESPONSE_CONVERT'] == 's') {
             //String
             $recData = array(PhpType::bytes2string($recData));
         } else {
             //
         }
         $result = implode(',', $recData);
         if ($result && $result != $rec['DATA']) {
             $rec['LOG'] = date('Y-m-d H:i:s') . " " . $result . "\n" . $rec['LOG'];
         }
         $rec['DATA'] = $result;
         SQLUpdate('modbusdevices', $rec);
         if ($rec['LINKED_OBJECT'] && $rec['LINKED_PROPERTY']) {
             setGlobal($rec['LINKED_OBJECT'] . '.' . $rec['LINKED_PROPERTY'], $rec['DATA'], array($this->name => '0'));
         }
     } else {
         SQLUpdate('modbusdevices', $rec);
     }
 }
 public function setReferencedType(PhpType $type)
 {
     if (!$type->isObjectType()) {
         throw new \LogicException('The referenced type must be an object type.');
     }
     if ($type->isNoObjectType()) {
         throw new \LogicException('The referenced type must not be the generic object type.');
     }
     parent::setReferencedType($type);
     $this->resolved = true;
 }
Example #3
0
 public function __construct($resourcesDir = null, $indexFilename = 'README', $filesExtension = 'md')
 {
     if (null === $resourcesDir) {
         $resourcesDir = __DIR__ . '/Resources/markdown';
     }
     parent::__construct($resourcesDir, $indexFilename, $filesExtension);
 }
Example #4
0
function getModbusRegisters($modbusHostname, $slaveAddress, $startRegister, $nRegisters)
{
    /*
    printf("# getModbusRegisters(modbusHostname=%s, slaveAddress=%s, startRegister=%s, nRegisters=%s\n",
    	$modbusHostname,
    	$slaveAddress,
    	$startRegister,
    	$nRegisters
    );
    */
    $modbus = new ModbusMaster($modbusHostname, "TCP");
    /* read registers */
    try {
        $result = $modbus->readMultipleRegisters($slaveAddress, $startRegister, $nRegisters);
    } catch (Exception $e) {
        printf("Exception: %s\n", $e);
        return array();
    }
    /* split into 1 word (2 byte) chunks */
    $result = array_chunk($result, 2);
    /* word results */
    $r = array();
    for ($i = 0; $i < sizeof($result); $i++) {
        $r[$i + $startRegister] = PhpType::bytes2unsignedInt($result[$i]);
    }
    return $r;
}
 public function testForEquality(PhpType $that)
 {
     if (null !== ($rs = parent::testForEquality($that))) {
         return $rs;
     }
     if ($that->isIntegerType() || $that->isDoubleType() || $that->isNullType() || $that->isResourceType() || $that->isNullType() || $that->isBooleanType()) {
         return TernaryValue::get('false');
     }
     return TernaryValue::get('unknown');
 }
 public function equals(PhpType $that)
 {
     if (parent::equals($that)) {
         return true;
     }
     if ($that->isUnknownType()) {
         return true;
     }
     return false;
 }
 public function testForEquality(PhpType $that)
 {
     if (null !== ($rs = parent::testForEquality($that))) {
         return $rs;
     }
     if ($that->isObjectType() || $that->isNoObjectType() || $that->isArrayType() || $that->isNullType() || $that->isCallableType()) {
         return TernaryValue::get('false');
     }
     return TernaryValue::get('unknown');
 }
 public function isSubTypeOf(PhpType $type)
 {
     if (parent::isSubTypeOf($type)) {
         return true;
     }
     if ($type->isCallableType()) {
         return true;
     }
     return false;
 }
 public function testForEquality(PhpType $that)
 {
     $rs = parent::testForEquality($that);
     if ($rs !== null) {
         return $rs;
     }
     if ($that->isUnknownType() || $that->isNullType() || $that->isStringType() || $that->isIntegerType() || $that->isResourceType() || $that->isDoubleType()) {
         return TernaryValue::get('unknown');
     }
     return TernaryValue::get('false');
 }
 public function testForEquality(PhpType $that)
 {
     $rs = parent::testForEquality($that);
     if (null !== $rs) {
         return $rs;
     }
     if ($that->isUnknownType() || $that->isSubTypeOf($this->registry->getNativeType('scalar')) || $that->isNullType() || $that->isResourceType() || $that->isObjectType()) {
         return TernaryValue::get('unknown');
     }
     return TernaryValue::get('false');
 }
 public function testForEquality(PhpType $that)
 {
     $rs = parent::testForEquality($that);
     if ($rs !== null) {
         return $rs;
     }
     if ($that->isNullType()) {
         return TernaryValue::get('true');
     }
     if ($that->isUnknownType() || $that->isNullable()) {
         return TernaryValue::get('unknown');
     }
     return TernaryValue::get('false');
 }
 public function getLeastSuperType(PhpType $that)
 {
     if ($that->isArrayType()) {
         if ($this->isSubtypeOf($that)) {
             // We have a special case, that is if we compare array<unknown> to array<all>. Although, both types are
             // subtypes of each other, we always must have a predictable outcome of this method regardless of the
             // order of types, i.e. $a->getLeastSuperType($b) === $b->getLeastSuperType($a).
             if ($this->elementType->isUnknownType() && $that->elementType->isAllType()) {
                 return $this;
             }
             if ($that->elementType->isUnknownType() && $this->elementType->isAllType()) {
                 return $that;
             }
             return parent::getLeastSuperType($that);
         }
         $genericArrayType = $this->registry->getNativeType('array');
         if ($this === $genericArrayType) {
             return $this;
         }
         if ($that === $genericArrayType) {
             return $that;
         }
         // If both arrays have different item types defined, we keep them as
         // separate arrays in a union type. It can be used as an indication
         // that a key is not always defined.
         if (count($this->itemTypes) !== count($that->itemTypes) || (bool) array_diff_key($this->itemTypes, $that->itemTypes)) {
             return parent::getLeastSuperType($that);
         }
         $keyType = $this->registry->createUnionType(array($this->keyType, $that->keyType));
         $elementType = $this->registry->createUnionType(array($this->elementType, $that->elementType));
         $itemTypes = array();
         foreach ($this->itemTypes as $name => $itemType) {
             $itemTypes[$name] = $this->registry->createUnionType(array($itemType, $that->itemTypes[$name]));
         }
         return $this->registry->getArrayType($elementType, $keyType, $itemTypes);
     }
     return parent::getLeastSuperType($that);
 }
Example #13
0
<?php

require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
// Received bytes interpreting 3 REAL values (6 words)
$data = array(0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x0, 0x61, 0x61);
// Print string interpretation of the values
echo PhpType::bytes2string($data) . "<br>";
echo PhpType::bytes2string($data, true) . "<br>";
Example #14
0
<?php

require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
// Received bytes interpreting Mixed values
$data = array("0" => 125, "1" => 98, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "6" => 0, "7" => 0, "8" => 0, "9" => 0, "10" => 0, "11" => 0, "12" => 255, "13" => 255, "14" => 255, "15" => 255, "16" => 158, "17" => 88, "18" => 97, "19" => 168);
// Print mixed values
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 4)) . "<br>";
echo PhpType::bytes2signedInt(array_slice($data, 4, 4)) . "<br>";
echo PhpType::bytes2signedInt(array_slice($data, 8, 4)) . "<br>";
echo PhpType::bytes2signedInt(array_slice($data, 12, 4)) . "<br>";
echo PhpType::bytes2signedInt(array_slice($data, 16, 2)) . "<br>";
echo PhpType::bytes2signedInt(array_slice($data, 18, 2)) . "<br>";
<?php

require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
// Received bytes interpreting DINT values
$data = array(0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0x80, 0x0, 0xff, 0xff, 0x7f, 0xff);
$dword = array_chunk($data, 4);
// Print float interpretation of the real value
foreach ($dword as $value) {
    var_dump(PhpType::bytes2unsignedInt($value));
    echo "<br>";
}
?>
 
<?php

require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
// Received bytes interpreting DINT values
$data = array(0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0x80, 0x0, 0xff, 0xff, 0x7f, 0xff);
$dword = array_chunk($data, 4);
// Print float interpretation of the real value
foreach ($dword as $value) {
    echo PhpType::bytes2signedInt($value) . "<br>";
}
?>
 
function test($modbus, $id, $start, $points)
{
    $recData = $modbus->readMultipleRegisters($id, $start, $points);
    // Print read data
    echo "</br>Data:</br>";
    var_dump($recData);
    echo "</br>";
    // Received data
    echo "<b>Received Data</b>\n";
    print_r($recData);
    // Conversion
    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 "<b>REAL to Float</b>\n";
    foreach ($values as $bytes) {
        echo PhpType::bytes2float($bytes) . "</br>";
    }
    // Get integer from DINT interpretation
    echo "<b>DINT to integer </b>\n";
    foreach ($values as $bytes) {
        echo PhpType::bytes2signedInt($bytes) . "</br>";
    }
    // Get integer of float from DINT interpretation
    echo "<b>DWORD to integer (or float) </b>\n";
    foreach ($values as $bytes) {
        echo PhpType::bytes2unsignedInt($bytes) . "</br>";
    }
    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 "<b>INT to integer </b>\n";
    foreach ($values as $bytes) {
        echo PhpType::bytes2signedInt($bytes) . "</br>";
    }
    // Get unsigned integer from WORD interpretation
    echo "<b>WORD to integer </b>\n";
    foreach ($values as $bytes) {
        echo PhpType::bytes2unsignedInt($bytes) . "</br>";
    }
    // Get string from STRING interpretation
    echo "<b>STRING to string </b>\n";
    echo PhpType::bytes2string($recData) . "</br>";
}
<?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, "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'";
}
Example #19
0
                $i = $i + 1;
            }
        } else {
            try {
                // FC 3
                // read 10 words (20 bytes) from device ID=0, address=12288
                $recData = $modbus->readMultipleRegisters(0, mysql_result($result1, 0, "start") + mysql_result($result, $w, "start"), mysql_result($result, $w, "anzahl"));
            } catch (Exception $e) {
                // Print error information if any
                echo $modbus;
                echo $e;
                exit;
            }
            $values = array_chunk($recData, 2);
            $i = 0;
            foreach ($values as $bytes) {
                $text1 = mysql_result($result, $w, "art");
                $text2 = mysql_result($result, $w, "start") + $i;
                $recData1["" . $text1 . $text2 . ""] = PhpType::bytes2signedInt($bytes);
                $i = $i + 1;
            }
        }
        $w++;
    }
    $current = json_encode($recData1);
    $file = 'https/read/read.html';
    file_put_contents($file, $current);
}
// Shut down the daemon nicely
// This is ignored if the class is actually running in the foreground
System_Daemon::stop();
Example #20
0
<?php

require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
// Received bytes interpreting 3 REAL values (6 words)
$data = array(0 => 0, 1 => 0, 2 => 68, 3 => 122, 4 => 0, 5 => 0, 6 => 68, 7 => 250, 8 => 0, 9 => 0, 10 => 63, 11 => 160);
$dword = array_chunk($data, 4);
// Print float interpretation of the real value
echo PhpType::bytes2float($dword[0]) . "<br>";
echo PhpType::bytes2float($dword[1]) . "<br>";
echo PhpType::bytes2float($dword[2]) . "<br>";
 public function isSubTypeOf(PhpType $that)
 {
     if ($that->isUnknownType()) {
         return true;
     }
     if ($that->isAllType()) {
         return true;
     }
     foreach ($this->alternates as $element) {
         if (!$element->isSubTypeOf($that)) {
             return false;
         }
     }
     return true;
 }
 /**
  * Returns whether the given type may be passed for the given expected type.
  *
  * @param PhpType $expectedType
  * @param PhpType $actualType
  *
  * @return boolean
  */
 public function mayBePassed(PhpType $expectedType, PhpType $actualType)
 {
     // This effectively disables all type checks for mock objects.
     // TODO: Remove this once we have support for parameterized types, and
     //       anonymous classes.
     if (null !== ($objType = $actualType->toMaybeObjectType()) && $objType->getName() === 'PHPUnit_Framework_MockObject_MockObject') {
         return true;
     }
     if ($actualType instanceof ProxyObjectType && $actualType->getReferenceName() === 'PHPUnit_Framework_MockObject_MockObject') {
         return true;
     }
     // If the actual type is not yet resolved, then we need to let it go through
     // in favor of avoiding false positives. This indicates a non-existent class,
     // but that is handled in a different pass.
     if ($actualType->isNoResolvedType()) {
         return true;
     }
     if ($expectedType->isCallableType()) {
         return $actualType->canBeCalled();
     }
     // Allow an object that is implementing __toString to be passed where a
     // string is expected. This should work in most cases unless users perform
     // some sort of is_??? check, but then they probably also expect non strings.
     if ($expectedType->isStringType() && null !== ($objType = $actualType->toMaybeObjectType()) && $objType->hasMethod('__toString')) {
         return true;
     }
     if ($actualType->isSubtypeOf($expectedType)) {
         return true;
     }
     // If we are in strict mode, it's already over here.
     if (self::LEVEL_LENIENT !== $this->level) {
         return false;
     }
     // If the actual type is an all-type, we will not make any fuzz in lenient mode. Simply, because there are a lot
     // of cases where "all" should really rather mean "unknown" type.
     if ($actualType->isAllType()) {
         return true;
     }
     switch (true) {
         case $expectedType->isArrayType():
             // If the generic array type is passed in places where a more specific
             // array type is required, we will let this go through in lenient mode.
             if ($actualType === $this->typeRegistry->getNativeType('array')) {
                 return true;
             }
             if (!$actualType->isArrayType()) {
                 return false;
             }
             return $this->mayBePassed($expectedType->getElementType(), $actualType->getElementType());
         case $expectedType->isDoubleType():
         case $expectedType->isStringType():
         case $expectedType->isIntegerType():
             $actualType = $actualType->restrictByNotNull();
             return $actualType->isSubTypeOf($this->typeRegistry->createUnionType(array('string', 'integer', 'double')));
             // For unions we let the check pass if the actual type may be passed for any
             // of the union's alternates.
         // For unions we let the check pass if the actual type may be passed for any
         // of the union's alternates.
         case $expectedType->isUnionType():
             assert($expectedType instanceof UnionType);
             foreach ($expectedType->getAlternates() as $alt) {
                 if ($this->mayBePassed($alt, $actualType)) {
                     return true;
                 }
             }
             return false;
         default:
             return false;
     }
 }
 * and open the template in the editor.
 */
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
// Received bytes interpreting Mixed values
$data = array("0" => 100, "1" => 2, "2" => 0, "3" => 0, "4" => 100, "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>";
}
Example #24
0
 /**
  * ReceiveData
  * @param string $JSONString
  */
 public function ReceiveData($JSONString)
 {
     // Empfangene Daten
     $Data = json_decode($JSONString);
     //IPS_LogMessage("ReceiveData", utf8_decode($JSONString));
     if ($Data->DataID === "{449015FB-6717-4BB6-9F95-F69945CE1272}") {
         $Data = json_decode($Data->Buffer, true);
         if ($this->ReadPropertyInteger("DataType") === 0) {
             if ($this->ReadPropertyBoolean("ReadOnly")) {
                 $Value = @$Data["FC2"][$this->ReadPropertyInteger("Address")];
             } else {
                 $Value = @$Data["FC1"][$this->ReadPropertyInteger("Address")];
             }
             if (isset($Value)) {
                 if (GetValue($this->GetIDForIdent("Value")) != $Value) {
                     SetValueBoolean($this->GetIDForIdent("Value"), $Value);
                 }
             }
         } else {
             if ($this->ReadPropertyBoolean("ReadOnly")) {
                 $Bytes = @$Data["FC4"][$this->ReadPropertyInteger("Address")];
             } else {
                 $Bytes = @$Data["FC3"][$this->ReadPropertyInteger("Address")];
             }
             if (isset($Bytes)) {
                 switch ($this->ReadPropertyInteger("DataType")) {
                     case 1:
                     case 2:
                     case 3:
                         $Value = PhpType::bytes2unsignedInt($Bytes);
                         break;
                     case 4:
                     case 5:
                     case 6:
                     case 8:
                         $Value = PhpType::bytes2signedInt($Bytes);
                         break;
                     case 7:
                     case 9:
                         $Value = PhpType::bytes2float($Bytes);
                         break;
                 }
                 if (GetValue($this->GetIDForIdent("Value")) != $Value) {
                     SetValue($this->GetIDForIdent("Value"), $Value);
                 }
                 if ($this->ReadPropertyString("Math") != "" and $this->ReadPropertyBoolean("ReadOnly")) {
                     $Value = $this->Math($Value . $this->ReadPropertyString("Math"));
                     if (GetValue($this->GetIDForIdent("ValueMath")) != $Value) {
                         SetValue($this->GetIDForIdent("ValueMath"), $Value);
                     }
                 }
             }
         }
     }
 }
 public function testForEquality(PhpType $that)
 {
     if ($this->isNoResolvedType() && ($that->isObjectType() || $that->isNoObjectType())) {
         return TernaryValue::get('unknown');
     }
     return $this->referencedType->testForEquality($that);
 }
Example #26
0
echo "<h3>REAL to Float</h3>\n";
foreach ($values as $bytes) {
    echo PhpType::bytes2float($bytes) . "</br>";
}
// Get integer from DINT interpretation
echo "<h3>DINT to integer </h3>\n";
foreach ($values as $bytes) {
    echo PhpType::bytes2signedInt($bytes) . "</br>";
}
// Get integer of float from DINT interpretation
echo "<h3>DWORD to integer (or float) </h3>\n";
foreach ($values as $bytes) {
    echo PhpType::bytes2unsignedInt($bytes) . "</br>";
}
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>\n";
foreach ($values as $bytes) {
    echo PhpType::bytes2signedInt($bytes) . "</br>";
}
// Get unsigned integer from WORD interpretation
echo "<h3>WORD to integer </h3>\n";
foreach ($values as $bytes) {
    echo PhpType::bytes2unsignedInt($bytes) . "</br>";
}
// Get string from STRING interpretation
echo "<h3>STRING to string </h3>\n";
echo PhpType::bytes2string($recData) . "</br>";