Skip to content

Commit

Permalink
drop php5.6, add read/write packet composer
Browse files Browse the repository at this point in the history
Breaking Changes:

* php 5.6 support is dropped - added return types etc 
* reworked exception logic:
** 'ModbusException' namespace changed from 'ModbusTcpClient' to 'ModbusTcpClient\Exception'
** all thrown exceptions are now 'ModbusException' or subclasses of it
* 'ReadCoilsRequest' quantity has max size of 2048 because response can not hold more than that

Added:
* Int64 support
* ReadRegistersBuilder/WriteRegistersBuilder - to help compose multiple packets with higher level API
* 'ModbusTcpClient\Network\BinaryStreamConnection' has new property 'uri' which can be used instead of  protocol+host+port
* ModbusTcpClient\Network\NonBlockingClient - client class to send packets or reader/writer classes as non-blocking stream IO (request responses are processed in parallel)

Fixed: 
* 'ErrorResponse' to binary string had incorrect value for 'length' field
  • Loading branch information
aldas committed Jun 2, 2018
2 parents 275b51d + ce4f11a commit dfa1aea
Show file tree
Hide file tree
Showing 103 changed files with 5,374 additions and 984 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ cache:
- $HOME/.composer/cache

php:
- 5.6
- 7.0
- 7.1
- 7.2
Expand Down
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ composer require aldas/modbus-tcp-client

## Requirements

* PHP 5.6+ (64bit PHP! 32bit php does not support 64bit ints and overflows with 32bit unsigned integers when 32th bit is set)
* PHP 7.0+ (64bit PHP if you need to read 64bit integers from modbus responses. NB: PHP supports only signed 64bit integers so usigned 64bit ints will overflow when 64th bit is set)
* Release [0.2.0](https://github.com/aldas/modbus-tcp-client/tree/0.2.0) was last to support PHP 5.6

## Intention
This library is influenced by [phpmodbus](https://github.com/adduc/phpmodbus) library and meant to be provide decoupled Modbus protocol (request/response packets) and networking related features so you could build modbus client with our own choice of networking code (ext_sockets/streams/Reactphp/Amp asynchronous streams) or use library provided networking classes (php Streams)
Expand All @@ -47,8 +48,50 @@ See [Endian.php](src/Utils/Endian.php) for additional info and [Types.php](src/U

## Example (fc3 - read holding registers)

Some of the Modbus function examples are in `examples/` folder
Some of the Modbus function examples are in [examples/](examples) folder

Advanced usage:
* command line poller with ReachPHP [examples/example_cli_poller.php](examples/example_cli_poller.php)
* send/recieve packets parallel using non-blocking IO:
* using [ReactPHP](https://reactphp.org/) see 'examples/[example_parallel_requests_reactphp.php](examples/example_parallel_requests_reactphp.php)'
* using [Amp](https://amphp.org/amp/) see 'examples/[example_parallel_requests_amp.php](examples/example_parallel_requests_amp.php)'

Request multiple packets with higher level API:
```php
$fc3 = ReadRegistersBuilder::newReadHoldingRegisters('tcp://127.0.0.1:5022')
->bit(256, 15, 'pump2_feedbackalarm_do')
// will be split into 2 requests as 1 request can return only range of 124 registers max
->int16(657, 'battery3_voltage_wo')
// will be another request as uri is different for subsequent int16 register
->useUri('tcp://127.0.0.1:5023')
->string(
669,
10,
'username_plc2',
function ($value, $address, $response) {
return 'prefix_' . $value; // optional: transform value after extraction
},
function (\Exception $exception, Address $address, $response) {
// optional: callback called then extraction failed with an error
return $address->getType() === Address::TYPE_STRING ? '' : null; // does not make sense but gives you an idea
}
)
->build(); // returns array of 3 ReadHoldingRegistersRequest requests

// this will use PHP non-blocking stream io to recieve responses
$responses = (new NonBlockingClient(['readTimeoutSec' => 0.2]))->sendReadRequests($fc3);
print_r($responses);
```
Response structure
```php
[
[ 'pump2_feedbackalarm_do' => true, ],
[ 'battery3_voltage_wo' => 12, ],
[ 'username_plc2' => 'prefix_admin', ]
]
```

Low level - send packets:
```php
$connection = BinaryStreamConnection::getBuilder()
->setHost('192.168.0.1')
Expand Down
14 changes: 9 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
"type": "library",
"license": "Apache-2.0",
"require": {
"php": "^5.6 || ^7.0",
"psr/log": "^1.0"
"php": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^5.6",
"react/datagram": "^1.1",
"react/child-process": "^0.4.1"
"phpunit/phpunit": "^6.0",
"react/datagram": "^1.4",
"react/socket": "^0.8",
"react/child-process": "^0.5.2",
"psr/log": "^1.0"
},
"suggest": {
"psr/log": "Required for using the Log middleware with BinaryStreamConnection"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit dfa1aea

Please sign in to comment.