由于我无法通过引用Linux Server和Windows Server来访问COM端口值。因此请向我解释引用Linux&Windows Server时需要进行的所有设置。由于以下代码在localhost中正常工作
<?php
//You need to set the com with a dos command like like:
//for windows
//$output = "mode COM1: BAUD=9600 PARITY=N data=8 stop=1 XON=off TO=on";
for linux
$output = "mode /dev/ttyS1: BAUD=9600 PARITY=N data=8 stop=1 XON=off TO=on";
//The next command executes the dos command through php:
system($output);
//Create the resource id:
//for Windows
$fp = fopen('COM1', 'r+');
//for Linux
$fp = fopen('/dev/ttyS1', 'r+');
if(!$fp)
{
echo"Port not accessible";
}
else
{
echo"Port COM1 opened successfully";
}
//Read from port:
$buffer = fgets($fp);
echo"Read from buffer: $buffer";
$file = "output/a.txt";
file_put_contents($file,$buffer);
?>
*
最佳答案
您无需费心检查所用的操作系统,而盲目尝试使用SAME文件句柄打开两个COM端口:
如果您使用的是Windows,则:
$fp = fopen('COM1', 'r+');
成功,然后您立即执行以下操作:
$fp = fopen('/dev/ttyS1', 'r+');
然后杀死您刚刚创建的句柄,因为Windows没有
/dev/ttyS1
。您需要条件代码:
$path = ($os == 'Linux') ? '/dev/ttyS1', 'COM1';
$fp = fopen($path, 'r+');
关于php - 使用PHP的Linux托管服务器的串行COM端口问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38666771/