File modules/modbusy/request/Request.class.php
Last commit: Thu Dec 26 19:50:18 2024 +0100 Jan Dankert New: ModbusTCP for later use in the scriptbox.
1 <?php 2 namespace modbusy\request; 3 4 5 use Closure; 6 7 class Request 8 { 9 /** 10 * @var integer 11 */ 12 protected $functionCode; 13 /** 14 * @var mixed 15 */ 16 protected $socket; 17 /** 18 * @var Closure 19 */ 20 protected $log; 21 22 public function __construct() 23 { 24 25 } 26 27 public function setClient( $client ) 28 { 29 $this->client = $client; 30 } 31 public function setFunctionCode( $fc ) 32 { 33 $this->functionCode = $fc; 34 } 35 public function setSocket( $socket ) 36 { 37 $this->socket = $socket; 38 } 39 40 public function setTransactionId( $transactionId ) 41 { 42 $this->transactionId = $transactionId; 43 return $this; 44 } 45 46 public function setUnit($unitId ) 47 { 48 $this->unitId = $unitId; 49 return $this; 50 } 51 52 public $transactionId = 1024; // optional 53 public $unitId = 1; 54 55 const PROTOCOL_IDENTIFIER = "\x00\x00"; 56 57 const FUNCTION_Read_Multiple_Holding_Registers = 3; 58 const ERRORS = [ 59 1 => 'Illegal Function Function code received in the query is not recognized or allowed by server', 60 2 => 'Illegal Data Address Data address of some or all the required entities are not allowed or do not exist in server', 61 3 => 'Illegal Data Value Value is not accepted by server', 62 4 => 'Server Device Failure Unrecoverable error occurred while server was attempting to perform requested action', 63 5 => 'Acknowledge Server has accepted request and is processing it, but a long duration of time is required. This response is returned to prevent a timeout error from occurring in the client. client can next issue a Poll Program Complete message to determine whether processing is completed', 64 6 => 'Server Device Busy Server is engaged in processing a long-duration command; client should retry later', 65 7 => 'Negative Acknowledge Server cannot perform the programming functions; client should request diagnostic or error information from server', 66 8 => 'Memory Parity Error Server detected a parity error in memory; client can retry the request', 67 10=> 'Gateway Path Unavailable Specialized for Modbus gateways: indicates a misconfigured gateway', 68 11=> 'Gateway Target Device Failed to Respond', 69 ]; 70 71 72 function strToHex($string){ 73 $hex=[]; 74 for ($i=0; $i < strlen($string); $i++){ 75 $hex[] = strtoupper(str_pad(dechex(ord($string[$i])),2,'0',STR_PAD_LEFT)); 76 } 77 return implode(' ',$hex); 78 } 79 80 81 protected static function hexDump( $data, $newline="\n") 82 { 83 $width = 16; # number of bytes per line 84 $pad = '.'; # padding for non-visible characters 85 86 $from = ''; 87 $to = ''; 88 $output = ''; 89 90 for ($i=0; $i<=0xFF; $i++) 91 { 92 $from .= chr($i); 93 $to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad; 94 } 95 96 $hex = str_split(bin2hex($data), $width*2); 97 $chars = str_split(strtr($data, $from, $to), $width); 98 99 foreach ($hex as $i=>$line) 100 $output .= 101 implode(' ',array_pad(str_split($chars[$i]),16,' ') ) . ' ['.str_pad($chars[$i],16).']' . $newline . 102 implode(' ' ,array_pad(str_split($line ,2),16,' ') ) . $newline; 103 return $output; 104 } 105 106 public function setLog(Closure $log) 107 { 108 $this->log = $log; 109 } 110 111 112 protected function logHex( $text,$value ) 113 { 114 $this->log( $text.': HEX:'.bin2hex($value).' ('.strlen($value).' bytes)'); 115 } 116 117 protected function logHexDump($text,$value) 118 { 119 $this->log( $text.': '."\n".self::hexDump($value)); 120 } 121 122 123 124 public function log( $log ) 125 { 126 if ( $this->log ) 127 call_user_func($this->log,$log ); 128 } 129 }
Downloadmodules/modbusy/request/Request.class.php
History Thu, 26 Dec 2024 19:50:18 +0100 Jan Dankert New: ModbusTCP for later use in the scriptbox.