Skip to content

Commit

Permalink
Add support for double data type (64bit double precision floating p…
Browse files Browse the repository at this point in the history
…oint)
  • Loading branch information
aldas committed Dec 25, 2021
1 parent 4418c74 commit 73eba8b
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 45 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.4] - 2021-12-26

### Added

* support for `double` (64bit double precision floating point) data type to API. This requires PHP 7.2+ as it uses
https://www.php.net/manual/en/function.pack.php `E` and `e` formats.

## [2.3.1] - 2021-12-05

### Added
Expand Down
7 changes: 7 additions & 0 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
} catch (Exception $e) {
$Int64 = '-';
}
try {
$double = $quadWord->getDouble();
} catch (Exception $e) {
$double = '-';
}

}

Expand All @@ -88,6 +93,7 @@
'int32' => $doubleWord ? $doubleWord->getInt32() : null,
'UInt32' => $doubleWord ? $doubleWord->getUInt32() : null,
'float' => $doubleWord ? $doubleWord->getFloat() : null,
'double' => $quadWord ? $double : null,
'Int64' => $quadWord ? $Int64 : null,
'UInt64' => $quadWord ? $UInt64 : null,
];
Expand Down Expand Up @@ -166,6 +172,7 @@
<td>int32</td>
<td>UInt32</td>
<td>float</td>
<td>double</td>
<td>int64</td>
<td>UInt64</td>
</tr>
Expand Down
2 changes: 2 additions & 0 deletions src/Composer/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Address
const TYPE_INT64 = 'int64';
const TYPE_UINT64 = 'uint64';
const TYPE_FLOAT = 'float';
const TYPE_DOUBLE = 'double';
const TYPE_STRING = 'string';

const TYPES = [
Expand All @@ -26,6 +27,7 @@ interface Address
Address::TYPE_UINT64,
Address::TYPE_INT64,
Address::TYPE_FLOAT,
Address::TYPE_DOUBLE,
Address::TYPE_STRING,
];

Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Read/ReadCoilsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public function fromArray(array $coil): ReadCoilsBuilder
}

