Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd2ae72b79 |
@@ -64,7 +64,7 @@ class ModbusMaster
|
|||||||
public $status;
|
public $status;
|
||||||
|
|
||||||
/** @var float Total response timeout (seconds, decimals allowed) */
|
/** @var float Total response timeout (seconds, decimals allowed) */
|
||||||
public $timeout_sec = 5;
|
public $timeout_sec = 3;
|
||||||
|
|
||||||
/** @var float Socket read timeout (seconds, decimals allowed) */
|
/** @var float Socket read timeout (seconds, decimals allowed) */
|
||||||
public $socket_read_timeout_sec = 0.3; // 300 ms
|
public $socket_read_timeout_sec = 0.3; // 300 ms
|
||||||
@@ -205,16 +205,18 @@ class ModbusMaster
|
|||||||
$rec = "";
|
$rec = "";
|
||||||
$totalReadTimeout = $this->timeout_sec;
|
$totalReadTimeout = $this->timeout_sec;
|
||||||
$lastAccess = microtime(true);
|
$lastAccess = microtime(true);
|
||||||
|
$readStart = microtime(true);
|
||||||
$readTout = $this->secsToSecUsecArray($this->socket_read_timeout_sec);
|
$readTout = $this->secsToSecUsecArray($this->socket_read_timeout_sec);
|
||||||
|
|
||||||
while (false !== socket_select($readsocks, $writesocks, $exceptsocks, $readTout['sec'], $readTout['usec'])) {
|
while (false !== socket_select($readsocks, $writesocks, $exceptsocks, $readTout['sec'], $readTout['usec'])) {
|
||||||
$this->status .= "Wait data ... \n";
|
|
||||||
if (in_array($this->sock, $readsocks)) {
|
if (in_array($this->sock, $readsocks)) {
|
||||||
if (@socket_recv($this->sock, $rec, 2000, 0)) { // read max 2000 bytes
|
if ($recn = @socket_recv($this->sock, $rec, 2000, 0)) { // read max 2000 bytes
|
||||||
$this->status .= "Data received \n";
|
$this->status .= "Data received \n";
|
||||||
return $rec;
|
return $rec;
|
||||||
}
|
}
|
||||||
$lastAccess = microtime(true);
|
$lastAccess = microtime(true);
|
||||||
|
if (microtime(true) - $readStart > $totalReadTimeout) {
|
||||||
|
throw new Exception("Read timed out, last error: " . socket_strerror(socket_last_error($this->sock)));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$timeSpentWaiting = microtime(true) - $lastAccess;
|
$timeSpentWaiting = microtime(true) - $lastAccess;
|
||||||
if ($timeSpentWaiting >= $totalReadTimeout) {
|
if ($timeSpentWaiting >= $totalReadTimeout) {
|
||||||
|
|||||||
Reference in New Issue
Block a user