PHP DIO Extension: Looking for beta testers
A few months ago I had a need to do some serial IO from PHP. After asking around I found out there was a PHP extension for this but it was unmaintained, unowned and out of date. To cut a long story short I ended up as the maintainer of said extension and since them I have been fixing and extending it.
The original DIO API is very basic and POSIX oriented. Serial support is not very configurable and doesn’t work at all on Window platforms. So I have been working on implementing PHP stream extensions that allow you to do raw and serial IO via streams. Anyway its ready for testing by people other than me on POSIX (Linux, OS X etc) and Windows platforms so I’m looking for beta testers.
Where to get it
The release candidate is downloadable as a package from:
http://www.cyberspice.org.uk/downloads/dio-0.0.4rc3.tgz
And directly from SVN at:
http://svn.php.net/repository/pecl/dio/tags/RELEASE_0_0_4RC3
Documentation
While I write proper PHP documentation below is a brief over view of how you use DIO.
Streams
DIO streams are just PHP streams. You use fopen() to open them and fread(), fwrite(), fgets(), stream_set_blocking() etc. There are two DIO streams, dio.raw and dio.serial.
dio.raw is as it sounds, raw streams. It uses the low level OS streams such as open(), read(), write(), close() etc. on POSIX and CreateFile(), OpenFile(), ReadFile(), WriteFile(), CloseFile() on Windows. It does not buffer except the internal PHP buffering.
dio.serial is full serial support. It allows setting of data rates, data size, stop bits, handshaking and raw/canonical modes etc.
Both stream types support timeouts and blocking/non-blocking use.
Example code
Below is a small example that opens a serial port, configuring it with a stream context, and then reads and writes from it before closing it.
<?php
// Create a stream context that configures the serial port
// And enables canonical input.
$c = stream_context_create(array('dio' =>
array('data_rate' => 115200,
'data_bits' => 8,
'stop_bits' => 1,
'parity' => 0,
'is_canonical' => 1)));
// Are we POSIX or Windows? POSIX platforms do not have a
// Standard port naming scheme so it could be /dev/ttyUSB0
// or some long /dev/tty.serial_port_name_thingy on OSX.
if (PATH_SEPARATOR != ";") {
$filename = "dio.serial:///dev/ttyS0";
} else {
$filename = "dio.serial://COM1";
}
// Open the stream for read only and use it.
$f = fopen($filename, "r+", false, $c);
if ($f) {
echo "Writing to port...\n";
fprintf($f,"Hello world\n");
echo "Reading from port...\n";
$data = fgets($f);
if ($data) {
echo $data;
}
fclose($f);
}
?>
Stream context
DIO uses the stream context to obtain the port configuration settings. There are settings which are common to all DIO streams and settings which are sub-stream specific. The common settings are:
perms Integer Sets the file permissions mask when creating a file (POSIX specific, ignored on Windows)
is_blocking Boolean Indicates whether the stream is blocking (true, default) or non blocking (false)
timeout_secs Integer The time to wait for input in seconds before returning. Also sets the stream non blocking.
timeout_usecs Integer The time to wait for input in micro seconds before returning. Used in conjunction with timeout_secs. If omitted but timeout_secs is defined it is assumed to be zero.
Both the blocking setting and the timeout settings can changed after the stream is open using the PHP functions stream_set_blocking() and stream_set_timeout() respectively.
The serial stream has the following additional configuration settings.
data_rate Integer Sets the data rate in bits per second (Not BAUD. BAUD is symbols per second. A symbol may include more than one bit)
data_bits Integer Number of bits per byte. Typically 5, 6, 7 or 8 (default).
stop_bits Integer The number of stop bits. 1 (default), 2 or 3 (1.5 stop bits)
parity Integer The parity checking. 0 (None, default), 1 (Odd), 2 (Even).
flow_control Boolean Enables/disables hardware flow control
is_canonical Boolean Enables canonical input mode. Canonical input buffers the input until either internal buffer is full or an end of line has been received. So a read in canonical mode will block until an end of line or file even if more than the requested characters have been read from the remote machine.
Full canonical mode handles control characters, delete, echoing characters back to the remote machine and so on. Its basically used to implement a terminal. DIO doesn’t have any of the latter other than the buffering. Yet!
Finally
So there you are. Any results, good or bad, are useful. DIO has error checking and will return useful warnings if you try and feed it silly values. Logs, PHP versions, etc. would be useful diagnosing issues. Thanks.
A few months ago I had a need to do some serial IO from PHP. After asking around I found out there was a PHP extension for this but it was unmaintained, unowned and out of date. To cut a long story short I ended up as the maintainer of said extension and since them I have been fixing and extending it.
The original DIO API is very basic and POSIX oriented. Serial support is not very configurable and doesn’t work at all on Window platforms. So I have been working on implementing PHP stream extensions that allow you to do raw and serial IO via streams. Anyway its ready for testing by people other than me on POSIX (Linux, OS X etc) and Windows platforms so I’m looking for beta testers.
Where to get it
The release candidate is downloadable as a package from:
http://www.cyberspice.org.uk/downloads/dio-0.0.4rc3.tgz
And directly from SVN at:
http://svn.php.net/repository/pecl/dio/tags/RELEASE_0_0_4RC3
Documentation
While I write proper PHP documentation below is a brief over view of how you use DIO.
Streams
DIO streams are just PHP streams. You use fopen() to open them and fread(), fwrite(), fgets(), stream_set_blocking() etc. There are two DIO streams, dio.raw and dio.serial.
dio.raw is as it sounds, raw streams. It uses the low level OS streams such as open(), read(), write(), close() etc. on POSIX and CreateFile(), OpenFile(), ReadFile(), WriteFile(), CloseFile() on Windows. It does not buffer except the internal PHP buffering.
dio.serial is full serial support. It allows setting of data rates, data size, stop bits, handshaking and raw/canonical modes etc.
Both stream types support timeouts and blocking/non-blocking use.
Example code
Below is a small example that opens a serial port, configuring it with a stream context, and then reads and writes from it before closing it.
<?php
// Create a stream context that configures the serial port
// And enables canonical input.
$c = stream_context_create(array('dio' =>
array('data_rate' => 115200,
'data_bits' => 8,
'stop_bits' => 1,
'parity' => 0,
'is_canonical' => 1)));
// Are we POSIX or Windows? POSIX platforms do not have a
// Standard port naming scheme so it could be /dev/ttyUSB0
// or some long /dev/tty.serial_port_name_thingy on OSX.
if (PATH_SEPARATOR != ";") {
$filename = "dio.serial:///dev/ttyS0";
} else {
$filename = "dio.serial://COM1";
}
// Open the stream for read only and use it.
$f = fopen($filename, "r+", false, $c);
if ($f) {
echo "Writing to port...\n";
fprintf($f,"Hello world\n");
echo "Reading from port...\n";
$data = fgets($f);
if ($data) {
echo $data;
}
fclose($f);
}
?>
Stream context
DIO uses the stream context to obtain the port configuration settings. There are settings which are common to all DIO streams and settings which are sub-stream specific. The common settings are:
perms Integer Sets the file permissions mask when creating a file (POSIX specific, ignored on Windows)
is_blocking Boolean Indicates whether the stream is blocking (true, default) or non blocking (false)
timeout_secs Integer The time to wait for input in seconds before returning. Also sets the stream non blocking.
timeout_usecs Integer The time to wait for input in micro seconds before returning. Used in conjunction with timeout_secs. If omitted but timeout_secs is defined it is assumed to be zero.
Both the blocking setting and the timeout settings can changed after the stream is open using the PHP functions stream_set_blocking() and stream_set_timeout() respectively.
The serial stream has the following additional configuration settings.
data_rate Integer Sets the data rate in bits per second (Not BAUD. BAUD is symbols per second. A symbol may include more than one bit)
data_bits Integer Number of bits per byte. Typically 5, 6, 7 or 8 (default).
stop_bits Integer The number of stop bits. 1 (default), 2 or 3 (1.5 stop bits)
parity Integer The parity checking. 0 (None, default), 1 (Odd), 2 (Even).
flow_control Boolean Enables/disables hardware flow control
is_canonical Boolean Enables canonical input mode. Canonical input buffers the input until either internal buffer is full or an end of line has been received. So a read in canonical mode will block until an end of line or file even if more than the requested characters have been read from the remote machine.
Full canonical mode handles control characters, delete, echoing characters back to the remote machine and so on. Its basically used to implement a terminal. DIO doesn’t have any of the latter other than the buffering. Yet!
Finally
So there you are. Any results, good or bad, are useful. DIO has error checking and will return useful warnings if you try and feed it silly values. Logs, PHP versions, etc. would be useful diagnosing issues. Thanks.
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/2833/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表