如何像 QBasic 程序一样在 PHP 中读取 RS232 串口
我正在尝试将以下小型 QBASIC 程序(100%工作)移植到 PHP:
OPEN "com1:2400,n,8,1,DS," FOR RANDOM AS #3
OPEN "data.dat" FOR OUTPUT AS #2
REM read 17 chars from the port
scale$ = INPUT$(17, #3)
PRINT scale$
WRITE #2, scale$
CLOSE #2
CLOSE #3
SYSTEM
目前我正在从 PHP(在 WAMP5 上)以其编译(exe)形式调用它,但我想摆脱 QBASIC并直接从 PHP 调用它。
我编写了这个 PHP 函数,但它只是挂在 fgets() 行:
function read_port($port='COM1:', $length=17, $setmode=TRUE, $simulate='') {
if ($simulate){
$buffer = '"'.strval(rand(1000, 2000));
return $buffer;
}
if ($setmode){
shell_exec('mode com1: baud=2400 parity=n data=8 stop=1 to=on xon=off odsr=on octs=on dtr=on rts=on idsr=on');
}
$fp = fopen($port, "rb+");
if (!$fp) {
file_put_contents('debug1.log','COM1: could not open'."\n",FILE_APPEND);
} else {
$buffer = fgets($fp, $length); // <-- IT JUST HANGS HERE DOING NOTHING !
fclose ($fp);
}
return $buffer;
}
我正在使用这个 PHP 行来调用上述函数:
$res = read_port('COM1:', 17, TRUE, SIMULATE_SCALE);
任何帮助将不胜感激!我基本上已经放弃尝试了。如果 QBASIC 可以完美地做到这一点,那么我们一定能够使用 PHP 来实现这一点!
I'm trying to port the following small QBASIC program (which works 100%) to PHP:
OPEN "com1:2400,n,8,1,DS," FOR RANDOM AS #3
OPEN "data.dat" FOR OUTPUT AS #2
REM read 17 chars from the port
scale$ = INPUT$(17, #3)
PRINT scale$
WRITE #2, scale$
CLOSE #2
CLOSE #3
SYSTEM
Currently I'm calling it in its compiled (exe) form from PHP (on WAMP5) but I'd like to get rid of the QBASIC and call it directly from PHP.
I wrote this PHP function but it just hangs at the fgets() line:
function read_port($port='COM1:', $length=17, $setmode=TRUE, $simulate='') {
if ($simulate){
$buffer = '"'.strval(rand(1000, 2000));
return $buffer;
}
if ($setmode){
shell_exec('mode com1: baud=2400 parity=n data=8 stop=1 to=on xon=off odsr=on octs=on dtr=on rts=on idsr=on');
}
$fp = fopen($port, "rb+");
if (!$fp) {
file_put_contents('debug1.log','COM1: could not open'."\n",FILE_APPEND);
} else {
$buffer = fgets($fp, $length); // <-- IT JUST HANGS HERE DOING NOTHING !
fclose ($fp);
}
return $buffer;
}
I'm using this PHP line to call the above function:
$res = read_port('COM1:', 17, TRUE, SIMULATE_SCALE);
Any help will be creatly appreciated! I've basically given up trying. If QBASIC can do it perfectly then we must be able to make this work with PHP!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想查看 PHP Serial 雷米·桑切斯。这里有一篇关于它的文章:
用 PHP 控制串口
还有看一下 PHP 站点上 jared at dctkc dot com 提供的示例:
http://php.net/manual/en/function.fopen.php#20935
You might want to look into PHP Serial by Rémy Sanchez. There's an article about it here:
Controlling the Serial Port with PHP
Also have a look at this example provided by jared at dctkc dot com on the PHP site:
http://php.net/manual/en/function.fopen.php#20935
可以肯定 PHP 默认情况下无法访问硬件端口。它可以访问网络资源、文件资源,但如果硬件和您要读取的内容之间没有某种传输,则无法看到此功能。
然而,您可能可以加载一个特定于平台的扩展来启用此功能 - 只是调查。
e:是的,有 - 检查此扩展,可能就是您想要的后。如果没有这样的东西,它就行不通。
Pretty sure PHP has no access to hardware ports by default. It has access to network resources, file resources, but without some kind of transport between the hardware and what you're trying to read, can't see this working.
There may however be a platform specific extension you can load which will enable this - just investigating.
e: Yep, there is - check this extension, might be what you're after. Without something like this, it's just not going to work.
如果您使用的是 Linux 或其他类似 UNX 的系统(例如 Mac OS X),请尝试
fopen('/dev/ttyS0')
- 在 UNX 中,一切是一个文件,甚至是串口。 请参阅此内容,了解一些关于找出哪个端口映射到哪个“文件”。If you're on Linux or other UNX-like system (e.g. Mac OS X), try
fopen('/dev/ttyS0')
- in UNX, everything is a file, even serial ports. See this for a few tips for finding out which port maps to which "file".