Skip to content

Commit

Permalink
sendRequest fix error detection
Browse files Browse the repository at this point in the history
  • Loading branch information
alphp committed Oct 12, 2021
1 parent 2aa0f92 commit 66154d6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fawno/modbus",
"version": "0.1.0",
"version": "0.1.1",
"description": "Modbus RTU serial protocol in PHP",
"keywords": ["Modbus RTU", "DDS238"],
"license": "MIT",
Expand Down
11 changes: 4 additions & 7 deletions src/ModbusRTU.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class ModbusRTU extends SerialDio {
public const MODBUS_ADU = 'C1station/C1function/C*data/';
public const MODBUS_ERROR = 'C1station/C1error/C1exception/';

private const MODBUS_REQUEST = 'C2n2';
//public const MODBUS_RESPONSE = 'C1station/C1function/C1count/n*records/';

protected $_options = [
'data_rate' => 9600,
'data_bits' => 8,
Expand Down Expand Up @@ -434,10 +431,6 @@ public function sendRequest (string $request) {
throw new Exception(sprintf('Response lenght too short: "%s"', bin2hex($response)), -1);
}

if (substr($response, -2) != $this->crc16(substr($response, 0, -2))) {
throw new Exception('Error check fails', -2);
}

$adu_request = unpack(self::MODBUS_ADU, $request);
$adu_response = unpack(self::MODBUS_ERROR, $response);
if ($adu_request['function'] != $adu_response['error']) {
Expand All @@ -449,6 +442,10 @@ public function sendRequest (string $request) {
}
}

if (substr($response, -2) != $this->crc16(substr($response, 0, -2))) {
throw new Exception(sprintf('Error check fails: "%s"', bin2hex($response)), -2);
}

return $response;
}
}

0 comments on commit 66154d6

Please sign in to comment.