PHP从Linux上的串行端口读取数据 [英] php read data from serial port on 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("\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("(大小".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屋!