php在linux上从串行端口读取数据
我尝试使用PHP在Linux平台上读取串行端口。php在linux上从串行端口读取数据,php,linux,serial-port,Php,Linux,Serial Port,我尝试使用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.
但是我看不到任何数据。当我尝试使用.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);
?>
我相信你现在已经解决了这个问题,但这是我的2c值
您读取串行端口两次。一次检查是否有数据,然后在有数据时再次检查。根据我的经验,读取一次会清除缓冲区,因此再次读取会产生一个空结果
只是不要第二次读它,因为它有同样的问题。我需要使用stty-isig-icanon设置两个选项,设置后脚本读取没有问题。您好,这确实很旧,但我目前(仍然)正在处理这一问题,我正确地恢复了字节(请删除ord()以作为字符串读取)
它之所以变成零是因为无限循环,即使您发送了一些东西,也没有返回任何东西(看起来是这样),尽管使用您的代码,我设法将东西作为字符串返回
实际上,我在设备端向控制台输入了数据……然后返回了我在设备中输入的数据,看来您需要完成这个过程,才能100%按自己的方式完成。它有时起作用的原因是,如果您输入一个返回几行的命令,它很可能会得到其中的一些行
使用屏幕连接到设备,然后只需键入随机内容并按enter键。。。您将在php输出中看到它。您是否尝试以php用户(可能名为Apache或www data)的身份运行“cat/dev/ttyS1”?此用户是否具有对/dev/ttyS1的读取权限?是的,用户具有对/dev/ttyS1的读取权限。我正在使用com2端口,我可以打开端口并将数据发送到端口。但我无法从端口读取数据。我也尝试了“cat/dev/ttyS1”,但也无法读取任何内容。您是否检查过PHP安装是否配置为报告错误和警告?该类使用警告来报告问题。尝试添加触发器错误(“日志记录正在工作”,E_用户警告);在脚本的顶部-如果您在输出中没有看到它,那么很可能以后出现了故障,您正在抑制错误消息。我已经在php_serial类中使用了trigger_error来检查任何错误或警告。我的问题不在于它。我的问题是在linux上从串行端口读取数据。我使用$read=$serial->readPort();但有时它可以阅读,有时它可以阅读,你曾经解决过这个问题吗?