public function coil(
int $address,
string $name = null,
int $address,
string $name = null,
callable $callback = null,
callable $errorCallback = null
): ReadCoilsBuilder
Expand Down
72 changes: 47 additions & 25 deletions src/Composer/Read/ReadRegistersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public function fromArray(array $register): ReadRegistersBuilder
case Address::TYPE_FLOAT:
$this->float($address, $register['name'] ?? null, $callback, $errorCallback, $endian);
break;
case Address::TYPE_DOUBLE:
$this->double($address, $register['name'] ?? null, $callback, $errorCallback, $endian);
break;
case Address::TYPE_STRING:
$byteLength = $register['length'] ?? null;
if ($byteLength === null) {
Expand All @@ -168,11 +171,11 @@ public function byte(int $address, bool $firstByte = true, string $name = null,
}

public function int16(
int $address,
string $name = null,
int $address,
string $name = null,
callable $callback = null,
callable $errorCallback = null,
int $endian = null
int $endian = null
): ReadRegistersBuilder
{
$r = new ReadRegisterAddress(
Expand All @@ -187,11 +190,11 @@ public function int16(
}

public function uint16(
int $address,
string $name = null,
int $address,
string $name = null,
callable $callback = null,
callable $errorCallback = null,
int $endian = null
int $endian = null
): ReadRegistersBuilder
{
$r = new ReadRegisterAddress(
Expand All @@ -206,11 +209,11 @@ public function uint16(
}

public function int32(
int $address,
string $name = null,
int $address,
string $name = null,
callable $callback = null,
callable $errorCallback = null,
int $endian = null
int $endian = null
): ReadRegistersBuilder
{
$r = new ReadRegisterAddress(
Expand All @@ -225,11 +228,11 @@ public function int32(
}

public function uint32(
int $address,
string $name = null,
int $address,
string $name = null,
callable $callback = null,
callable $errorCallback = null,
int $endian = null
int $endian = null
): ReadRegistersBuilder
{
$r = new ReadRegisterAddress(
Expand All @@ -244,11 +247,11 @@ public function uint32(
}

public function uint64(
int $address,
string $name = null,
int $address,
string $name = null,
callable $callback = null,
callable $errorCallback = null,
int $endian = null
int $endian = null
): ReadRegistersBuilder
{
$r = new ReadRegisterAddress(
Expand All @@ -263,11 +266,11 @@ public function uint64(
}

public function int64(
int $address,
string $name = null,
int $address,
string $name = null,
callable $callback = null,
callable $errorCallback = null,
int $endian = null
int $endian = null
): ReadRegistersBuilder
{
$r = new ReadRegisterAddress(
Expand All @@ -282,11 +285,11 @@ public function int64(
}

public function float(
int $address,
string $name = null,
int $address,
string $name = null,
callable $callback = null,
callable $errorCallback = null,
int $endian = null
int $endian = null
): ReadRegistersBuilder
{
$r = new ReadRegisterAddress(
Expand All @@ -300,13 +303,32 @@ public function float(
return $this->addAddress($r);
}

public function double(
int $address,
string $name = null,
callable $callback = null,
callable $errorCallback = null,
int $endian = null
): ReadRegistersBuilder
{
$r = new ReadRegisterAddress(
$address,
ReadRegisterAddress::TYPE_DOUBLE,
$name,
$callback,
$errorCallback,
$endian
);
return $this->addAddress($r);
}

public function string(
int $address,
int $byteLength,
string $name = null,
int $address,
int $byteLength,
string $name = null,
callable $callback = null,
callable $errorCallback = null,
int $endian = null
int $endian = null
): ReadRegistersBuilder
{
if ($byteLength < 1 || $byteLength > 228) {
Expand Down
12 changes: 8 additions & 4 deletions src/Composer/Read/Register/ReadRegisterAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class ReadRegisterAddress extends RegisterAddress
private $endian;

public function __construct(
int $address,
string $type,
string $name = null,
int $address,
string $type,
string $name = null,
callable $callback = null,
callable $errorCallback = null,
int $endian = null
int $endian = null
)
{
parent::__construct($address, $type);
Expand All @@ -49,6 +49,7 @@ protected function getAllowedTypes(): array
Address::TYPE_INT64,
Address::TYPE_UINT64,
Address::TYPE_FLOAT,
Address::TYPE_DOUBLE,
];
}

Expand All @@ -71,6 +72,9 @@ protected function extractInternal(ModbusResponse $response)
case Address::TYPE_FLOAT:
$result = $response->getDoubleWordAt($this->address)->getFloat($this->endian);
break;
case Address::TYPE_DOUBLE:
$result = $response->getQuadWordAt($this->address)->getDouble($this->endian);
break;
case Address::TYPE_INT64:
$result = $response->getQuadWordAt($this->address)->getInt64($this->endian);
break;
Expand Down
1 change: 1 addition & 0 deletions src/Composer/RegisterAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function getSize(): int
break;
case Address::TYPE_INT64:
case Address::TYPE_UINT64:
case Address::TYPE_DOUBLE:
$size = 4;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Composer/Write/Register/WriteRegisterAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected function getAllowedTypes(): array
Address::TYPE_INT64,
Address::TYPE_UINT64,
Address::TYPE_FLOAT,
Address::TYPE_DOUBLE,
];
}

Expand Down Expand Up @@ -66,6 +67,9 @@ public function toBinary(): string
case Address::TYPE_FLOAT:
$result = Types::toReal($this->getValue());
break;
case Address::TYPE_DOUBLE:
$result = Types::toDouble($this->getValue());
break;

}
return $result;
Expand Down
8 changes: 8 additions & 0 deletions src/Composer/Write/WriteRegistersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public function fromArray(array $register): WriteRegistersBuilder
case Address::TYPE_FLOAT:
$this->float($address, $value);
break;
case Address::TYPE_DOUBLE:
$this->double($address, $value);
break;
case Address::TYPE_STRING:
$byteLength = $register['length'] ?? null;
if ($byteLength === null) {
Expand Down Expand Up @@ -171,6 +174,11 @@ public function float(int $address, float $value): WriteRegistersBuilder
return $this->addAddress(new WriteRegisterAddress($address, Address::TYPE_FLOAT, $value));
}

public function double(int $address, float $value): WriteRegistersBuilder
{
return $this->addAddress(new WriteRegisterAddress($address, Address::TYPE_DOUBLE, $value));
}

public function string(int $address, string $string, int $byteLength = null): WriteRegistersBuilder
{
return $this->addAddress(new StringWriteRegisterAddress($address, $string, $byteLength));
Expand Down
12 changes: 11 additions & 1 deletion src/Packet/QuadWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public function getInt64(int $endianness = null): int
return Types::parseInt64($this->getData(), $endianness);
}

/**
* @param int $endianness byte and word order for modbus binary data
* @return float
* @throws \RuntimeException
*/
public function getDouble(int $endianness = null): float
{
return Types::parseDouble($this->getData(), $endianness);
}

/**
* @return DoubleWord
* @throws \ModbusTcpClient\Exception\ModbusException
Expand Down Expand Up @@ -65,4 +75,4 @@ public static function fromWords(Word $word1, Word $word2, Word $word3, Word $wo
$word4->getData()
);
}
}
}
Loading

0 comments on commit 73eba8b

Please sign in to comment.