9 April 2007 View Comments

PHP and Serial Ports

Last month there were at least two enquiries at the Bangalore PHP UG on how to get PHP to communicate with the server’s serial port. I didn’t think it was possbile, but here’s what a quick google search turned up:

PHP Serial Extension:

This PHP extension for Windows allows you to communicate directly with the serial port through PHP.

This is a loadable extension for PHP running on Windows implementing serial port handling and communications. Once installed in PHP environment you can programm PHP applications to control serial device hosted on server from remote location; these devices can be anything from video switcher, cameras pan/tilt, ham radios, media players, home automation boxes, GSM modems/terminals (send/receive SMS), only your imagination and needs are the limits.

Link: http://www.easyvitools.com/phpserial/index.html
Link to installation instructions and usage: http://www.easyvitools.com/phpserial/php_ser_reference.html

PHP Serial Class

If you’re a solution in Linux, here’s the PHP class you’re looking for. PHPClasses.org has a PHP Serial Class which allows you interact with your serial ports in Linux

This class can be used to communicate and configure peripherals connected to a serial port under Linux, simplifying the development of applications that need access serial devices. It takes the path of serial device and checks whether it is valid before opening a connection to it. Once the connection is opened, it can send data to the serial port. The class may also change connection parameters for the given serial device.

Link: http://www.phpclasses.org/browse/package/3679.html

If you’ve used any of these solutions for your applications, do let me know your feedback.

Attaining qualifications like 642-425 and 642-432 make one go for 642-444 and 642-511 as well.

