reformat code and run PHP Code Sniffer beautifier

pull/2/head
toimtoimtoim 8 years ago
parent 6137326277
commit dea30f53b4
  1. 87
      src/IecType.php
  2. 876
      src/ModbusMaster.php
  3. 10
      src/ModbusMasterTcp.php
  4. 10
      src/ModbusMasterUdp.php
  5. 38
      src/PhpType.php

@ -29,32 +29,30 @@ namespace PHPModbus;
class IecType
{
/**
* iecBYTE
*
* Converts a value to IEC-1131 BYTE data type
* iecINT
*
* @param int $value from 0 to 255
* @return int IEC BYTE data type
* Converts a value to IEC-1131 INT data type
*
* @param int $value to be converted
* @return int IEC-1131 INT data type
*/
public static function iecBYTE($value)
public static function iecINT($value)
{
return chr($value & 0xFF);
return self::iecBYTE(($value >> 8) & 0x00FF) .
self::iecBYTE($value & 0x00FF);
}
/**
* iecINT
*
* Converts a value to IEC-1131 INT data type
* iecBYTE
*
* @param int $value to be converted
* @return int IEC-1131 INT data type
* Converts a value to IEC-1131 BYTE data type
*
* @param int $value from 0 to 255
* @return int IEC BYTE data type
*/
public static function iecINT($value)
public static function iecBYTE($value)
{
return self::iecBYTE(($value >> 8) & 0x00FF) .
self::iecBYTE(($value & 0x00FF));
return chr($value & 0xFF);
}
/**
@ -65,7 +63,6 @@ class IecType
* @param int $value to be converted
* @param int $bigEndian defines endian codding (little endian == 0, big endian == 1)
* @return int IEC-1131 INT data type
*
*/
public static function iecDINT($value, $bigEndian = 0)
{
@ -73,6 +70,33 @@ class IecType
return self::endianness($value, $bigEndian);
}
/**
* endianness
*
* Make endianess as required.
* For more see http://en.wikipedia.org/wiki/Endianness
*
* @param int $value
* @param bool $bigEndian
* @return int
*/
private static function endianness($value, $bigEndian = 0)
{
if ($bigEndian == 0) {
return
self::iecBYTE(($value >> 8) & 0x000000FF) .
self::iecBYTE($value & 0x000000FF) .
self::iecBYTE(($value >> 24) & 0x000000FF) .
self::iecBYTE(($value >> 16) & 0x000000FF);
} else {
return
self::iecBYTE(($value >> 24) & 0x000000FF) .
self::iecBYTE(($value >> 16) & 0x000000FF) .
self::iecBYTE(($value >> 8) & 0x000000FF) .
self::iecBYTE($value & 0x000000FF);
}
}
/**
* iecREAL
*
@ -106,36 +130,9 @@ class IecType
private static function float2iecReal($value)
{
// get float binary string
$float = pack("f", $value);
$float = pack('f', $value);
// set 32-bit unsigned integer of the float
$w = unpack("L", $float);
$w = unpack('L', $float);
return $w[1];
}
/**
* endianness
*
* Make endianess as required.
* For more see http://en.wikipedia.org/wiki/Endianness
*
* @param int $value
* @param bool $bigEndian
* @return int
*/
private static function endianness($value, $bigEndian = 0)
{
if ($bigEndian == 0) {
return
self::iecBYTE(($value >> 8) & 0x000000FF) .
self::iecBYTE(($value & 0x000000FF)) .
self::iecBYTE(($value >> 24) & 0x000000FF) .
self::iecBYTE(($value >> 16) & 0x000000FF);
} else {
return
self::iecBYTE(($value >> 24) & 0x000000FF) .
self::iecBYTE(($value >> 16) & 0x000000FF) .
self::iecBYTE(($value >> 8) & 0x000000FF) .
self::iecBYTE(($value & 0x000000FF));
}
}
}

File diff suppressed because it is too large Load Diff

