使用 Event 和 DIO 读取串行端口

** 事件扩展当前无法识别 DIO 流。没有干净的方法来获取封装在 DIO 资源中的文件描述符。但有一个解决方法:

  • fopen() 打开端口流;
  • 使用 stream_set_blocking() 使流无阻塞 ;
  • 使用 EventUtil::getSocketFd() 从流中获取数字文件描述符 ;
  • 将数字文件描述符传递给 dio_fdopen()(当前未记录)并获取 DIO 资源;
  • 添加一个带有回调的 Event,用于监听文件描述符上的读取事件;
  • 在回调中排出可用数据并根据应用程序的逻辑对其进行处理。

dio.php

<?php
class Scanner {
  protected $port; // port path, e.g. /dev/pts/5
  protected $fd; // numeric file descriptor
  protected $base; // EventBase
  protected $dio; // dio resource
  protected $e_open; // Event
  protected $e_read; // Event

  public function __construct ($port) {
    $this->port = $port;
    $this->base = new EventBase();
  }

  public function __destruct() {
    $this->base->exit();

    if ($this->e_open)
      $this->e_open->free();
    if ($this->e_read)
      $this->e_read->free();
    if ($this->dio)
      dio_close($this->dio);
  }

  public function run() {
    $stream = fopen($this->port, 'rb');
    stream_set_blocking($stream, false);

    $this->fd = EventUtil::getSocketFd($stream);
    if ($this->fd < 0) {
      fprintf(STDERR, "Failed attach to port, events: %d\n", $events);
      return;
    }

    $this->e_open = new Event($this->base, $this->fd, Event::WRITE, [$this, '_onOpen']);
    $this->e_open->add();
    $this->base->dispatch();

    fclose($stream);
  }

  public function _onOpen($fd, $events) {
    $this->e_open->del();

    $this->dio = dio_fdopen($this->fd);
    // Call other dio functions here, e.g.
    dio_tcsetattr($this->dio, [
      'baud' => 9600,
      'bits' => 8,
      'stop'  => 1,
      'parity' => 0
    ]);

    $this->e_read = new Event($this->base, $this->fd, Event::READ | Event::PERSIST,
      [$this, '_onRead']);
    $this->e_read->add();
  }

  public function _onRead($fd, $events) {
    while ($data = dio_read($this->dio, 1)) {
      var_dump($data);
    }
  }
}

// Change the port argument
$scanner = new Scanner('/dev/pts/5');
$scanner->run();

测试

在终端 A 中运行以下命令:

$ socat -d -d pty,raw,echo=0 pty,raw,echo=0
2016/12/01 18:04:06 socat[16750] N PTY is /dev/pts/5
2016/12/01 18:04:06 socat[16750] N PTY is /dev/pts/8
2016/12/01 18:04:06 socat[16750] N starting data transfer loop with FDs [5,5] and [7,7]

输出可能不同。使用前几行中的 PTY(特别是/dev/pts/5/dev/pts/8)。

在终端 B 中运行上述脚本。你可能需要 root 权限:

$ sudo php dio.php

在终端 C 中,将字符串发送到第一个 PTY:

$ echo test > /dev/pts/8

输出

string(1) "t"
string(1) "e"
string(1) "s"
string(1) "t"
string(1) "
"