繁体   English   中英

php从linux上的串口读取数据

[英]php read data from serial port on linux

我尝试使用PHP在Linux平台上读取串口。
但我无法读取任何数据。 当我尝试使用.net阅读时,这次我可以阅读。

我使用“php_serial.class.php”类进行串口操作。 您可以从以下链接阅读此课程:
这里

我的代码是这样的:

<?php  
 include "php_serial.class.php";  

// Let's start the class
$serial = new phpSerial;

// First we must specify the device. This works on both linux and windows (if
// your linux serial device is /dev/ttyS0 for COM1, etc)
$serial->deviceSet("/dev/ttyS1");

// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(19200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");

// Then we need to open it
$serial->deviceOpen();  

// read from serial port
$read = $serial->readPort();

//Determine if a variable is set and is not NULL
if(isset($read)){
   while(1){
       $read = $serial->readPort();
       print_r(" (size ".strlen($read). " ) ");
       for($i = 0; $i < strlen($read); $i++)
       {
          echo ord($read[$i])." ";
       }
       print_r("\n");
       sleep(1);
  }// end while
}// end if  


// If you want to change the configuration, the device must be closed
$serial->deviceClose();

// We can change the baud rate
$serial->confBaudRate(19200);  
?>  

行“print_r(”(size“.strlen($ read)。”)“);” 总是归零。 我无法从串口读取数据的原因是什么?

我相信你现在已经解决了这个问题,但这是我的2c价值。

你读了两次串口。 一旦检查是否有数据,然后在有数据时再次检查。 根据我的经验,读取它一旦清除缓冲区,因此再次读取它将产生一个空的结果。

只是不要第二次读它

有同样的问题。 我需要使用stty -isig -icanon设置两个选项,一旦设置脚本读取没问题。

您好,这是真的很老,但我目前(仍在)正在努力,我正确地恢复了字节(删除ord()读取字符串顺便说一句)。

它是零的原因是因为无限循环,即使你发送的东西没有被返回(所以它看起来像)虽然使用你的代码我设法把事情作为字符串返回。

我实际上将设备端的数据输入控制台...然后我回到了我输入设备的内容,看来你需要分叉这个过程,以便100%按照你的方式完成。 它有时工作的原因是因为如果你输入一个返回几行的命令,它很可能会得到一些。

使用屏幕连接到设备,然后只需键入随机的东西,然后点击输入...你会看到它出现在你的PHP输出上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM