Reading COM (serial modem) in PHP

Reading COM (serial modem) in PHP

I need a COM interface (Windows, COM2) to read from PHP.

This demonstration continues. Reading is a problem, it works sometimes.

Is there any other way (no dio, no C ++), maybe w32api_register_function()better?


    function rs232init ($ com, $ bautrate)
    {
    `mode $ com: BAUD = $ bautrate PARITY = N data = 8 stop = 1 xon = off`;
    }

    function send ($ comport, $ char)
    {

         $ fp = fopen ("$ comport", "w +");
         if (! $ fp)
          {
             echo "not open for read";
          }
        else {
                fputs ($ fp, $ char);
                 fclose ($ fp);
                }
    }

    function read ($ comport2, $ sek)
    {

       $ buffer = "";

       $ fp2 = fopen ("$ comport2", "r +");
         if (! $ fp2)
         {
       echo "port is open for read";
       }
    else
      {
      sleep ($ sek);
             $ buffer. = fgets ($ fp2, 4096);
            }
          return $ buffer;
          fclose ($ fp2);
}


rs232init ("com2", "9600");
send ("com2", "3"); 
$ a = read ("com2", "2"); 
echo $ a; 
+5
source share
2 answers

The com2 device should be referred to as "COM2:"

+1
source

I must point out that there is a PHP serial class already available at http://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html .

I donโ€™t know what methods he uses internally, but perhaps this will make it a little easier to get started.

0
source

All Articles