php从linux上的串口读取数据 [英] php read data from serial port on linux

查看:31
本文介绍了php从linux上的串口读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to read serial port on Linux platform using PHP.
But I cant read any data. When I try to read using .net, this time I can read.

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

I use "php_serial.class.php" class for serial port operations. You can read this class from this link :
here

我的代码是这样的:

<?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("
");
       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). " ) ");"总是返回零.无法从串口读取数据是什么原因?

the line "print_r(" (size ".strlen($read). " ) ");" always return zero. What is the reason why I cant read data from serial port?

推荐答案

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

I am sure you have rsolved this by now, but here is my 2c worth.

您读取了两次串行端口.一次检查是否有数据,然后在有数据时再次检查.根据我的经验,读取一次会清除缓冲区,因此再次读取会产生空结果.

You read the serial port twice. Once to check if there is data and then again when there is data. In my experience, reading it once clears the buffer and thus reading it again will yield an empty result.

千万别看第二遍

这篇关于php从linux上的串口读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