美文网首页
PHP 串口通讯

PHP 串口通讯

作者: 哈哈新开张 | 来源:发表于2022-07-28 16:59 被阅读0次

    dio 扩展 https://pecl.php.net/package/dio
    linux代码

    <?php
    $fd  =  dio_open ( '/dev/ttyS0' ,  O_RDWR  |  O_NOCTTY  |  O_NONBLOCK );
     
    //dio_fcntl ( $fd ,  F_SETFL ,  O_SYNC );
    if ( dio_fcntl ( $fd ,  F_SETLK , Array( "type" => F_WRLCK )) == - 1 ) {
        // the file descriptor appears locked
        echo  "The lock can not be cleared. It is held by someone else.\n" ;
    } else {
       echo  "Lock successfully set/cleared\n" ;
    }
     
    dio_tcsetattr ( $fd , array(
       'baud'  =>  9600 ,
       'bits'  =>  8 ,
       'stop'   =>  1 ,
       'parity'  =>  0
    )); 
     
    while ( 1 ) {
       $data  =  dio_read ( $fd ,  9999 );
       if ( $data  ) {
          echo  bin2hex($data)."\n";
       }  
       usleep(300000);
    }
    

    windows 代码

    <?php
     
    exec('mode com1: baud=9600 data=8 stop=1 parity=n xon=on');
    // execute 'help mode' in command line of Windows for help
     
    $fd = dio_open('com1:', O_RDWR);
     
    while ( true ) {
      $data  =  dio_read ( $fd ,  256 );
      if ($data) {
          echo bin2hex($data)."\n";
      }
      //usleep(10000);
    }
    

    其他参考: https://www.php.cn/php-ask-480151.html

    相关文章

      网友评论

          本文标题:PHP 串口通讯

          本文链接:https://www.haomeiwen.com/subject/riyxwrtx.html