boost :: asio从/ dev / input / event0读取

时间:2015-01-07 16:59:17

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

我希望使用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")因为它不是串口的,对吗?

谢谢。

1 个答案:

答案 0 :(得分:5)

在我的系统上,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
相关问题