BayEOS-PHP
 All Data Structures Namespaces Files Functions Variables Pages
eurotherm2704.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
3 /*
4  * Command line script to read out Eurotherm 2704 via ModbusTCP
5  *
6  *
7  */
8 
9 require_once 'BayEOSGatewayClient.php';
10 
11 $config=parse_ini_file('/etc/eurotherm2704.ini');
12 /*
13  * Read configuration file and set some defaults
14 */
15 if(! isset($config['names'])){
16  $names=array();
17  for($i=0;$i<count($config['host']);$i++){
18  $names[$i]='IP.'.$config['host'][$i];
19  }
20 } else $names=$config['names'];
21 
22 if(! isset($config['sender'])) $config['sender']=$names;
23 
24 /*
25  * Check hosts
26  */
27 for($i=0;$i<count($config['host']);$i++){
28  if(! preg_match('/^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$/',$config['host'][$i]))
29  die($config['host'][$i]." is not a valid IP address\n");
30 }
31 
32 
33 //Extend BayEOSGatewayClient Class
35  protected function readData(){
36  $host=$this->getOption('host');
37  $name=$this->names[$this->i];
38  $addr=array(1,5,1025,1029,2049,2053,
39  12197,12198,12199,12200,12201,12202,
40  12204,12205);
41  $namen=array('Istwert Temperatur Prüfraum','Sollwert Temperatur Prüfraum',
42  'Istwert Feuchte Prüfraum','Sollwert Feuchte Prüfraum',
43  'Istwert Temperatur Lampenraum','Sollwert Temperatur Lampenraum',
44  'Lampengruppe 1','Lampengruppe 2','Lampengruppe 3','Lampengruppe 4','Lampengruppe 5','Lampengruppe 6',
45  'Störung','Störung Kaltsolesatz');
46 
47  $fp=fsockopen($host,502, $errno, $errstr, 10);
48  if(! $fp){
49  fwrite(STDERR, date('Y-m-d H:i:s')." $name: No Socket to $host: $errstr ($errno)\n");
50  return FALSE;
51  }
52  $values=array();
53  $modbus_error=0;
54  for($i=0;$i<count($addr);$i++){
55  $req=$this->readMultipleRegistersPacketBuilder(255, $addr[$i], 1);
56  // echo bin2hex($req)."\n";
57  fputs($fp,$req);
58  $packet=fread($fp,11);
59  // echo bin2hex($packet)."\n";
60  if((ord($packet[7]) & 0x80) > 0) $modbus_error=1;
61  if($modbus_error){
62  fwrite(STDERR, date('Y-m-d H:i:s')." $name: Modbus Error $host\n");
63  return FALSE;
64  }
65 
66  $int=BayEOSType::unpackINT16($packet[9].$packet[10],1);
67  if($i<6) $int/=10;
68  $values[]=$int;
69  }
70  fclose($fp);
71  return $values;
72 
73  }
74 
75  private function readMultipleRegistersPacketBuilder($unitId, $reference, $quantity){
76  $dataLen = 0;
77  // build data section
78  $buffer1 = "";
79  // build body
80  $buffer2 = "";
81  $buffer2 .= BayEOSType::BYTE(3); // FC 3 = 3(0x03)
82  // build body - read section
83  $buffer2 .= BayEOSType::UINT16($reference,1); // refnumber = 12288
84  $buffer2 .= BayEOSType::UINT16($quantity,1); // quantity
85  $dataLen += 5;
86  // build header
87  $buffer3 = '';
88  $buffer3 .= BayEOSType::UINT16(rand(0,65000),1); // transaction ID
89  $buffer3 .= BayEOSType::UINT16(0,1); // protocol ID
90  $buffer3 .= BayEOSType::UINT16($dataLen + 1,1); // lenght
91  $buffer3 .= BayEOSType::BYTE($unitId); //unit ID
92  // return packet string
93  return $buffer3. $buffer2. $buffer1;
94  }
95 
96 
97 }
98 
99 
100 
101 
103 $my_client->run();
104 
105 
106 
107 
108 
109 
110 ?>
static UINT16($value, $endianness=0)
static BYTE($value)
static unpackINT16($value, $endianness=0)
$config
$fp
Definition: testFifo.php:5
if(!isset($config['names'])) else $names
getOption($key, $default='')
$my_client