我正在编写一个PHP应用程序,它使用串行设备来处理GSM调制解调器。因此,您可以通过screen
与串行端口进行通信,并在编写命令时收到响应。
我使用PHP与我的串口通信,并使用sleep
在命令之间等待,我fopen
带有w+
标志和fwrite
来发送命令。我尝试使用fread
函数检查响应是否存在,但它不是。怎么可以在PHP中完成?
答案 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)