Из счетчика можно вытащить много разных интересных параметров (потребление за день, месяц и т.д). Но эти параметры не нужно запрашивать у счетчика при каждом обращении к нему (каждые n секунд).
Было бы не плохо в модуле иметь возможность задавать время(частоту) запросов для отдельных параметров. Хотя может реализовать это сложно именно в модуле??
Я создал скрипт, на примере файла test.php, и он благополучно получает необходимые данные. НО как эти данные засунуть в свойство объекта???? В php я не силен и не могу из полученного массива выделить величину, для того чтобы потом использовать setGlobal.
Вот пример скрипта, (счетчик СЕ102М).
Код: Выделить всё
/*** iek61107 device
* @package project
* @author Wizard <sergejey@gmail.com>
* @copyright http://majordomo.smartliving.ru/ (c)
* @version 0.1 (wizard, 16:09:07 [Sep 03, 2016])
*/
include_once('C:\_majordomo\htdocs\modules\energomera\PhpSerial.php');
class iek61107{
public $Serial;
public $debug = false;
public $WaitBeforeRead = 0.5;
function iek61107($device){
$serial = new phpSerial;
$serial->deviceSet($device);
$serial->confBaudRate(9600);
$serial->confParity("even");
$serial->confCharacterLength(7);
$serial->confStopBits(1);
$this->Serial = $serial;
}
/**
* connect
* Connect the device
* @return bool
*/
function connect(){
if($this->debug) echo date("Y-m-d H:i:s")." Connecting COM\n";
$result = $this->Serial->deviceOpen("w+b");
if ($result === false) {
throw new Exception("serrial.open() failed");
}
if($this->debug) echo date("Y-m-d H:i:s")." Connected\n";
stream_set_timeout($this->Serial->_dHandle, 0, 3500000);
return true;
}
/**
* disconnect
* Disconnect the device
*/
function disconnect(){
$this->Serial->sendMessage(hex2bin("0142300375"), $this->WaitBeforeRead);
$this->Serial->deviceClose();
if($this->debug) echo date("Y-m-d H:i:s")." Disconnected\n";
}
function init(){
//=== #1
// /?!..
// /EKT5CE102Mv01..
$result = $this->Serial->sendMessage(hex2bin("2F3F210D0A"), $this->WaitBeforeRead);
if ($result === false)
{
//if($this->debug)
echo date("Y-m-d H:i:s")." Error send init\n";
return $result;
}
if($this->debug) echo date("Y-m-d H:i:s")." Send init #1 \n";
$ch = $this->Serial->readPort(3500);
if (empty($ch))
{
$result = $this->Serial->sendMessage(hex2bin("2F3F210D0A"), $this->WaitBeforeRead);
if ($result === false)
{
//if($this->debug) echo
date("Y-m-d H:i:s")." Error send init #1-2\n";
return $result;
}
if($this->debug) echo date("Y-m-d H:i:s")." Send init #1-2 \n";
$ch = $this->Serial->readPort(3500);
if (empty($ch))
{
if($this->debug) echo date("Y-m-d H:i:s")." Init timeout\n";
return false;
}
}
if ($ch != hex2bin("2F454B543543453130324D7630310D0A"))
{
//if($this->debug)
echo date("Y-m-d H:i:s")." Device not equal: ".$ch."\n";
return false;
}
//=== #2
// .051..
// .P0.(www.energomera.ru).#
$result = $this->Serial->sendMessage(hex2bin("063035310D0A"), $this->WaitBeforeRead);
if ($result === false)
{
//if($this->debug)
echo date("Y-m-d H:i:s")." Error send init #2\n";
return $result;
}
if($this->debug) echo date("Y-m-d H:i:s")." Send init #2 \n";
$ch = $this->Serial->readPort(3500);
// Model
if($this->debug) echo date("Y-m-d H:i:s")." model:".$ch."\n";
return true;
}
function getValue($val, $timeout = 3500)
{
// if($this->debug) echo date("Y-m-d H:i:s")." Read ".$val." ";
$data = "\1R1\2".$val."\3";
$cs = 0;
for ($i=1;$i<strlen($data);$i++)
$cs = $cs + ord($data[$i]);
$cs = $cs % 128;
$data = $data . chr($cs);
$result = $this->Serial->sendMessage($data, $this->WaitBeforeRead);
if ($result === false)
{
//if($this->debug)
echo date("Y-m-d H:i:s")." Error send init #2\n";
return $result;
}
$data = $this->Serial->readPort($timeout);
if (empty($data))
{
//if($this->debug)
echo date("Y-m-d H:i:s")." Time out\n";
return (false);
}
//TODO check CS
$data = substr($data, 1, strlen($data)-3);
$arr = explode("\r\n", $data);
$ret = array();
$lastkey = "";
for ($i=0; $i < count($arr); $i++)
{
$str = trim($arr[$i]);
if ($str == "") continue;
// Get key
$start = strpos($str, "(");
$stop = strpos($str, ")", $start);
$key = substr($str, 0, $start);
$val = substr($str, $start+1, $stop-$start-1);
if ($key != "") $lastkey = $key;
if (!array_key_exists($lastkey, $ret))
$ret[$lastkey] = array();
$arritm = $ret[$lastkey];
$arritm[] = $val;
$ret[$lastkey] = $arritm;
}
return $ret;
}
}
//include_once("C:\_majordomo\htdocs\modules\energomera\iek61107.class.php");
function ShowVal($dev, $val, $timeout = 3500)
{
$arr = $dev->getValue($val, $timeout);
echo "<pre>$val = ".htmlspecialchars(print_r($arr,true))."</pre><br>\n";
}
$dev = new iek61107("COM3");
$dev->debug = true;
$ret = $dev->connect();
if ($ret === false)
{
echo "ops1";
die;
}
$ret = $dev->init();
if ($ret === false)
{
echo "ops1";
die;
}
echo "<br>\n";
ShowVal($dev, "SNUMB()");
//ShowVal($dev, "VOLTA()");
//ShowVal($dev, "CURRE()");
//ShowVal($dev, "POWEP()");
//ShowVal($dev, "FREQU()");
//ShowVal($dev, "COS_f()");
//ShowVal($dev, "ET0PE()");
//ShowVal($dev, "MSYAD()");
//ShowVal($dev, "V_BAT()");
//ShowVal($dev, "TEMPR()");
//ShowVal($dev, "EAMPE(10.16,01)");
//ShowVal($dev, "LOG01()", 20000);
//setGlobal()
$dev->disconnect();