我写了一个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()的文档似乎表明它应该是.
jjc*_*f89 15
我还要提到QSocketNotifier可以用来观察stdin使用以下内容
#include "ConsoleReader.h"
#include <QTextStream>
#include <unistd.h> //Provides STDIN_FILENO
ConsoleReader::ConsoleReader(QObject *parent) :
QObject(parent),
notifier(STDIN_FILENO, QSocketNotifier::Read)
{
connect(¬ifier, SIGNAL(activated(int)), this, SLOT(text()));
}
void ConsoleReader::text()
{
QTextStream qin(stdin);
QString line = qin.readLine();
emit textReceived(line);
}
Run Code Online (Sandbox Code Playgroud)
---头
#pragma once
#include <QObject>
#include <QSocketNotifier>
class ConsoleReader : public QObject
{
Q_OBJECT
public:
explicit ConsoleReader(QObject *parent = 0);
signals:
void textReceived(QString message);
public slots:
void text();
private:
QSocketNotifier notifier;
};
Run Code Online (Sandbox Code Playgroud)
QSocketNotifer一旦该if块结束,您就会被销毁.它没有机会报告任何事情.
只要您希望监视该文件,就必须使该套接字通知程序保持活动状态.最简单的方法是将QSocketNotifer*成员保留在其中一个类中.
| 归档时间: |
|
| 查看次数: |
13863 次 |
| 最近记录: |