我写了一个char设备驱动程序,现在正在写一个QT"包装器",它的一部分是当设备通过poll机制变得可读时触发信号.我试过这样做:
QFile file("/dev/testDriver");
if(file.open(QFile::ReadOnly)) {
QSocketNotifier sn(file.handle(), , QSocketNotifier::Read);
sn.setEnabled(true);
connect(&sn, SIGNAL(activated(int)), &this, SLOT(readyRead()));
}
Run Code Online (Sandbox Code Playgroud)
但是readyRead从未被调用过,我的驱动程序从未报告过调用其poll方法.
我能够得到以下代码,所以我知道我的驱动程序正在运行
QFile file("/dev/testDriver");
if(file.open(QFile::ReadOnly)) {
struct pollfd fd;
fd.fd = file.handle();
fd.events = POLLIN;
struct pollfd fds[] = {fd};
int ready;
qDebug() << "Started poll";
ready = poll(fds, 1, -1);
qDebug() << "Poll returned: " << ready;
QTextStream in(&file);
QTextStream out(stdout);
out << in.readAll();
}
Run Code Online (Sandbox Code Playgroud)
这正好等待我的驱动程序调用wake_up,我可以看到来自我的驱动程序的两个轮询调用.一个用于初始轮询注册,另一个用于wake_up发生时.
这样做我可能不得不产生一个单独的线程,它所做的只是在这个设备上轮询并抛出一个信号和循环.
是否可以这种方式使用QSocketNotifier?QFile :: handle()的文档似乎表明它应该是.