-
Notifications
You must be signed in to change notification settings - Fork 1
/
elicom_scale.php
57 lines (52 loc) · 1.3 KB
/
elicom_scale.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
<?php
// За Windows
if (substr(php_uname(), 0, 7) === "Windows") {
exec("mode COM1 96,n,8");
}
/*
* 000050A - нестабилно 50 гр.
* 000150B - стабилно 150 гр.
*
**/
$tmpl = "<?xml version='1.0' encoding='UTF-8' ?>" .
"<monitor>" .
"<Devices>" .
"<item>" .
"<Name>D19</Name>" .
"<Entries>" .
"<item>" .
"<Unit>KG</Unit>" .
"<ID>20</ID>" .
"<Value>[#0.000#]</Value>" . // UNSTABLE, NO_CONNECTION, PORT_BUSSY
"<Name>ElectronicScale1</Name>" .
"</item>" .
"</Entries>" .
"</item>" .
"</Devices>" .
"</monitor>";
//define ('DEVICE', '/dev/ttyUSB0');
//define ('DEVICE', '/dev/ttyS0');
define ('DEVICE', 'COM1');
clearstatcache();
$fp = fopen(DEVICE,'r');
//stream_set_blocking($fp, 0);
//stream_set_timeout($fp, 2);
$res = "";
$stable = false;
// Търсим първият стринг с дължина 6 и завършващ на 'B'
while ($stable != true) {
$res .= fgetc($fp);
if (strlen($res)>6 && (false !== strpos($res, "B"))) {
$stable = true;
}
}
fclose($fp);
$weight = substr($res,strlen($res)-7,3) . "." . substr($res,strlen($res)-4,3);
$weight = number_format($weight, 3, '.', '');
$err = false;
if (!$err) {
$tmpl = str_replace('[#0.000#]', $weight, $tmpl);
} else {
$tmpl = str_replace('[#0.000#]', 'PORT_BUSSY', $tmpl);
}
//echo $tmpl;