如何像这个 QBasic 程序一样在 PHP 中读取 RS232 串口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3226979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Read RS232 Serial Port in PHP like this QBasic Program
提问by CJ_
I'm trying to port the following small QBASIC program (which works 100%) to PHP:
我正在尝试将以下小型 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
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.
目前,我正在从 PHP(在 WAMP5 上)以编译(exe)形式调用它,但我想摆脱 QBASIC 并直接从 PHP 调用它。
I wrote this PHP function but it just hangs at the fgets() line:
我写了这个 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;
}
}
I'm using this PHP line to call the above function:
我正在使用此 PHP 行调用上述函数:
$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!
任何帮助将不胜感激!我基本上已经放弃尝试了。如果 QBASIC 可以完美地做到这一点,那么我们必须能够使用 PHP 来完成这项工作!
回答by Mike
You might want to look into PHP Serialby Rémy Sanchez. There's an article about it here:
您可能想了解Rémy Sanchez 的PHP Serial。这里有一篇关于它的文章:
Controlling the Serial Port with PHP
Also have a look at this example provided by jared at dctkc dot comon the PHP site:
还可以查看jared 在PHP 站点上的dctkc dot com上提供的这个示例:
回答by dmp
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.
可以肯定的是,默认情况下 PHP 无法访问硬件端口。它可以访问网络资源、文件资源,但是如果没有在硬件和您要读取的内容之间进行某种传输,则无法看到它的工作原理。
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.
e:是的,有 - 检查这个扩展,可能是你想要的。没有这样的东西,它就行不通。
"This extension allows the direct access the parallel and serial(rs232) port in reading and writing by the DLL inpout32.dll under WIN9x/NT/2000/XP for any assembly. An example of concret application: Complete house automation with web interface and php, connection hardware of any nature with the ports like assemblies simple or to complicate. One idea simple but quite practical... Extension and source was compiled with Delphi 6 for PHP5.0 to 5.1.2, sources and example included."
“此扩展允许在 WIN9x/NT/2000/XP 下通过 DLL inpout32.dll 直接访问并行和串行(rs232)端口以进行任何程序集的读取和写入。具体应用示例:具有 Web 界面的完整房屋自动化和php,任何性质的连接硬件与简单或复杂的组件等端口。一个想法简单但非常实用......扩展和源代码是用 Delphi 6 编译的,用于 PHP5.0 到 5.1.2,包括源代码和示例。”
回答by Piskvor left the building
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 thisfor a few tips for finding out which port maps to which "file".
如果您使用的是 Linux 或其他类似fopen('/dev/ttyS0')UN X 的系统(例如 Mac OS X),请尝试- 在 UNX 中,一切都是文件,甚至是串行端口。有关找出哪个端口映射到哪个“文件”的一些提示,请参阅此处。

