parent
2fbbbc5842
commit
95ecf4bf5b
@ -1,11 +0,0 @@ |
||||
<?php |
||||
|
||||
namespace PHPModbus; |
||||
|
||||
/** |
||||
* Exception class thrown when a socket operation failure happens. |
||||
*/ |
||||
class IOException extends \RuntimeException |
||||
{ |
||||
|
||||
} |
@ -0,0 +1,12 @@ |
||||
<?php |
||||
namespace PHPModbus\Network; |
||||
|
||||
use PHPModbus\ModbusException; |
||||
|
||||
/** |
||||
* Exception class thrown when a modbus network connection operation failure happens. |
||||
*/ |
||||
class IOException extends ModbusException |
||||
{ |
||||
|
||||
} |
@ -0,0 +1,111 @@ |
||||
<?php |
||||
|
||||
namespace PHPModbus\Network; |
||||
|
||||
|
||||
class ModbusConnectionBuilder extends ModbusConnectionProperties |
||||
{ |
||||
/** |
||||
* Return built instance of ModbusConnection |
||||
* |
||||
* @return ModbusConnection built instance |
||||
* @throws \LogicException |
||||
*/ |
||||
public function build() |
||||
{ |
||||
if (empty($this->host)) { |
||||
throw new \LogicException('host property can not be left null or empty!'); |
||||
} |
||||
return new ModbusConnection($this); |
||||
} |
||||
|
||||
/** |
||||
* @param string $client |
||||
* @return ModbusConnectionBuilder |
||||
*/ |
||||
public function setClient($client) |
||||
{ |
||||
$this->client = $client; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @param string $clientPort |
||||
* @return ModbusConnectionBuilder |
||||
*/ |
||||
public function setClientPort($clientPort) |
||||
{ |
||||
$this->clientPort = $clientPort; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @param float $timeoutSec |
||||
* @return ModbusConnectionBuilder |
||||
*/ |
||||
public function setTimeoutSec($timeoutSec) |
||||
{ |
||||
$this->timeoutSec = $timeoutSec; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @param float $connectTimeoutSec |
||||
* @return ModbusConnectionBuilder |
||||
*/ |
||||
public function setConnectTimeoutSec($connectTimeoutSec) |
||||
{ |
||||
$this->connectTimeoutSec = $connectTimeoutSec; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @param float $readTimeoutSec |
||||
* @return ModbusConnectionBuilder |
||||
*/ |
||||
public function setReadTimeoutSec($readTimeoutSec) |
||||
{ |
||||
$this->readTimeoutSec = $readTimeoutSec; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @param float $writeTimeoutSec |
||||
* @return ModbusConnectionBuilder |
||||
*/ |
||||
public function setWriteTimeoutSec($writeTimeoutSec) |
||||
{ |
||||
$this->writeTimeoutSec = $writeTimeoutSec; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @param string $protocol |
||||
* @return ModbusConnectionBuilder |
||||
*/ |
||||
public function setProtocol($protocol) |
||||
{ |
||||
$this->protocol = $protocol; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @param string $host |
||||
* @return ModbusConnectionBuilder |
||||
*/ |
||||
public function setHost($host) |
||||
{ |
||||
$this->host = $host; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @param string $port |
||||
* @return ModbusConnectionBuilder |
||||
*/ |
||||
public function setPort($port) |
||||
{ |
||||
$this->port = $port; |
||||
return $this; |
||||
} |
||||
} |
@ -0,0 +1,120 @@ |
||||
<?php |
||||
namespace PHPModbus\Network; |
||||
|
||||
/** |
||||
* ModbusConnection immutable properties base class |
||||
*/ |
||||
abstract class ModbusConnectionProperties |
||||
{ |
||||
/** |
||||
* @var string (optional) client IP address when binding client |
||||
*/ |
||||
protected $client = ''; |
||||
/** |
||||
* @var string client port set when binding client to local ip&port |
||||
*/ |
||||
protected $clientPort = 502; |
||||
/** |
||||
* @var float Total response timeout (seconds, decimals allowed) |
||||
*/ |
||||
protected $timeoutSec = 5; |
||||
/** |
||||
* @var float maximum timeout when establishing connection (seconds, decimals allowed) |
||||
*/ |
||||
protected $connectTimeoutSec = 1; |
||||
/** |
||||
* @var float read timeout (seconds, decimals allowed) |
||||
*/ |
||||
protected $readTimeoutSec = 0.3; // 300 ms |
||||
/** |
||||
* @var float maximum timeout for write operation on connection (seconds, decimals allowed) |
||||
*/ |
||||
protected $writeTimeoutSec = 1; |
||||
/** |
||||
* @var string network protocol (TCP, UDP) |
||||
*/ |
||||
protected $protocol = 'UDP'; |
||||
/** |
||||
* @var string|null Modbus device IP address |
||||
*/ |
||||
protected $host; |
||||
/** |
||||
* @var string gateway port |
||||
*/ |
||||
protected $port = 502; |
||||
|
||||
|
||||
|
||||
/** |
||||
* @return string (optional) client IP address when binding client |
||||
*/ |
||||
public function getClient() |
||||
{ |
||||
return $this->client; |
||||
} |
||||
|
||||
/** |
||||
* @return string client port set when binding client to local ip&port |
||||
*/ |
||||
public function getClientPort() |
||||
{ |
||||
return $this->clientPort; |
||||
} |
||||
|
||||
/** |
||||
* @return float Total response timeout (seconds, decimals allowed) |
||||
*/ |
||||
public function getTimeoutSec() |
||||
{ |
||||
return $this->timeoutSec; |
||||
} |
||||
|
||||
/** |
||||
* @return float maximum timeout when establishing connection (seconds, decimals allowed) |
||||
*/ |
||||
public function getConnectTimeoutSec() |
||||
{ |
||||
return $this->connectTimeoutSec; |
||||
} |
||||
|
||||
/** |
||||
* @return float read timeout (seconds, decimals allowed) |
||||
*/ |
||||
public function getReadTimeoutSec() |
||||
{ |
||||
return $this->readTimeoutSec; |
||||
} |
||||
|
||||
/** |
||||
* @return float maximum timeout for write operation on connection (seconds, decimals allowed) |
||||
*/ |
||||
public function getWriteTimeoutSec() |
||||
{ |
||||
return $this->writeTimeoutSec; |
||||
} |
||||
|
||||
/** |
||||
* @return string network protocol (TCP, UDP) |
||||
*/ |
||||
public function getProtocol() |
||||
{ |
||||
return $this->protocol; |
||||
} |
||||
|
||||
/** |
||||
* @return string Modbus device IP address |
||||
*/ |
||||
public function getHost() |
||||
{ |
||||
return $this->host; |
||||
} |
||||
|
||||
/** |
||||
* @return string gateway port |
||||
*/ |
||||
public function getPort() |
||||
{ |
||||
return $this->port; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue