用串口通讯,如果采用vc,vb,delphi这个问题就比较容易解决,现在有个想法就是用php来做,这样的话就出现两个大的问题,一个是页面的刷新,一个是串口通讯。请大家来讨论,这种做法可行吗?刷新页面是否可以通过ajax来做呢?

解决方案 »

  1.   

    1.php与com通讯
      php官方站点提供了sample
      link:http://jp.php.net/manual/zh/function.fopen.php
    2.可以用ajax来刷新页面sample:
      <?php
    // HOW TO USE PHP TO WRITE TO YOUR SERIAL PORT: TWO METHODS
    $serproxy=true;
    if ($serproxy) {
        // Use this code in conjunction with SERPROXY.EXE
        // (http://www.lspace.nildram.co.uk/freeware.html)
        // which converts a Serial stream to a TCP/IP stream
        $fp = fsockopen ("localhost", 5331, $errno, $errstr, 30);
        if (!$fp) {
            echo "$errstr ($errno)";
        } else {
            $e = chr(27);
            $string  = $e . "A" . $e . "H300";
            $string .= $e . "V100" . $e . "XL1SATO";
            $string .= $e . "Q1" . $e . "Z";
            echo $string;
            fputs ($fp, $string );
            fclose ($fp);
        }
    } elseif ($com1) {
        // Use this code to write directly to the COM1 serial port
        // First, you want to set the mode of the port. You need to set
        // it only once; it will remain the same until you reboot.
        // Note: the backticks on the following line will execute the
        // DOS 'mode' command from within PHP
        `mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
        $fp = fopen ("COM1:", "w+");
        if (!$fp) {
            echo "Uh-oh. Port not opened.";
        } else {
            $e = chr(27);
            $string  = $e . "A" . $e . "H300";
            $string .= $e . "V100" . $e . "XL1SATO";
            $string .= $e . "Q1" . $e . "Z";
            echo $string;
            fputs ($fp, $string );
            fclose ($fp);
        }
    }
    ?>
      

  2.   

    我想这样设计是否可以:1. php程序完成串口数据的发送。
    2. 用c编写服务在服务器运行,接收串口数据,更新数据库
    3. ajax定请求检测数据库,数据更新则返回新数据,完成页面刷新(比如颜色改变、参数值改变)这样的好处就是多处监控只需要在服务器安装一套程序,方便维护。