function file_write($path, $data, $simple = false, $skip_purge = false) { global $config, $debug; if (preg_match('/^remote:\\/\\/(.+)\\:(.+)$/', $path, $m)) { if (isset($config['remote'][$m[1]])) { require_once 'inc/remote.php'; $remote = new Remote($config['remote'][$m[1]]); $remote->write($data, $m[2]); return; } else { error('Invalid remote server: ' . $m[1]); } } if (!function_exists("dio_truncate")) { if (!($fp = fopen($path, $simple ? 'w' : 'c'))) { error('Unable to open file for writing: ' . $path); } // File locking if (!$simple && !flock($fp, LOCK_EX)) { error('Unable to lock file: ' . $path); } // Truncate file if (!$simple && !ftruncate($fp, 0)) { error('Unable to truncate file: ' . $path); } // Write data if (($bytes = fwrite($fp, $data)) === false) { error('Unable to write to file: ' . $path); } // Unlock if (!$simple) { flock($fp, LOCK_UN); } // Close if (!fclose($fp)) { error('Unable to close file: ' . $path); } } else { if (!($fp = dio_open($path, O_WRONLY | O_CREAT, 0644))) { error('Unable to open file for writing: ' . $path); } // File locking if (dio_fcntl($fp, F_SETLKW, array('type' => F_WRLCK)) === -1) { error('Unable to lock file: ' . $path); } // Truncate file if (!dio_truncate($fp, 0)) { error('Unable to truncate file: ' . $path); } // Write data if (($bytes = dio_write($fp, $data)) === false) { error('Unable to write to file: ' . $path); } // Unlock dio_fcntl($fp, F_SETLK, array('type' => F_UNLCK)); // Close dio_close($fp); } /** * Create gzipped file. * * When writing into a file foo.bar and the size is larger or equal to 1 * KiB, this also produces the gzipped version foo.bar.gz * * This is useful with nginx with gzip_static on. */ if ($config['gzip_static']) { $gzpath = "{$path}.gz"; if ($bytes & ~0x3ff) { // if ($bytes >= 1024) if (file_put_contents($gzpath, gzencode($data), $simple ? 0 : LOCK_EX) === false) { error("Unable to write to file: {$gzpath}"); } if (!touch($gzpath, filemtime($path), fileatime($path))) { error("Unable to touch file: {$gzpath}"); } } else { @unlink($gzpath); } } if (!$skip_purge && isset($config['purge'])) { // Purge cache if (basename($path) == $config['file_index']) { // Index file (/index.html); purge "/" as well $uri = dirname($path); // root if ($uri == '.') { $uri = ''; } else { $uri .= '/'; } purge($uri); } purge($path); } if ($config['debug']) { $debug['write'][] = $path . ': ' . $bytes . ' bytes'; } event('write', $path); }
ob_flush(); } if (!extension_loaded('dio')) { echoFlush("PHP Direct IO does not appear to be installed for more info see: http://www.php.net/manual/en/book.dio.php"); exit; } try { //the serial port resource $bbSerialPort; echoFlush("Connecting to serial port: {$portName}"); if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $bbSerialPort = dio_open($portName, O_RDWR); //we're on windows configure com port from command line exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=on"); } else { $bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK); dio_fcntl($bbSerialPort, F_SETFL, O_SYNC); //we're on 'nix configure com from php direct io function dio_tcsetattr($bbSerialPort, array('baud' => $baudRate, 'bits' => $bits, 'stop' => $spotBit, 'parity' => 0)); } if (!$bbSerialPort) { echoFlush("Could not open Serial port {$portName} "); exit; } // send data $dataToSend = "4 "; echoFlush("Writing to serial port data: \"{$dataToSend}\""); $bytesSent = dio_write($bbSerialPort, $dataToSend); echoFlush("Sent: {$bytesSent} bytes"); /* //date_default_timezone_set ("Europe/London");
<?php $fd = dio_open('/dev/ttyS0', O_RDWR | O_NOCTTY | O_NONBLOCK); dio_fcntl($fd, F_SETFL, O_SYNC); dio_tcsetattr($fd, array('baud' => 9600, 'bits' => 8, 'stop' => 1, 'parity' => 0)); while (1) { $data = dio_read($fd, 256); if ($data) { echo $data; } }
pcntl_signal(SIGINT, "signal_handler"); function signal_handler($signal) { switch ($signal) { case SIGTERM: print "Caught SIGTERM\n"; exit; case SIGKILL: print "Caught SIGKILL\n"; exit; case SIGINT: global $running; $running = false; } } $fd = dio_open('/dev/ttyAMA0', O_RDONLY | O_NOCTTY); # | O_NONBLOCK); dio_fcntl($fd, F_SETFL, O_SYNC); dio_tcsetattr($fd, array('baud' => 19200, 'bits' => 8, 'stop' => 1, 'parity' => 0)); $stats = array(); $requests = array(); $answers = array(); $running = true; $buf = ""; while ($running) { $data = dio_read($fd); if (substr(bin2hex($data), 0, 4) == "0000") { $add = ""; if (substr(bin2hex($buf), 0, 4) == "0000" and substr(bin2hex($buf), 0, 6) != "000000") { $add = "00"; }
public function __construct($device, $flags = 02) { // Attempt to set device... if (!$this->set_device($device)) { throw new \Exception("Unable to set device for serial connection"); } // Check if Direct IO extension installed if (!function_exists("dio_open")) { throw new \Exception("PHP Direct IO is not installed, cannot open serial connection!"); } // Create direct IO file handle with specified flags $this->serial = dio_open($device, $flags); // Set synchronous IO dio_fcntl($this->serial, F_SETFL, O_SYNC); // Set options default $options = array("baud" => self::DEFAULT_BAUD, "bits" => self::DEFAULT_BITS, "stop" => self::DEFAULT_STOP, "parity" => self::DEFAULT_PARITY); $this->set_options($options); return; }
function bbc_write_entry() { global $BBC_CACHE_PATH; $file = $this->filename; $base = basename($file); if (!is_readable($file)) { return array($base, "r"); } if (!is_writable($file)) { return array($base, "w"); } $fp = defined("_BBC_DIO") ? dio_open($file, O_RDWR | O_APPEND) : fopen($file, "ab+"); if (defined("_BBC_DIO") && dio_fcntl($fp, F_SETLK, 1) !== -1) { dio_write($fp, $this->string); dio_fcntl($fp, F_SETLK, 0); $ok = 1; } else { if (defined("_BBC_SEM") ? $id = bbc_semlock($file) : flock($fp, LOCK_EX)) { fputs($fp, $this->string); fflush($fp); defined("_BBC_SEM") ? sem_release($id) : flock($fp, LOCK_UN); $ok = 1; } } defined("_BBC_DIO") ? dio_close($fp) : fclose($fp); return isset($ok) ? array($base, "o") : array($base, "l"); }
function bbc_begin_write($file, $data) { $fp = defined("_BBC_DIO") ? dio_open($file, O_RDWR | O_APPEND) : fopen($file, "rb+"); if (defined("_BBC_DIO") && dio_fcntl($fp, F_SETLK, 1) !== -1) { dio_seek($fp, 0); dio_truncate($fp, 0); dio_write($fp, $data); return $fp; } elseif (defined("_BBC_SEM") ? $id = bbc_semlock($file) : flock($fp, LOCK_EX)) { rewind($fp); fwrite($fp, $data); fflush($fp); ftruncate($fp, ftell($fp)); return defined("_BBC_SEM") ? array($fp, $id) : $fp; } else { defined("_BBC_DIO") ? dio_close($fp) : fclose($fp); return false; } }
function file_write($path, $data, $simple = false, $skip_purge = false) { global $config, $debug; //echo "file_write($path, ", strlen($data), ", $simple, $skip_purge)<br>\n"; $board = ''; if (strpos($path, '/') !== false) { $parts = explode('/', $path, 2); $board = $parts[0]; } $useCache = true; $useFile = false; if ($path == 'main.js') { $useCache = false; $useFile = true; } if ($board && $useCache) { if (!isset($config['cache']['odiliMagicBoards'][$board])) { $useCache = false; $useFile = true; } else { $type = strtolower($config['cache']['odiliMagicBoards'][$board]); if ($type === 'hybrid') { $useFile = true; $useCache = true; } elseif ($type === 'memory') { // defaults will be fine $useCache = true; $useFile = false; } else { $useCache = false; $useFile = true; } } } if ($useCache) { Cache::store('vichan_filecache_' . $path, $data, -1); if ($config['gzip_static']) { $bytes = strlen($data); if ($bytes & ~0x3ff) { Cache::store('vichan_filecache_' . $path . '.gz', gzencode($data), -1); } else { Cache::delete('vichan_filecache_' . $path . '.gz'); } } } if (preg_match('/^remote:\\/\\/(.+)\\:(.+)$/', $path, $m)) { if (isset($config['remote'][$m[1]])) { require_once 'inc/remote.php'; $remote = new Remote($config['remote'][$m[1]]); $remote->write($data, $m[2]); return; } else { error('Invalid remote server: ' . $m[1]); } } if ($useFile) { if (!function_exists("dio_truncate")) { if (!($fp = fopen($path, $simple ? 'w' : 'c'))) { error('Unable to open file for writing: ' . $path); } // File locking if (!$simple && !flock($fp, LOCK_EX)) { error('Unable to lock file: ' . $path); } // Truncate file if (!$simple && !ftruncate($fp, 0)) { error('Unable to truncate file: ' . $path); } // Write data if (($bytes = fwrite($fp, $data)) === false) { error('Unable to write to file: ' . $path); } // Unlock if (!$simple) { flock($fp, LOCK_UN); } // Close if (!fclose($fp)) { error('Unable to close file: ' . $path); } } else { if (!($fp = dio_open($path, O_WRONLY | O_CREAT, 0644))) { error('Unable to open file for writing: ' . $path); } // File locking if (dio_fcntl($fp, F_SETLKW, array('type' => F_WRLCK)) === -1) { error('Unable to lock file: ' . $path); } // Truncate file if (!dio_truncate($fp, 0)) { error('Unable to truncate file: ' . $path); } // Write data if (($bytes = dio_write($fp, $data)) === false) { error('Unable to write to file: ' . $path); } // Unlock dio_fcntl($fp, F_SETLK, array('type' => F_UNLCK)); // Close dio_close($fp); } /** * Create gzipped file. * * When writing into a file foo.bar and the size is larger or equal to 1 * KiB, this also produces the gzipped version foo.bar.gz * * This is useful with nginx with gzip_static on. */ if ($config['gzip_static']) { $gzpath = "{$path}.gz"; if ($bytes & ~0x3ff) { // if ($bytes >= 1024) if (file_put_contents($gzpath, gzencode($data), $simple ? 0 : LOCK_EX) === false) { error("Unable to write to file: {$gzpath}"); } if (!touch($gzpath, filemtime($path), fileatime($path))) { error("Unable to touch file: {$gzpath}"); } } else { @unlink($gzpath); } } } if (!$skip_purge && isset($config['purge'])) { // Purge cache if (basename($path) == $config['file_index']) { // Index file (/index.html); purge "/" as well $uri = dirname($path); // root if ($uri == '.') { $uri = ''; } else { $uri .= '/'; } purge($uri); } purge($path); } if ($config['debug']) { $bytes = strlen($data); $debug['write'][] = $path . ': ' . $bytes . ' bytes'; } event('write', $path); }
/** * 写入 pid 文件 * * @return void */ private function _write_pid() { $fp = dio_open($this->__pid_file, O_WRONLY | O_CREAT, 0644); dio_truncate($fp, 0); dio_write($fp, posix_getpid()); }
protected function spoolData() { // write stdin to a temp file $input = fopen($this->config['inputStream'], "rb"); stream_set_blocking($input, 0); if (extension_loaded('dio') && isset($this->dataConfig['useDio']) && $this->dataConfig['useDio']) { $fd = dio_open($this->tmpPath, O_CREAT | O_NONBLOCK | O_WRONLY); $totalWritten = 0; while ($data = fread($input, $this->config['spoolReadSize'])) { $totalWritten += dio_write($fd, $data); } // make a check that we wrote all of the data that we expected to write // if not throw an exception if (isset($this->params['contentLength']) && $this->params['contentLength'] > 0 && $this->params['contentLength'] != $totalWritten) { dio_close($fd); unlink($this->tmpPath); $msg = sprintf($this->config['exceptionMsgs']['incompleteWrite'], $this->params['contentLength'], $totalWritten); throw new WebDFS_Exception($msg); } $this->fsync($fd); dio_close($fd); fclose($input); } else { $totalWritten = file_put_contents($this->tmpPath, $input); if (isset($this->params['contentLength']) && $this->params['contentLength'] > 0 && $this->params['contentLength'] != $totalWritten) { unlink($this->tmpPath); $msg = sprintf($this->config['exceptionMsgs']['incompleteWrite'], $this->params['contentLength'], $totalWritten); throw new WebDFS_Exception($msg); } } }
function listarSms() { $port = $puerto; //$port = "COM5"; webLog("PORT: {$port}"); try { $config = "mode {$port}: baud=9600 data=8 stop=1 parity=n xon=on"; webLog($config); exec($config); //============================== webLog("OPEN {$port}"); $modem = dio_open("{$port}:", O_RDWR); $c = 0; $res = 'NOK'; while ($c < 20 && $res != 'OK') { $c++; $res = callAT("AT", $modem, false); } if ($res == 'OK') { $res = callAT("AT", $modem); $res = callAT("AT+CMGF=1", $modem); $res = callAT("AT+CMGL=\"ALL\"", $modem); ?> <pre><h1><?php print_r($res); ?> </h1> </pre> <?php $res = explode("+CMGL", $res); foreach ($res as $k => $v) { $v = str_replace(chr(0xa), ',', $v); $res[$k] = mb_split('(,)(?=(?:[^"]|"[^"]*")*$)', $v); } } else { webLog("NO SINCRONIZADO....", "MODEM"); } //============================== webLog("CLOSE {$port}"); dio_close($modem); return $res; } catch (Exception $error) { webLog($error, "ERROR"); } webLog("FIN del proceso.."); return array(); }