从串行端口上的命令读取响应
Read response from command on serial port
我正在编写一个 PHP 应用程序,该应用程序使用串行设备来处理 GSM 调制解调器。所以你可以通过screen
与串口通信,你写命令就会得到响应。
我使用 PHP 与我的串行端口通信并使用 sleep
在命令之间等待,我使用 fopen
和 w+
标志和 fwrite
发送命令。我尝试使用 fread
函数来检查响应是否存在,但没有。在 PHP 中如何做到这一点?
概念上 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 )
我正在编写一个 PHP 应用程序,该应用程序使用串行设备来处理 GSM 调制解调器。所以你可以通过screen
与串口通信,你写命令就会得到响应。
我使用 PHP 与我的串行端口通信并使用 sleep
在命令之间等待,我使用 fopen
和 w+
标志和 fwrite
发送命令。我尝试使用 fread
函数来检查响应是否存在,但没有。在 PHP 中如何做到这一点?
概念上 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 )