Skip to content

Commit 73eba8b

Browse files
committed
Add support for double data type (64bit double precision floating point)
1 parent 4418c74 commit 73eba8b

File tree

17 files changed

+282
-45
lines changed

17 files changed

+282
-45
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [2.4] - 2021-12-26
8+
9+
### Added
10+
11+
* support for `double` (64bit double precision floating point) data type to API. This requires PHP 7.2+ as it uses
12+
https://www.php.net/manual/en/function.pack.php `E` and `e` formats.
13+
714
## [2.3.1] - 2021-12-05
815

916
### Added

examples/index.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
} catch (Exception $e) {
7474
$Int64 = '-';
7575
}
76+
try {
77+
$double = $quadWord->getDouble();
78+
} catch (Exception $e) {
79+
$double = '-';
80+
}
7681

7782
}
7883

@@ -88,6 +93,7 @@
8893
'int32' => $doubleWord ? $doubleWord->getInt32() : null,
8994
'UInt32' => $doubleWord ? $doubleWord->getUInt32() : null,
9095
'float' => $doubleWord ? $doubleWord->getFloat() : null,
96+
'double' => $quadWord ? $double : null,
9197
'Int64' => $quadWord ? $Int64 : null,
9298
'UInt64' => $quadWord ? $UInt64 : null,
9399
];
@@ -166,6 +172,7 @@
166172
<td>int32</td>
167173
<td>UInt32</td>
168174
<td>float</td>
175+
<td>double</td>
169176
<td>int64</td>
170177
<td>UInt64</td>
171178
</tr>

src/Composer/Address.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface Address
1414
const TYPE_INT64 = 'int64';
1515
const TYPE_UINT64 = 'uint64';
1616
const TYPE_FLOAT = 'float';
17+
const TYPE_DOUBLE = 'double';
1718
const TYPE_STRING = 'string';
1819

1920
const TYPES = [
@@ -26,6 +27,7 @@ interface Address
2627
Address::TYPE_UINT64,
2728
Address::TYPE_INT64,
2829
Address::TYPE_FLOAT,
30+
Address::TYPE_DOUBLE,
2931
Address::TYPE_STRING,
3032
];
3133

src/Composer/Read/ReadCoilsBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public function fromArray(array $coil): ReadCoilsBuilder
106106
}
107107

108108
public function coil(
109-
int $address,
110-
string $name = null,
109+
int $address,
110+
string $name = null,
111111
callable $callback = null,
112112
callable $errorCallback = null
113113
): ReadCoilsBuilder