View Comments to “PHP and Serial Ports”

  1. Rémy Sanchez 9 April 2007 at 11:28 pm #

    Hello,
    I wrote the phpSerial class, so I could test serial port through PHP. It is working realy fine for controlling a customer display. I am writing additional components for the class in order to be able to read the serial port and use it under windows.

    I will be pleased to answer to questions, as far I can ^^

  2. vinu 10 April 2007 at 9:19 am #

    Thanks Rémy. We’ll be interested to get any updates to your class, especially the ability to read from the serial port.

  3. Rémy Sanchez 2 June 2007 at 3:22 am #

    I just released my new version. Adds :
    - Support of windows
    - Easy configuration of serial port
    - Reading (linux only)

  4. vinu 2 June 2007 at 4:39 pm #

    Thanks for the update Rémy.

  5. wayne hutchison 16 June 2007 at 6:46 am #

    Dear Remy:
    I am developing my own open source home automation/robotics system. I currently use Apache/PHP/Serial Extender in Windows, but need to port all to Linux/Debian/Slackware.
    Could you show me how to use your php_serial.class in PHP?
    I need to send/receive characters from Web page to serial port. Any help would be greatly appreciated :) )

    Sincerely,
    Wayne Hutchison, VoX Automation, LLC

  6. vinu 18 June 2007 at 10:01 am #

    Wayne,
    You can contact Rémy over at:
    http://remy.is-a-geek.org/moi/

  7. stonicus 6 July 2007 at 8:29 pm #

    Awesome class. How soon before you’ll have read capability for Windows?

  8. Rémy Sanchez 25 July 2007 at 5:38 pm #

    Thanks for the “awsome” =D
    But I don’t think I ever manage to get reading for windows… I already tried a lot of different functions, and each time it acts as if the stream was empty… So for a pure PHP implementation I don’t think it is possible (or maybe on win 9x only, but actualy I have no time for testing it).
    Maybe you can connect to some serproxy server instead

  9. Ilya 18 August 2007 at 3:11 am #

    Hello!

    Great class! One problem though that I couldn’t find a solution for.

    When I receive a response back from the RS-232 device I communicate with, for some reason my script
    (utilizing the serial class) echoes every character of the response back to the serial device.
    E.g. when I connect to the serial port with hyper terminal and send a command to it — it works fine, but when I type something back on the hyper terminal I get every single character echoed back — so that have 2 symbols for each one I type on the screen if the local echoing is on and
    just 1 symbol for each one I type if the local echoing is off.
    Could anyone please tell me what I need to do to disable the echoing back? I’d really appreciate your help.

    Thanks!

  10. Rav 15 March 2008 at 2:02 pm #

    Hey.. this isn’t working for me.. I am using Fedors core 4.. I tried to connect thru Stty1 and it does nothing…. no error msg too..

  11. Lara 17 March 2008 at 2:52 pm #

    Hi
    I need to send some data to a printer conected to serial port (com)
    and i wnat know the function in PHP that did this operation
    Thanks

  12. Mike FIeld 18 March 2008 at 3:42 am #

    Hi,

    I am able to read/write to a serial port under windows with nothing more exotic than the code below. I am just working on exec()ing a ‘mode’ command to set the port speeds and so on.

    Mike
    <?php
    function readInput($f,$t)
    {
    $i=0;
    $buffer = “”;
    while($i “.$buffer;
    }

    error_reporting(E_ALL);

    $f = fopen(“COM4″,”r+”);
    if(!$f)
    {
    die(“Unable to open serial port\n”);
    }
    $i=0;
    fwrite($f,”Hello\r\n”);
    while(true)
    {
    fwrite($f,”AT-”);
    sleep(1);
    readInput($f,5);
    fwrite($f,”AT+CMGF=1-”);
    sleep(1);
    readInput($f,5);
    fwrite($f,”AT+CMGW=\”0212266816\”-”);
    sleep(1);
    readInput($f,5);
    fwrite($f,”AT+CMSS=1-”);
    sleep(1);
    readInput($f,5);
    $i++;
    }

    fclose($f);
    ?>

  13. EDWINSPIRE 21 April 2008 at 10:40 am #

    Saludos:
    He programado en PHP un software que permite comunicarse con el puerto serial, enviarle un comando AT y leer su respuesta.
    El software esta diseñado para enviar y recibir SMS tanto en formato PDU como en modo Texo.
    Para mas informacion y descargas visiten edwinspire.890m.com hagan clic en PROGRAMACION PHP

    Translation (google translate):

    Hello!
    I programmed a PHP software that allows you to communicate with the serial port, send an AT command and read its response.
    The software is designed to send and receive SMS in either mode as PDU Texo.
    For more information and downloads visit edwinspire.890m.com click on PHP Programming

  14. alfredo 15 May 2008 at 6:21 am #

    hey mike fleld

    I think your code is incomplete in
    function readInput($f,$t)
    {
    $i=0;
    $buffer = “�;
    while($i “.$buffer;
    }

    can you check it and post again

  15. Ever 17 May 2008 at 12:26 am #

    Hi everybody!! I was wondering if some could help me! I need to make a button that sends something trough the serial port without having to refresh all the web page. Something that u click and u see an inmediate reaction in the port. Thanks. Anything would be usefull!

  16. edwinspire 19 May 2008 at 6:50 pm #

    Saludos nuevamente:
    Quiero comunicarles a las personas que visitan el foro y buscan informacion acerca de PHP, PHP-GTK2 y/o comunicacion con el puerto serie desde PHP pueden comunicarse conmigo a mi email.

    Esta disponible el nuevo codigo fuente de la Aplicacion PHP_GSM, para descargarla en http://edwinspire.890m.com

    Hay dos versiones:

    - 1 – Una integramente hecha en PHP (Pero en las pruebas realizadas bajo windows se ha notado un alto consumo de memoria RAM y algo de lentitud en la ejecucion del programa.)

    - 2 – Otra version donde la parte correspondiente a la comunicacion con el puerto esta Programada en C, el resto del programa sigue escrito en PHP (Con este cambio se noto una mejora notable en el desempeño del programa)

    Probado unicamente con el Motorola L6i sin ningun problema, y diseñado tanto para Win como para LINUX.

  17. Rod 13 June 2008 at 1:20 am #

    I would like to know if you have documentation on how to implement communication with customer display for a POS project im working on. Are the commands etc different from manufactures? where can i get the commands, etc sample will be great thanks.

  18. B Karthick 24 July 2008 at 12:02 am #

    Dear Rod,

    Usually the manufacturer supplies code (in C, Java and VB). It may work with small modification for all Displays.

  19. Tracker 11 September 2008 at 6:44 am #

    Hello. Has anyone seen or heard from Rémy Sanchez? The class has an error line 503 and Id like to know more about it. There is no documentation for the class and his personal website has vanished.

  20. vinu 19 September 2008 at 12:42 pm #

    Tracker,
    You can get the contact details for Rémy Sanchez from the phpclasses site:
    http://www.phpclasses.org/browse/author/428373.html

  21. John 31 January 2009 at 8:28 pm #

    Hey, just wondering is there any way of reading serial port with windows, help

    Thanks

  22. vinu 3 February 2009 at 9:59 am #

    There seems to be a third party component called ActiveComPort which allows you to communicate with Serial Ports in Windows.
    http://www.activexperts.com/activcomport/
    Here’s their PHP example:
    http://www.activexperts.com/activcomport/howto/php/


Trackbacks/Pingbacks.

  1. developercast.com » Vinu Thomas’ Blog: PHP and Serial Ports - 16. Apr, 2007

    [...] a recent post to his blog, Vinu Thomas posted about a PHP extension that allows your scripts to directly interact with the serial ports on the web [...]

  2. Notícias e links - 03/05 « PHP-BR - 04. May, 2007

    [...] PHP and Serial Ports [...]

Leave a Reply

blog comments powered by Disqus