从串口读取命令的响应

时间:2018-01-04 22:29:13

标签: php serial-port usb hardware

我正在编写一个PHP应用程序,它使用串行设备来处理GSM调制解调器。因此,您可以通过screen与串行端口进行通信,并在编写命令时收到响应。

我使用PHP与我的串口通信,并使用sleep在命令之间等待,我fopen带有w+标志和fwrite来发送命令。我尝试使用fread函数检查响应是否存在,但它不是。怎么可以在PHP中完成?

1 个答案:

答案 0 :(得分:0)

如果端口配置正确,概念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

相关问题