php read data from serial port on linux(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上的串口读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:php从linux上的串口读取数据
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01