[英]Read response from command on serial port
我正在编写一个PHP应用程序,该程序使用串行设备通过GSM调制解调器进行处理。 因此,您可以通过screen
与串行端口通信,并且在编写命令时会得到响应。
我使用PHP与串行端口通信,并使用sleep
在命令之间等待,我使用带有w+
标志的fopen
和fwrite
发送命令。 我尝试使用fread
函数检查响应是否存在,但实际上不是。 用PHP怎么做?
从概念f_read
如果端口配置正确,则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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.