首页 > 解决方案 > 适用于 Windows 的 PHP 中串行类的 readPort 函数

问题描述

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")
        {
            /* Do nothing : not implented yet */
        }
        trigger_error("Reading serial port is not implemented for Windows", E_USER_WARNING);
        return false;
    }

我刚刚给出了来自 Github 的部分代码。这些代码是我唯一的兴趣点。如果你想查看源代码,这里是

我正在寻找如何在 Windows 中将串口数据从 Arduino 读取到 PHP。在搜索了这么多时间后,我从 Github 找到了代码。但是在 windows 还没有实现 readPort() 的地方。

任何人都可以帮助我吗?

标签: phparduinoserial-portread-write

解决方案


推荐阅读