@ -8,14 +8,12 @@ namespace PHPModbus;
* This source file is subject to the "PhpModbus license" that is bundled
* with this package in the file license.txt.
*
*
* @copyright Copyright (c) 2004, 2012 Jan Krakora
* @license PhpModbus license
* @category Phpmodbus
* @tutorial Phpmodbus.pkg
* @package Phpmodbus
* @version $id$
*
*/
/**
@ -23,17 +21,9 @@ namespace PHPModbus;
*
* This class deals with the MODBUS master using TCP. Extends ModbusMaster class.
*
* Implemented MODBUS functions:
* - FC 1: read coils
* - FC 3: read multiple registers
* - FC 15: write multiple coils
* - FC 16: write multiple registers
* - FC 23: read write registers
*
* @author Jan Krakora
* @copyright Copyright (c) 2004, 2012 Jan Krakora
* @package Phpmodbus
*
*/
class ModbusMasterTcp extends ModbusMaster
{

@ -8,14 +8,12 @@ namespace PHPModbus;
* This source file is subject to the "PhpModbus license" that is bundled
* with this package in the file license.txt.
*
*
* @copyright Copyright (c) 2004, 2012 Jan Krakora
* @license PhpModbus license
* @category Phpmodbus
* @tutorial Phpmodbus.pkg
* @package Phpmodbus
* @version $id$
*
*/
/**
@ -23,17 +21,9 @@ namespace PHPModbus;
*
* This class deals with the MODBUS master using UDP stack.
*
* Implemented MODBUS master functions:
* - FC 1: read coils
* - FC 3: read multiple registers
* - FC 15: write multiple coils
* - FC 16: write multiple registers
* - FC 23: read write registers
*
* @author Jan Krakora
* @copyright Copyright (c) 2004, 2012 Jan Krakora
* @package Phpmodbus
*
*/
class ModbusMasterUdp extends ModbusMaster
{

@ -1,6 +1,7 @@
<?php
namespace PHPModbus;
use Exception;
/**
@ -15,7 +16,6 @@ use Exception;
* @category Phpmodbus
* @package Phpmodbus
* @version $id$
*
*/
/**
@ -27,7 +27,6 @@ use Exception;
* @author Jan Krakora
* @copyright Copyright (c) 2004, 2012 Jan Krakora
* @package Phpmodbus
*
*/
class PhpType
{
@ -70,7 +69,7 @@ class PhpType
// Combine bytes
$int = self::combineBytes($data, $bigEndian);
// In the case of signed 2 byte value convert it to 4 byte one
if ((count($values) == 2) && ((0x8000 & $int) > 0)) {
if ((count($values) === 2) && ((0x8000 & $int) > 0)) {
$int = 0xFFFF8000 | $int;
}
// Convert the value
@ -159,9 +158,9 @@ class PhpType
private static function real2float($value)
{
// get unsigned long
$ulong = pack("L", $value);
$ulong = pack('L', $value);
// set float
$float = unpack("f", $ulong);
$float = unpack('f', $ulong);
return $float[1];
}
@ -176,7 +175,7 @@ class PhpType
*/
private static function dword2signedInt($value)
{
if ((0x80000000 & $value) != 0) {
if ((0x80000000 & $value) !== 0) {
return -(0x7FFFFFFF & ~$value) - 1;
} else {
return (0x7FFFFFFF & $value);
@ -193,7 +192,7 @@ class PhpType
*/
private static function dword2unsignedInt($value)
{
if ((0x80000000 & $value) != 0) {
if ((0x80000000 & $value) !== 0) {
return ((float)(0x7FFFFFFF & $value)) + 2147483648;
} else {
return (int)(0x7FFFFFFF & $value);
@ -206,29 +205,30 @@ class PhpType
* Check if the data variable is array, and check if the values are numeric
*
* @param int[] $data
* @return int
* @return array|\int[]
* @throws Exception
*/
private static function checkData($data)
{
// Check the data
if (!is_array($data) ||
count($data) < 2 ||
count($data) > 4 ||
count($data) == 3
$count = count($data);
if (!is_array($data)
|| $count < 2
|| $count > 4
|| $count === 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) {
if ($count === 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])
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');
}
@ -252,13 +252,13 @@ class PhpType
if ($bigEndian == 0) {
$value = (($data[3] & 0xFF) << 16) |
(($data[2] & 0xFF) << 24) |
(($data[1] & 0xFF)) |
($data[1] & 0xFF) |
(($data[0] & 0xFF) << 8);
} else {
$value = (($data[3] & 0xFF) << 24) |
(($data[2] & 0xFF) << 16) |
(($data[1] & 0xFF) << 8) |
(($data[0] & 0xFF));
($data[0] & 0xFF);
}
return $value;

Loading…
Cancel
Save