Quality ready made and affordable custom software



PHP Serial
Easy Control
Rack Designer

Quick Texts
Wallpaper Slideshow


All articles
WS2300 weather station PHP project
Your own terminal in 17 lines
PC PIC Voltmeter
Network messenger in 22 lines
Phone Dialer




Copyright 2001-2023
© Cosmin Buhu

  Put your serial device on the web

Hot news !!
PHP serial extension for Windows - Handle serial port communications with PHP scripting
Direct link here!

    Another easy method to handle serial port from an web interface (with a script engine like PHP or ASP) is EzCom2Web.
    Is more convenient for people that does not know or need PHP scripting because it uses a simpler Basic language, and contains its own web server, no need to install and run bigger servers like Apache or IIS. Also permits persistent serial ports connections, ie leaving the port open to accumulate data between page loads, which is not possible with PHP serial extension.
New version 3.0 with stronger script engine and new example to send / view SMS with a GSM phone, modem or terminal.

    Following article was written some time ago showing how to deal with PHP and serial ports on Windows using TCP/IP sockets and a TCP to serial port bride / proxy. As TCP bridge Easy Control was used, please note that syntax used in this article is from an older version of Easy Control / PHP.
   This method is considered obsolete and should be replaced with native scripting with serial port support, and PHP serial and EzCom2Web are such products. Complete web interfaces for serial devices (remote cameras, video switcher, ham radio, home automation boxes, media players, GSM modems) can be built with both products.

How to use web server side scripting (PHP, VBS ,ASP...) to put
your serial device on web.

    It is generally known that web server side script engines such as PHP or ASP didn't have their
own libraries to work with the serial ports, and for good reasons. The complexity and the
asynchronous behavior of the serial port and of the devices connected on it are the major
obstacles in writing such general driver libraries.
    Fortunately you had heard about Easy Control and you are reading this article. You'll see
how to add "serial communication capabilities" to your server side scripts, very simple,
elegant and efficient.
    The scenario: the visitor interacts with the web page which has behind it the server side
script. He wants to send a command to the serial port, or read data from it, or to send some data.
The server side script process user's query, and forward it to Easy Control. Easy Control do its
job (open port, close port, send/read data, set timers, process data, etc.) and if it is the case
sends back the reply, whatever is this, data, confirmations, etc. The web server side script use
this reply to output the web page for the visitor.
    But how will communicate the web server side script and Easy Control?
Very simple, through sockets ! Both the server side script and Easy Control can open sockets to
listen for connections and can initiate connections to a pear, to send or receive data.
This is trick !

All this been told, let see an example :
Because I know PHP I'll use it for this example, but other server side script engines can be used.
What we'll do: the PHP script will send a message to Easy Control. Easy Control will open a serial
port (with a modem on it, just for testing purposes), send an initialization string, wait for the modem
to respond, and forward the response to the PHP script which will output the response.
The Easy Control script :
openssrv (80)
rec=readsock ()
lenrec=len (rec)
if (lenrec>>0)
flushsock (lenrec)
openport (com3,9600,8,n,1)
send (com3,"atz"&chr(13)&chr(10))
rec=receive (com3)
posofOK=pos ("OK",rec)
until (posofOK<>0)
flush (com3)
closeport (com3)
writesock (,8090,rec)
goto again
What this small script does:
- opens a sockets server on port 80
- waits a message to arrive on this port
- opens com3 serial port
- sends initialization string to the modem
- waits the reply (ended with the "OK" string)
- closes the serial port
- sends this reply to the PHP script
- and do it again...
The PHP script :
$fp = fsockopen ("", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)\n";
} else {
fputs ($fp, "Message from PHP !".chr(4));
fclose ($fp);
if( ($sock = socket_create( AF_INET, SOCK_STREAM, 0 )) < 0 )
print strerror( $sock ) . "\n";
if( ($ret = socket_bind( $sock, "localhost", 8090 )) < 0 )
print strerror( $ret ) . "\n";
if( ($ret = socket_listen( $sock, 5 )) < 0 )
print strerror( $ret ) . "\n";
if( ($msgsock = socket_accept( $sock )) < 0)
print strerror( $msgsock ) . "\n";
$ret = socket_read( $msgsock, 2048);
socket_close( $msgsock );
socket_close( $sock );
echo substr($ret,0,strlen($ret)-2);
What this PHP script does:
- opens a socket connection and sends a message for Easy Control
- opens a socket to accept connections on it (from Easy Control)
- reads data from connection
- close sockets
- outputs received data

Please note :
- Easy Control reads data from a socket connection until a EOT character arrives (ASCII 4)
- Easy Control appends an EOT character (ASCII 4) at the end of user data to be fully
compliant with the Easy Control socket server
- that's why in the PHP script we appended the chr(4) at the end of the message sent to
Easy Control, and also we must cut the terminal chr(4) from the received data
- the PHP script will be blocked until data arrives from Easy Control
- you must have at least PHP version 4.1.0 and Easy Control 1.4

Final remarks :
- I had very good results using this method. Even with large amounts of transfered data
it works very well.
- the web server and the Easy Control can be on different networked machines
- just think what you can do with your serial device (microcontroller or whatever)
in this way. Endless possibilities !
- and of course don't forget about the other capabilities of the Easy Control, like
virtual instrumentation, and don't forget that Easy Control can save it's rack in a
jpeg image file !

Enjoy, and contact me if you have questions.