doucan957495 2012-07-24 09:32
浏览 131

php从linux上的串口读取数据

I try to read serial port on Linux platform using PHP.
But I cant read any data. When I try to read using .net, this time I can read.

I use "php_serial.class.php" class for serial port operations. You can read this class from this link :
here

My code is like this :

<?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("
");
       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);  
?>  

the line "print_r(" (size ".strlen($read). " ) ");" always return zero. What is the reason why I cant read data from serial port?

  • 写回答

3条回答 默认 最新

  • dongxing4643 2012-10-04 08:46
    关注

    I am sure you have rsolved this by now, but here is my 2c worth.

    You read the serial port twice. Once to check if there is data and then again when there is data. In my experience, reading it once clears the buffer and thus reading it again will yield an empty result.

    Just do not read it the second time

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用