serialProxy
Gepost door Ilias B op 03-02-2010 23:56.
Class die je kan gebruiken zodat php met een serieele poort kan praten onder windows of linux. Je zal wel het tooltje serproxy moeten compileren onder unix of windows. De windows binairy kan je ook downloaden van: http://www.lspace.nildram.co.uk/freeware.html
Revisie 3
Bestanden van dit script
index.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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | <?php
/**
* SerialProxy
*
* This PHP5 Class allows you to set up a serproxy 'server', This allowing multiple machines to control single or multiple serial connections.
*
* @package serialProxy
*
* @author Ilias B <ilias twixel.be>
* @copyright (c) 2010 Twixel
* @link www.twixel.be
* @license GPL
* @version 1.0.1.4
* @uses serproxy => <http://www.lspace.nildram.co.uk/freeware.html> Windows & *nix based
*
* Installation:
*
* Unix:
* 1.) Download the binairy or source from @link <http://www.lspace.nildram.co.uk/freeware.html>
* 2.) Install or compile the source to a subdirectory of your applications working directory. (For example "serproxy/") (Compile command "sudo make realclean && make serproxy.c")
* 3.) Auto start the serproxy binairy. This can be done using a cron job starting the binairy or using the serialPorxy::connect() function in a seperate PHP call (Don't forget to include the serialProxy class)
* 4.) Set permission 755 (sudo chmod 755 serproxy.php && serproxy binairy)
* 5.) Edit the serproxy.cfg file if required (Optional)
* 6.) Include the class in your php webapplication (require_once('serialProxy.php'))
* 7.) Use the serialProxy public functions an variables the desired configuration.
*
* Windows:
* 1.) Download the binairy or source from @link <http://www.lspace.nildram.co.uk/freeware.html>
* 2.) Install or compile the source to a subdirectory of your applications working directory. (For example "serproxy/") (Use Visual Studio to compile)
* 3.) Auto start the serproxy binairy. This can be done using windows task sheduler (Load @ startup) or using the serialPorxy::connect() function in a seperate PHP call (Don't forget to include the serialProxy class)
* 4.) Set execution permission on serproxy.php && serproxy.exe (Right click on file => properties)
* 5.) Edit the serproxy.cfg file if required (Optional)
* 6.) Include the class in your php webapplication (require_once('serialProxy.php'))
* 7.) Use the serialProxy public functions an variables the desired configuration.
*
* Usage:
*
* - Starting serproxy binairy
* $st = new serialProxy();
* $st->connect();
*
* - Start serialProxy instance
* $sp = new serialProxy();
*
* - Change the working directory (Optional)
* $sp->cwd = '/serproxy';
*
* - Change the name of the binairy (Optional)
* $sp->cmd = 'serproxy.exe'; (Windows) or $sp->cmd = 'serproxy'; in Linux
*
* - Change the file buffer size (Optional)
* $sp->bufsize = 1024;
*
* - Change the available serial ports (This should be the same as in the serproxy.cfg file)
* $sp->serPorts = array('COM1'=>5331,
* 'COM2'=>5332,
* 'COM3'=>5333,
* 'COM4'=>5334,
* 'COM5'=>5335,
* 'COM6'=>5336,
* 'COM7'=>5337,
* 'COM8'=>5338,
* 'COM9'=>5339,
* 'COM10'=>5340);
*
* - Change the serproxy server
* $sp->serHost = 'localhost';
*
* - Set the COM port you are going to be using
* $sp->setCom('COM3');
*
* - Send a command to the serial port
* $result = $sp->sendCommand('HELLO WORLD');
*
* if(!result){
* - Something went wrong , use the $sp->output to return the error commands
* echo $sp->output;
* }else{
* echo $result;
* }
*
*
*/
class serialProxy{
/**
* The printed result to be returned when connecting
* @var string $output
*
* The command for executing the serproxy binairy
* @var string $cmd
*
* The working directory of the binairy
* @var string $cwd
*
* The maximum buffer size used
* @var integer $bufsize
*
* The serproxy server name or IP
* @var string $serHost
*
* The serproxy listening port
* @var integer $serPorts
*
* Process identifier file
* @var string $pidFile
*
* @access public
*/
public $output,$cmd,$cwd,$bufSize,$serPorts,$serHost,$pidFile;
/**
* Pipes descriptions
* @var integer $fdRead
* @var integer $fdError
* @var integer $ipPort
* @access private
*/
private $fdRead,$fdError,$ipPort;
/**
* Constructor
* @access public
* @return void
*/
public function __construct(){
/* Disable execution time limitation in php_ini */
set_time_limit(0);
$this->cmd = $this->isWindows() ? 'serproxy.exe':'serproxy';
$this->pidFile = 'data.pid';
$this->cwd = getcwd().'/serproxy';
$this->serPorts = array('COM1'=>5331,
'COM2'=>5332,
'COM3'=>5333,
'COM4'=>5334,
'COM5'=>5335,
'COM6'=>5336,
'COM7'=>5337,
'COM8'=>5338,
'COM9'=>5339,
'COM10'=>5340);
$this->ipPort = NULL;
$this->serHost = 'localhost';
$this->output = '';
$this->bufSize = 1024;
$this->fdRead = 1; //stdout
$this->fdError = 2; //stderr
}
/**
* Connect function
* @access public
* @return boolean
* @todo build pid check
*/
public function connect(){
/* Check if we already started up the serproxy app */
$this->pidCheck();
$descriptorspec = array(0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w"));
$options = array('suppress_errors'=>TRUE); /* PHP Bug => suppress_errors */
$ptr = proc_open($this->cmd, $descriptorspec, $pipes, $this->cwd, NULL,$options);
if (!is_resource($ptr)){
return FALSE;
}
/* Store this process identifier in a file */
$this->pidFile('w',$ptr);
while (($buffer = fgets($pipes[$this->fdRead], $this->bufSize)) != NULL || ($errbuf = fgets($pipes[$this->fdError], $this->bufSize)) != NULL) {
if (!isset($flag)) {
$pstatus = proc_get_status($ptr);
$first_exitcode = $pstatus["exitcode"];
$flag = TRUE;
}
if (strlen($buffer)){
$this->output = 'CONNECT RETURN: ' .$buffer;
return TRUE;
}
if (strlen($errbuf)){
$this->output = 'CONNECT ERROR: ' . $errbuf;
return FALSE;
}
}
foreach ($pipes as $pipe) fclose($pipe);
/* Get the expected *exit* code to return the value */
$pstatus = proc_get_status($ptr);
if (!strlen($pstatus["exitcode"]) || $pstatus["running"]) {
/* we can trust the retval of proc_close() */
if ($pstatus["running"]){
proc_terminate($ptr);
$ret = proc_close($ptr);
}
} else {
if ((($first_exitcode + 256) % 256) == 255 && (($pstatus["exitcode"] + 256) % 256) != 255){
$ret = $pstatus["exitcode"];
}elseif (!strlen($first_exitcode)){
$ret = $pstatus["exitcode"];
}elseif ((($first_exitcode + 256) % 256) != 255){
$ret = $first_exitcode;
}else{
$ret = 0; /* we "deduce" an EXIT_SUCCESS ;) */
proc_close($ptr);
}
}
$this->output = 'CONNECT RETURN: ' .($ret + 256) % 256;
return TRUE;
}
/**
* sendCommand function
* @param string $command
* @access public
* @return boolean || string
*/
public function sendCommand($command){
if (!empty($this->ipPort)){
$this->output = 'COMMAND RETURN: Please use the setConfig function first.';
return FALSE;
}
$fp = fsockopen ($this->serHost, $this->serPort, $errno, $errstr, 30);
if (!$fp) {
$this->output = 'COMMAND ERROR: '.$errstr .'('.$errno.')';
return FALSE;
} else {
$this->output = 'EXECUTING COMMAND: '.$string;
fputs ($fp, $command );
return fgets($fp,$this->bufSize);
fclose ($fp);
}
}
/**
* setCom function - Set the COM port you want to use
* @access public
* @param string $comport
* @return boolean
*/
public function setCom($comPort){
if (!key_exists($comPort,$this->serPorts)){
$this->output = 'CONFIG ERROR: (' .$comPort .') isn\'t a valid com port.';
return FALSE;
}else{
/* Select the COM port we are going to be using */
$this->ipPort = $this->serPorts[$comPort];
return TRUE;
}
}
/**
* isWindows function - Checks if the local operating system is windows.
* @access protected
* @return boolean
*/
protected function isWindows(){
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? TRUE:FALSE;
}
/**
* pidCheck function - See if the process is already running.
* @access protected
* @return mixed
*/
protected function pidCheck(){
return exec('taskkill /im serproxy.exe /f');
}
}
//Start the serproxy thread (Do this in a seperate call! At apache startup or with a cron job)
//require_once('serialProxy.php')
$sp = new serialProxy();
$sp->connect();
?> |

