src.dualinventive.com/dinet/sec-multi-proxy/common/ditest-php/equipment/serial_port.php

149 lines
2.8 KiB
PHP
Executable File

<?php
require_once(dirname(__FILE__) . "/../test_framework.php");
class SerialPort {
private $__componentName = "SerialPort";
private $name = "serial_port";
private $debug = false;
private $__fd = null;
private function debug($msg) {
if ($this->debug) {
$msg = str_replace("\r", '\r', $msg);
$msg = str_replace("\n", '\n', $msg);
DiTestMsg::dbg(trim($msg), $this->__componentName);
}
}
private function err($msg) {
DiTestMsg::err(trim($msg), $this->__componentName);
}
private function warn($msg) {
DiTestMsg::warn(trim($msg), $this->__componentName);
}
private function fail($msg) {
DiTestMsg::fail(trim($msg), $this->__componentName);
}
private function pass($msg) {
DiTestMsg::pass(trim($msg), $this->__componentName);
}
function __construct($host, $port = 2001, $debug = false) {
$this->debug = $debug;
$this->__fd = fsockopen($host, $port, $errno, $errstr, 3);
if ($this->__fd === FALSE) {
$this->fail("$errstr ($errno)");
} else {
$this->debug($host);
$this->debug($port);
$this->setInfo("host", $host);
$this->setInfo("port", $port);
}
}
function __destruct() {
if ($this->__fd) {
fclose($this->__fd);
$this->__fd = null;
}
}
function setInfo($key, $value) {
$this->__info[$key] = $value;
}
function getInfo($key) {
return $this->__info[$key];
}
private function reply() {
if ($this->__fd) {
$reply = fgets($this->__fd);
$this->debug("[reply] \"$reply\"");
return $reply;
}
return "";
}
private function request($cmd) {
if ($this->__fd) {
$cmd .= "\r\n";
$this->debug("[request] cmd: \"$cmd\"");
fputs($this->__fd, $cmd);
}
}
public function flush() {
stream_set_blocking($this->__fd, false);
stream_set_timeout($this->__fd, 1);
do {
$rep = $this->reply();
} while ($rep);
stream_set_blocking($this->__fd, true);
}
public function sendMessage($msg) {
$this->request($msg);
}
public function getMessage() {
// eat echo
$rep = $this->reply();
// get reply
$rep = $this->reply();
return $rep;
}
public function getState() {
$this->flush();
$this->sendMessage("devdata");
$rep = $this->reply(); // eat echo
$rep = $this->reply(); // state
var_dump($rep);
return $rep;
}
public function getToken() {
$this->flush();
$this->sendMessage("devdata");
$rep = $this->reply(); // eat echo
$rep = $this->reply(); // state
$rep = $this->reply(); // token
var_dump($rep);
return $rep;
}
public function getError() {
$this->flush();
$this->sendMessage("devdata");
$rep = $this->reply(); // eat echo
$rep = $this->reply(); // state
$rep = $this->reply(); // token
$rep = $this->reply(); // error
var_dump($rep);
if (strpos($rep, "error: true" ))
return true;
return false;
}
public function getReply() {
$rep = $this->reply();
return $rep;
}
}