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:

And directly from SVN at:


While I write proper PHP documentation below is a brief over view of how you use DIO.


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.

		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;

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!


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.

Tags: , ,

8 Responses to “PHP DIO Extension: Looking for beta testers”

  1. siddesh Says:

    it is giving me error on line fopen telling no such file…………
    i also dint get why you are using dio.serial://com1?
    does it gives serial port name

  2. Hakan Says:


    How to linux system set RtsEnable = true ; ?
    if flow_control => 1 = rts then

    not working 🙂 ?


  3. shibi Says:

    I am using that code it shows the error

    Warning: fopen() [function.fopen]: Unable to find the wrapper “dio.serial” – did you forget to enable it when you configured PHP?

    if you have any idea means share with me…

  4. Andy Stangeland Says:

    Any chance you’ll make a Windows DLL available? I’ve been using the old dio extension that came bundled with PHP 5.0.5. I’d love to try this one out and see if it fixes the problems I’ve been having with the old one. However compiling from source is a little beyond me.

  5. Doug Miller Says:

    I’m with Andy. Will there be a dll available (or a makefile made available?)


  6. Krisno W Utomo Says:

    Hi Andy Stangeland, I have the compiled dll using PHP v 5.3.1 source. You can download it at Sorry for my bad english.

  7. Ajit Kumar Moharana Says:

    Thanks to provide a DLL for PHP Direct Input Output Stream

  8. SJZurek Says:

    You can now download binary files for 5.3 and 5.4 from:
    Kudos to weltling, cyberspice and others.

    [Cyberspice] – These are windows DLL files.

Leave a Reply