问题描述
我正在尝试将以下小型QBASIC程序(可以100%运行)移植到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
目前,我正在通过PHP(在WAMP5上)以其已编译(exe)格式调用它,但我想摆脱QBASIC并直接从PHP调用它.
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函数,但是它只是挂在fgets()行:
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;
}
我正在使用此PHP行来调用上述函数:
I'm using this PHP line to call the above function:
$res = read_port('COM1:', 17, TRUE, SIMULATE_SCALE);
任何帮助将受到热烈的感谢!我基本上已经放弃了尝试.如果QBASIC可以完美地做到这一点,那么我们必须能够使用PHP进行这项工作!
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!
推荐答案
您可能想研究 PHP序列.这里有一篇关于它的文章:
You might want to look into PHP Serial by Rémy Sanchez. There's an article about it here:
还可以查看PHP网站上dctkc dot com上的 jared 提供的示例:
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
这篇关于像这样的QBasic程序,如何在PHP中读取RS232串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!