【问题标题】:Read response from command on serial port从串行端口上的命令读取响应
【发布时间】:2018-06-14 17:29:03
【问题描述】:

我正在编写一个 PHP 应用程序,该应用程序使用串行设备来处理 GSM 调制解调器。这样你就可以通过screen与串口通信,写一个命令就会得到响应。

我使用 PHP 与我的串口通信并使用sleep 在命令之间等待,我使用fopenw+ 标志和fwrite 发送命令。我尝试使用fread 函数来检查响应是否存在,但事实并非如此。用 PHP 怎么办?

【问题讨论】:

    标签: php serial-port usb hardware


    【解决方案1】:

    从概念上讲,如果端口配置正确,f_read 应该可以工作。 https://github.com/Xowap/PHP-Serial

    上有一个库 php-serial

    这是readPort() 函数:

    public function readPort($count = 0)
        {
            if ($this->_dState !== SERIAL_DEVICE_OPENED) {
                trigger_error("Device must be opened to read it", E_USER_WARNING);
                return false;
            }
            if ($this->_os === "linux" || $this->_os === "osx") {
                // Behavior in OSX isn't to wait for new data to recover, but just
                // grabs what's there!
                // Doesn't always work perfectly for me in OSX
                $content = ""; $i = 0;
                if ($count !== 0) {
                    do {
                        if ($i > $count) {
                            $content .= fread($this->_dHandle, ($count - $i));
                        } else {
                            $content .= fread($this->_dHandle, 128);
                        }
                    } while (($i += 128) === strlen($content));
                } else {
                    do {
                        $content .= fread($this->_dHandle, 128);
                    } while (($i += 128) === strlen($content));
                }
                return $content;
            } elseif ($this->_os === "windows") {
                // Windows port reading procedures still buggy
                $content = ""; $i = 0;
                if ($count !== 0) {
                    do {
                        if ($i > $count) {
                            $content .= fread($this->_dHandle, ($count - $i));
                        } else {
                            $content .= fread($this->_dHandle, 128);
                        }
                    } while (($i += 128) === strlen($content));
                } else {
                    do {
                        $content .= fread($this->_dHandle, 128);
                    } while (($i 
    
    += 128) === strlen($content));
            }
            return $content;
        }
        return false;
    }
    

    (How to Read RS232 Serial Port in PHP like this QBasic Program)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 2011-02-08
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      相关资源
      最近更新 更多