我試着用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