我正在尝试用 PHP 连接称重机。
我已经使用 super 终端进行了测试,它工作正常。
但是当我将 PHP 代码与 fopen
或 dio_open
一起使用时,它会被执行,但是当 fgets
或 dio_read
为调用后,它挂起并且不显示任何内容。
Fopen 示例
exec('mode com2: BAUD=2400 PARITY=N data=8 stop=1 xon=on');
$fp = fopen("COM2:", "r");
if (!$fp) {
echo "Uh-oh. Port not opened.";
} else {
echo fgets($fp);
fclose($fp);
}
DIO 示例
exec('mode COM2: baud=2400 data=8 stop=1 parity=n xon=off to=on');
$fd = dio_open('COM2:', O_RDONLY | O_NONBLOCK, 0644);
echo dio_read($fd, 256);
我无法尝试这两种方法来收集任何有用的输出。
我需要 inpout32.dll
文件才能连接吗?
php_iol.dll
的作用是什么,我也需要这个文件?
最佳答案
Windows下PHP无法读取串口。在 Linux 系统上则不然,在 Linux 系统上使用常见的 PHP 文件系统函数从 COM 端口读取数据没有问题。
这些文件系统函数也是处理 DIO extension 的首选方法:
The use of the DIO functions should be considered only when direct control of a device is needed. In all other cases, the standard filesystem functions are more than adequate.
当您将 DIO 示例报告为“不工作”时,您需要跟踪错误。请参阅:
接下来,您需要首先找到一个有效的 POSIX 示例,因为 DIO 不是 super 终端,而是 POSIX 风格。
或者,存在一些软件可以通过网络代理 COM 端口(例如 GPL'ed serproxy )。这可能会解决您与操作系统的集成问题,但我很确定您运气不好。
比较:
- How to Read RS232 Serial Port in PHP like this QBasic Program(2010 年 7 月)
- Reading COM (Serial Modem) in PHP(2011 年 6 月)
- Giving PHP permission to access COM port(2012 年 2 月)- 仅写作
关于PHP COM口与DIO的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23589702/