如何像 QBasic 程序一样在 PHP 中读取 RS232 串口

发布于 2024-09-09 01:27:10 字数 1276 浏览 4 评论 0原文

我正在尝试将以下小型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余厌 2024-09-16 01:27:10

您可能想查看 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

兰花执着 2024-09-16 01:27:10

可以肯定 PHP 默认情况下无法访问硬件端口。它可以访问网络资源、文件资源,但如果硬件和您要读取的内容之间没有某种传输,则无法看到此功能。

然而,您可能可以加载一个特定于平台的扩展来启用此功能 - 只是调查。

e:是的,有 - 检查此扩展,可能就是您想要的后。如果没有这样的东西,它就行不通。

“此扩展允许直接
访问并行和串行(rs232)
DLL 读写端口
WIN9x/NT/2000/XP下的inpout32.dll
对于任何装配。一个例子
具体应用: 完整住宅
使用 Web 界面和 php 实现自动化,
连接任何性质的硬件
像组件这样的端口简单或
复杂化。一个想法简单但相当
实用...扩展和来源是
用Delphi 6 for PHP5.0编译为
5.1.2,包括来源和示例。”

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.

"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."

人生戏 2024-09-16 01:27:10

如果您使用的是 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文