我试着用PHP读取Linux平台上的串口。
但我不能读取任何数据。当我尝试阅读.net时,这次我可以阅读。php从linux上的串口读取数据
我使用“php_serial.class.php”类进行串口操作。您可以通过以下链接阅读这个类:
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($读)”)“); “总是返回零。什么原因导致我无法从串口读取数据?
DId您尝试运行“cat/dev/ttyS1”例如作为PHP用户(可能名为Apache或www-data)?此用户是否具有对/ dev/ttyS1的读取权限? – 2012-07-24 09:41:53
是的,用户可以读取/ dev/ttyS1。我使用com2端口,我可以打开端口并将数据发送到端口。但我不能从端口读取数据。另外我尝试“cat/dev/ttyS1”,但不能读取任何内容。 – cukcuk 2012-07-24 10:31:21
您是否检查过您的PHP安装配置为报告错误和警告?该班级使用警告来报告问题。尝试添加trigger_error(“logging is working”,E_USER_WARNING);在脚本的顶部 - 如果你没有在输出中看到它,那么稍后有可能会失败,并且你会压制错误消息。 – symcbean 2012-07-24 10:32:34