-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreload_modbus.php
106 lines (81 loc) · 3.13 KB
/
reload_modbus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
// application/json und UTF8 for php api
header("Content-Type: application/json; charset=utf-8");
require_once('Phpmodbus/ModbusMaster.php');
require_once('setup_modbus.php');
$json_setup = json_decode($setup,true);
$response = array('modbus'=>array());
foreach($json_setup as $setup) // each modbus api
{
$modbus_name = $setup['name'];
$modbus_url = $setup['url'];
$modbus_protocol = $setup['protocol'];
$modbus_success = true;
$modbus_error = '';
$modbus_meters = array();
// Modbus master
$modbus = new ModbusMaster($modbus_url, $modbus_protocol);
foreach($setup['meters'] as $meter) // each meter in meters
{
$name = $meter['name'];
$unit_of_measurement = $meter['unit_of_measurement'];
$data_type = $meter['data_type'];
$unit_id = $meter['unit_id'];
$address = $meter['address'];
$scale = $meter['scale'];
try {
// FC 3
if ($data_type=="int16") {
$recData = $modbus->readMultipleRegisters($unit_id, $address, 1);
$values = array_chunk($recData, 2);
foreach($values as $bytes) { $return_value = PhpType::bytes2signedInt($bytes); }
$scaled_value = $return_value / $scale;
}
if ($data_type=="uint16") {
$recData = $modbus->readMultipleRegisters($unit_id, $address, 1);
$values = array_chunk($recData, 2);
foreach($values as $bytes) { $return_value = PhpType::bytes2unsignedInt($bytes); }
$scaled_value = $return_value / $scale;
}
if ($data_type=="int32") {
$recData = $modbus->readMultipleRegisters($unit_id, $address, 2);
$values = array_chunk($recData, 2);
foreach($values as $bytes) { $return_value = PhpType::bytes2signedInt($bytes); }
$scaled_value = $return_value / $scale;
}
if ($data_type=="uint32") {
$recData = $modbus->readMultipleRegisters($unit_id, $address, 2);
$values = array_chunk($recData, 2);
foreach($values as $bytes) { $return_value = PhpType::bytes2unsignedInt($bytes, false); }
$scaled_value = $return_value / $scale;
}
if ($data_type=="string") {
$recData = $modbus->readMultipleRegisters($unit_id, $address, 1);
$scaled_value = PhpType::bytes2string($recData);
}
//echo '<br>'.$name.': '.$scaled_value.' '.$unit_of_measurement.'<br>';
$meters_item = array('name'=>$name,'value'=>$scaled_value,'unit_of_measurement'=>$unit_of_measurement,'unit_id'=>$unit_id,'address'=>$address);
array_push($modbus_meters,$meters_item);
}
catch (Exception $e) {
// Print error information if any
$modbus_success = false;
//echo $modbus;
//echo $e;
$modbus_error = $e->getMessage();
// end forrach
break;
}
}
$unixtimestamp = time();
$modbus_item = array('success'=>$modbus_success,'unixtimestamp'=>$unixtimestamp,'name'=>$modbus_name,'url'=>$modbus_url,'protocol'=>$modbus_protocol,'meters'=>$modbus_meters,'error'=>$modbus_error,'log'=>$modbus);
array_push($response['modbus'],$modbus_item);
}
try {
// Convert array to json string
$responsestr = json_encode($response);
} catch (Exception $e) {
$responsestr = $e->getMessage();
}
echo $responsestr;
?>