使用QSocketNotifier在char设备上进行选择。 [英] Using QSocketNotifier to select on a char device.
问题描述
我写了一个char设备驱动程序,我现在正在写一个QT包装程序,它的一部分是获得一个信号,当设备变得可读通过轮询机制。我试图做:
I wrote a char device driver and am now writing a QT "wrapper" which part of it is to get a signal to fire when the device becomes readable via the poll mechanism. I had tried to do:
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()));
}
但是readyRead从来没有被调用,我的驱动程序从来没有报告调用它的poll方法。
But readyRead was never called and my driver never reported having its poll method called.
我可以得到下面的代码,所以我知道我的驱动程序正在工作
I was able to get the following code to work so I know my driver is working
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();
}
正确等待我的驱动程序调用wake_up,我可以看到两个轮询调用从我的司机。一个用于初始轮询注册,一个用于wake_up发生时。
This properly waits for my driver to call wake_up and I can see two poll calls from my driver. One for the initial poll registration and one for when the wake_up happens.
这样做可能会产生一个单独的线程,它所做的是在这个设备上轮询并抛出一个信号和循环。
Doing it this way I would probably have to spawn a separate thread which all it did was poll on this device and throw a signal and loop.
可以使用 QSocketNotifier 这样吗? QFile :: handle()的文档似乎表明它应该。
Is it possible to use QSocketNotifier in this way? The documentation of QFile::handle() seems to indicate it should be.
推荐答案
您的 QSocketNotifer
$ c> if 块结束。它不会报告任何内容。
Your QSocketNotifer
gets destroyed as soon as that if
block ends. It doesn't stand a chance of reporting anything.
只要您希望监控该文件,您必须保持该套接字通知程序处于活动状态。这样做的最简单的方法是在你的一个类中保留一个 QSocketNotifer *
成员。
You must keep that socket notifier alive as long as you want that file to be monitored. The simplest way of doing that is probably keeping a QSocketNotifer*
member in one of your classes.
这篇关于使用QSocketNotifier在char设备上进行选择。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!