note 112243 added to function.fopen
| From: | simon dot riget at gamil dot com | Date: | Tue, 21 May 2013 18:50:13 +0000 |
| Subject: | note 112243 added to function.fopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194875@lists.php.net to get a copy of this message | ||
Writing and reading on a serial port.
If you are unable or unwilling to install the serial device library for PHP, its still possible to
communicate through a serial port or USB device.
There are two issues to note:
- you must use a system call to set the port control options
- you must use NON blocking stream mode for reading (not for writing unless you use flow control)
<?php
// Set timeout to 500 ms
$timeout=microtime(true)+0.5;
// Set device controle options (See man page for stty)
exec("/bin/stty -F /dev/ttyS0 19200 sane raw cs8 hupcl cread clocal -echo -onlcr ");
// Open serial port
$fp=fopen("/dev/ttyS0","c+");
if(!$fp) die("Can't open device");
// Set blocking mode for writing
stream_set_blocking($fp,1);
fwrite($fp,"foo\n");
// Set non blocking mode for reading
stream_set_blocking($fp,0);
do{
// Try to read one character from the device
$c=fgetc($fp);
// Wait for data to arive
if($c === false){
usleep(50000);
continue;
}
$line.=$c;
}while($c!="\n" && microtime(true)<$timeout);
echo "Responce: $line";
?>
----
Server IP: 195.184.96.15
Probable Submitter: 109.198.131.34
----
Manual Page -- http://php.net/manual/en/function.fopen.php
Edit -- https://master.php.net/note/edit/112243
Del: integrated -- https://master.php.net/note/delete/112243/integrated
Del: useless -- https://master.php.net/note/delete/112243/useless
Del: bad code -- https://master.php.net/note/delete/112243/bad+code
Del: spam -- https://master.php.net/note/delete/112243/spam
Del: non-english -- https://master.php.net/note/delete/112243/non-english
Del: in docs -- https://master.php.net/note/delete/112243/in+docs
Del: other reasons-- https://master.php.net/note/delete/112243
Reject -- https://master.php.net/note/reject/112243
Search -- https://master.php.net/manage/user-notes.php