2012-04-29 30 views
8

我已經在Fedora 13中安裝了xampp。我試圖通過使用php串行類的串行端口與微控制器進行通信。 我的代碼是使用example.php在Linux中的PHP串行通信

include("php_serial.class.php"); 
$serial = new phpSerial(); 
$serial->deviceSet("0"); 

$serial->confBaudRate(9600); //Baud rate: 9600 
$serial->confParity("none"); //Parity (this is the "N" in "8-N-1") 
$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") 
$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") 
$serial->confFlowControl("none"); //Device does not support flow control of any kind, so set it to none. 

//Now we "open" the serial port so we can write to it 
$serial->deviceOpen(); 

$serial->sendMessage("*1"); //sleep(1); // echo "hi"; $serial->deviceClose(); 

?> 

PHP腳本被執行,但給出了以下警告。

警告:在/opt/lampp/htdocs/xampp/php_serial.class.php上在線147 中指定的串行端口無效警告:無法設置波特率:設備未設置或未在/選擇/ LAMPP/htdocs中/ XAMPP/php_serial.class.php上線241 警告:無法設置奇偶校驗:該設備未設置或在/opt/lampp/htdocs/xampp/php_serial.class.php打開上線295

...我已經使用命令:chmod 0777/dev/ttyUSB0來授予權限。我也嘗試使用以下命令將apache用戶「prudhvi」添加到撥出組: $ usermod -a -G撥出prudhvi

但它不起作用。當我使用以下命令直接從終端發送命令時:echo 1>/dev/ttyUSB0它工作並且'1'被髮送到串行端口。但使用PHP我得到了上述警告。

我已經使用了「$ WHOAMI」來檢查用戶名和添加用戶「prudhvi」的撥出組。它仍然無法正常工作。請幫助我們。

+3

你確定php正在訪問ttyUSB0嗎?它可以嘗試默認使用ttyS0。 –

回答

0

首先測試一個hello world類型的php腳本來測試你的基本安裝。

然後,驗證網絡服務器/ PHP的發動機運行作爲其是允許訪問對應於該串行端口的適用的/ dev/ttyWHATEVER設備文件組中的用戶。如果默認情況下是真的,那將是令人驚訝的 - 您可能必須將其添加到「撥出」或類似組。

添加一些錯誤檢查/報告到您的代碼。

2

你能張貼有關「上線147 /opt/lampp/htdocs/xampp/php_serial.class.php」近線/?

我懷疑你試圖錯誤地設置設備(如Marc所示)。無論是該端口還是端口都已經在同時進行的其他測試中使用。我不確定您正在運行的腳本是否提供了特定於您嘗試附加到已被使用的端口的錯誤。

3

我做了這個曾經在Debian控制的Arduino板與PHP腳本,最初遇到了同樣的問題。

在Debian中,你需要Apache用戶添加到撥出組,以使其能夠使串行連接請求。我認爲Fedora也是如此。

在Debian的命令是:

useradd -G dialout www-data 

但是我相信Fedora的名字Apache用戶是Apache的替代。我沒有一個Fedora機器上測試,但我會假設你需要運行的命令是:

useradd -G dialout apache 

然後,您將需要重新啓動您的XAMPP服務器。

參見參考以下內容:

http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/ http://fedoraproject.org/wiki/Administration_Guide_Draft/Apache#Apache_File_Security

尼爾

0

幸得馬克·B的意見,使我看這件事,他是死在:http://www.phpclasses.org/browse/file/17926.html

function deviceSet ($device) 
{ 
    if ($this->_dState !== SERIAL_DEVICE_OPENED) 
    { 
     if ($this->_os === "linux") 
     { 
      if (preg_match("@^COM(\d+):[email protected]", $device, $matches)) 
      { 
       $device = "/dev/ttyS" . ($matches[1] - 1); 
      } 

      if ($this->_exec("stty -F " . $device) === 0) 
      { 
       $this->_device = $device; 
       $this->_dState = SERIAL_DEVICE_SET; 
       return true; 
      } 
     } 
     elseif ($this->_os === "windows") 
     { 
      if (preg_match("@^COM(\d+):[email protected]", $device, $matches) and $this->_exec(exec("mode " . $device)) === 0) 
      { 
       $this->_windevice = "COM" . $matches[1]; 
       $this->_device = "\\.\com" . $matches[1]; 
       $this->_dState = SERIAL_DEVICE_SET; 
       return true; 
      } 
     } 

     trigger_error("Specified serial port is not valid", E_USER_WARNING); 
     return false; 
    } 
    else 
    { 
     trigger_error("You must close your device before to set an other one", E_USER_WARNING); 
     return false; 
    } 
} 

我相信調用$serial->deviceSet("/dev/ttyUSB0");會解決它,b您可能需要修改php_serial.class.php的來源才能使用/dev/ttyUSB而不是/dev/ttyS