簡體   English   中英

php從linux上的串口讀取數據

[英]php read data from serial port on linux

我嘗試使用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. 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(”(size“.strlen($ read)。”)“);” 總是歸零。 我無法從串口讀取數據的原因是什么?

我相信你現在已經解決了這個問題,但這是我的2c價值。

你讀了兩次串口。 一旦檢查是否有數據,然后在有數據時再次檢查。 根據我的經驗,讀取它一旦清除緩沖區,因此再次讀取它將產生一個空的結果。

只是不要第二次讀它

有同樣的問題。 我需要使用stty -isig -icanon設置兩個選項,一旦設置腳本讀取沒問題。

您好,這是真的很老,但我目前(仍在)正在努力,我正確地恢復了字節(刪除ord()讀取字符串順便說一句)。

它是零的原因是因為無限循環,即使你發送的東西沒有被返回(所以它看起來像)雖然使用你的代碼我設法把事情作為字符串返回。

我實際上將設備端的數據輸入控制台...然后我回到了我輸入設備的內容,看來你需要分叉這個過程,以便100%按照你的方式完成。 它有時工作的原因是因為如果你輸入一個返回幾行的命令,它很可能會得到一些。

使用屏幕連接到設備,然后只需鍵入隨機的東西,然后點擊輸入...你會看到它出現在你的PHP輸出上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM