【问题标题】:boost::asio read from /dev/input/event0boost::asio 从 /dev/input/event0 读取
【发布时间】:2015-03-05 15:35:30
【问题描述】:

我希望使用 boost::asio 从 12 位键盘读取。我目前可以在没有提升的情况下做到这一点,这样:

fd = open ("/dev/input/event0", 0_NONBLOCK);
read (fd, &ev, sizeof ev);

你知道我如何使用 boost::asio 做到这一点吗?我正在使用 Linux 和 C++。这个post 和这个post 很有用。我不会使用串行端口(io,“/dev/usb/hiddev0”),因为它不是串行的,对吧?

谢谢。

【问题讨论】:

  • 我可以打开设备,获取文件描述符,然后在我认为的 asio 中使用它
  • Boost.Asio 提供现代 C++ 接口,但在其实现中它使用纯 C(或 OS)函数。所以,首先,值得了解为什么上面的read 不起作用。你观察到什么行为?
  • 高度相关:考虑使用库而不是使用原始设备:stackoverflow.com/a/25559167/85371
  • @IgorR。我无法以这种方式打开设备:serial_port port (io, "/dev/usb/hiddev0");我想我应该使用 stream_descriptor

标签: c++ boost debian boost-asio keypad


【解决方案1】:

在我的系统上,event2 代表鼠标,下面这个简单的readloop 程序就像一个魅力。

以 root 身份运行

#include <boost/asio.hpp>
#include <boost/asio/posix/stream_descriptor.hpp>
#include <boost/bind.hpp>

#include <iostream>      // for debug output
#include <iomanip>

#include <linux/input.h> // for input_event
#include <boost/range/adaptor/sliced.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace boost::asio;

struct input {
    using error_code = boost::system::error_code;
    using sliced = boost::adaptors::sliced;

    input(const char* devspec) : svc(), sd(svc, open(devspec, O_RDONLY)) {
        readloop(); // post work
    }

    void run() {
        svc.run();
    }

  private:
    io_service svc;
    posix::stream_descriptor sd;

    std::vector<input_event> events;

    void handle_input(error_code ec, size_t bytes_transferred) {
        if (!ec) {
            auto const n = bytes_transferred / sizeof(input_event);

            for (auto& ev : events | sliced(0,n)) {
                using namespace boost::posix_time;

                ptime ts({1970,1,1}, seconds(ev.time.tv_sec) + microsec(ev.time.tv_usec));

                std::cout << std::dec << ts.time_of_day() << "\t" << std::hex 
                          << std::hex << ev.type << " " << ev.code  << " " << ev.value << "\n";
            }

            std::cout << "\n";

            readloop();
        } else {
            std::cerr << ec.message() << "\n";
        }
    }

    void readloop() {
        events.resize(32);
        sd.async_read_some(buffer(events), boost::bind(&input::handle_input, this, placeholders::error, placeholders::bytes_transferred));
    }

};

int main()
{
    input monitor("/dev/input/event2");
    monitor.run();
}

典型输出:

22:33:09.705346 2 0 ffffffff
22:33:09.705346 2 1 1
22:33:09.705346 0 0 0

22:33:09.713412 2 0 ffffffff
22:33:09.713412 2 1 1
22:33:09.713412 0 0 0

22:33:09.721308 2 0 ffffffff
22:33:09.721308 0 0 0

22:33:09.729328 2 0 ffffffff
22:33:09.729328 0 0 0

22:33:09.737346 2 1 1
22:33:09.737346 0 0 0

22:33:09.745328 2 0 ffffffff
22:33:09.745328 2 1 1
22:33:09.745328 0 0 0

22:33:11.897301 4 4 90001
22:33:11.897301 1 110 1
22:33:11.897301 0 0 0

22:33:12.065294 4 4 90001
22:33:12.065294 1 110 0
22:33:12.065294 0 0 0

【讨论】:

  • 在您尝试之前,您可能不应该接受它作为“解决方案”:)
  • 我已经更新以实际解码事件,打印友好的 µs 时间戳和事件批次。 (这是 Asio 如何透明地使用 POD 结构向量作为写入缓冲区的典型代表。)
  • 可能是 date_time。 (您可以与first version 进行对比)
  • 看起来所有的包含目录都是多余的 /usr/include/ 就在上面。所以g++ -x c++ -std=c++11 -g -Wall -m32 -c asio_event_reader.cc -o asio_event_reader.o 通常更有意义。
  • @xinthose 循环只是循环通过events 向量。 events | sliced(0,n) 表达式采用第一个 n 元素(即传输的事件数)。你可以把它写成更传统的 for 循环 for (size_t i = 0; i &lt; n; ++i) { auto&amp; ev = events[i]; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-20
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
相关资源
最近更新 更多