src/Composer/Read/ReadRegistersBuilder.php

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ public function fromArray(array $register): ReadRegistersBuilder
143143
case Address::TYPE_FLOAT:
144144
$this->float($address, $register['name'] ?? null, $callback, $errorCallback, $endian);
145145
break;
146+
case Address::TYPE_DOUBLE:
147+
$this->double($address, $register['name'] ?? null, $callback, $errorCallback, $endian);
148+
break;
146149
case Address::TYPE_STRING:
147150
$byteLength = $register['length'] ?? null;
148151
if ($byteLength === null) {
@@ -168,11 +171,11 @@ public function byte(int $address, bool $firstByte = true, string $name = null,
168171
}
169172

170173
public function int16(
171-
int $address,
172-
string $name = null,
174+
int $address,
175+
string $name = null,
173176
callable $callback = null,
174177
callable $errorCallback = null,
175-
int $endian = null
178+
int $endian = null
176179
): ReadRegistersBuilder
177180
{
178181
$r = new ReadRegisterAddress(
@@ -187,11 +190,11 @@ public function int16(
187190
}
188191

189192
public function uint16(
190-
int $address,
191-
string $name = null,
193+
int $address,
194+
string $name = null,
192195
callable $callback = null,
193196
callable $errorCallback = null,
194-
int $endian = null
197+
int $endian = null
195198
): ReadRegistersBuilder
196199
{
197200
$r = new ReadRegisterAddress(
@@ -206,11 +209,11 @@ public function uint16(
206209
}
207210

208211
public function int32(
209-
int $address,
210-
string $name = null,
212+
int $address,
213+
string $name = null,
211214
callable $callback = null,
212215
callable $errorCallback = null,
213-
int $endian = null
216+
int $endian = null
214217
): ReadRegistersBuilder
215218
{
216219
$r = new ReadRegisterAddress(
@@ -225,11 +228,11 @@ public function int32(
225228
}
226229

227230
public function uint32(
228-
int $address,
229-
string $name = null,
231+
int $address,
232+
string $name = null,
230233
callable $callback = null,
231234
callable $errorCallback = null,
232-
int $endian = null
235+
int $endian = null
233236
): ReadRegistersBuilder
234237
{
235238
$r = new ReadRegisterAddress(
@@ -244,11 +247,11 @@ public function uint32(
244247
}
245248

246249
public function uint64(
247-
int $address,
248-
string $name = null,
250+
int $address,
251+
string $name = null,
249252
callable $callback = null,
250253
callable $errorCallback = null,
251-
int $endian = null
254+
int $endian = null
252255
): ReadRegistersBuilder
253256
{
254257
$r = new ReadRegisterAddress(
@@ -263,11 +266,11 @@ public function uint64(
263266
}
264267

265268
public function int64(
266-
int $address,
267-
string $name = null,
269+
int $address,
270+
string $name = null,
268271
callable $callback = null,
269272
callable $errorCallback = null,
270-
int $endian = null
273+
int $endian = null
271274
): ReadRegistersBuilder
272275
{
273276
$r = new ReadRegisterAddress(
@@ -282,11 +285,11 @@ public function int64(
282285
}
283286

284287
public function float(
285-
int $address,
286-
string $name = null,
288+
int $address,
289+
string $name = null,
287290
callable $callback = null,
288291
callable $errorCallback = null,
289-
int $endian = null
292+
int $endian = null
290293
): ReadRegistersBuilder
291294
{
292295
$r = new ReadRegisterAddress(
@@ -300,13 +303,32 @@ public function float(
300303
return $this->addAddress($r);
301304
}
302305

306+
public function double(
307+
int $address,
308+
string $name = null,
309+
callable $callback = null,
310+
callable $errorCallback = null,
311+
int $endian = null
312+
): ReadRegistersBuilder
313+
{
314+
$r = new ReadRegisterAddress(
315+
$address,
316+
ReadRegisterAddress::TYPE_DOUBLE,
317+
$name,
318+
$callback,
319+
$errorCallback,
320+
$endian
321+
);
322+
return $this->addAddress($r);
323+
}
324+
303325
public function string(
304-
int $address,
305-
int $byteLength,
306-
string $name = null,
326+
int $address,
327+
int $byteLength,
328+
string $name = null,
307329
callable $callback = null,
308330
callable $errorCallback = null,
309-
int $endian = null
331+
int $endian = null
310332
): ReadRegistersBuilder
311333
{
312334
if ($byteLength < 1 || $byteLength > 228) {

src/Composer/Read/Register/ReadRegisterAddress.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class ReadRegisterAddress extends RegisterAddress
2323
private $endian;
2424

2525
public function __construct(
26-
int $address,
27-
string $type,
28-
string $name = null,
26+
int $address,
27+
string $type,
28+
string $name = null,
2929
callable $callback = null,
3030
callable $errorCallback = null,
31-
int $endian = null
31+
int $endian = null
3232
)
3333
{
3434
parent::__construct($address, $type);
@@ -49,6 +49,7 @@ protected function getAllowedTypes(): array
4949
Address::TYPE_INT64,
5050
Address::TYPE_UINT64,
5151
Address::TYPE_FLOAT,
52+
Address::TYPE_DOUBLE,
5253
];
5354
}
5455

@@ -71,6 +72,9 @@ protected function extractInternal(ModbusResponse $response)
7172
case Address::TYPE_FLOAT:
7273
$result = $response->getDoubleWordAt($this->address)->getFloat($this->endian);
7374
break;
75+
case Address::TYPE_DOUBLE:
76+
$result = $response->getQuadWordAt($this->address)->getDouble($this->endian);
77+
break;
7478
case Address::TYPE_INT64:
7579
$result = $response->getQuadWordAt($this->address)->getInt64($this->endian);
7680
break;

src/Composer/RegisterAddress.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function getSize(): int
3939
break;
4040
case Address::TYPE_INT64:
4141
case Address::TYPE_UINT64:
42+
case Address::TYPE_DOUBLE:
4243
$size = 4;
4344
break;
4445
}

src/Composer/Write/Register/WriteRegisterAddress.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ protected function getAllowedTypes(): array
3030
Address::TYPE_INT64,
3131
Address::TYPE_UINT64,
3232
Address::TYPE_FLOAT,
33+
Address::TYPE_DOUBLE,
3334
];
3435
}
3536

@@ -66,6 +67,9 @@ public function toBinary(): string
6667
case Address::TYPE_FLOAT:
6768
$result = Types::toReal($this->getValue());
6869
break;
70+
case Address::TYPE_DOUBLE:
71+
$result = Types::toDouble($this->getValue());
72+
break;
6973

7074
}
7175
return $result;

src/Composer/Write/WriteRegistersBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public function fromArray(array $register): WriteRegistersBuilder
125125
case Address::TYPE_FLOAT:
126126
$this->float($address, $value);
127127
break;
128+
case Address::TYPE_DOUBLE:
129+
$this->double($address, $value);
130+
break;
128131
case Address::TYPE_STRING:
129132
$byteLength = $register['length'] ?? null;
130133
if ($byteLength === null) {
@@ -171,6 +174,11 @@ public function float(int $address, float $value): WriteRegistersBuilder
171174
return $this->addAddress(new WriteRegisterAddress($address, Address::TYPE_FLOAT, $value));
172175
}
173176

177+
public function double(int $address, float $value): WriteRegistersBuilder
178+
{
179+
return $this->addAddress(new WriteRegisterAddress($address, Address::TYPE_DOUBLE, $value));
180+
}
181+
174182
public function string(int $address, string $string, int $byteLength = null): WriteRegistersBuilder
175183
{
176184
return $this->addAddress(new StringWriteRegisterAddress($address, $string, $byteLength));

src/Packet/QuadWord.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public function getInt64(int $endianness = null): int
3232
return Types::parseInt64($this->getData(), $endianness);
3333
}
3434

35+
/**
36+
* @param int $endianness byte and word order for modbus binary data
37+
* @return float
38+
* @throws \RuntimeException
39+
*/
40+
public function getDouble(int $endianness = null): float
41+
{
42+
return Types::parseDouble($this->getData(), $endianness);
43+
}
44+
3545
/**
3646
* @return DoubleWord
3747
* @throws \ModbusTcpClient\Exception\ModbusException
@@ -65,4 +75,4 @@ public static function fromWords(Word $word1, Word $word2, Word $word3, Word $wo
6575
$word4->getData()
6676
);
6777
}
68-
}
78+
}

0 commit comments

Comments
 (0)