如何使用Qt在Linux中读取文件设备?

时间:2018-02-11 17:39:08

标签: c++ linux qt

我正在开发一个基于Qt5的小型GUI,它将显示来自Linux文件设备的数据流。 为此我选择了一个操纵杆输入。使用cat /dev/input/js0,可以在终端上看到传入的流。

使用C,您可以使用带阻塞读取的循环读取此设备文件或处理设备信号。但我不能用Qt来解决这个问题。

使用Qt与设备文件交互的典型方法是什么?

根据@rodrigo的答案,这里有一个新的实现:

joystick.h

#ifndef JOYSTICK_H
#define JOYSTICK_H

#include <QObject>
#include <QFile>
#include <QSocketNotifier>

class Joystick
      : public QObject
{
    Q_OBJECT

    QString fileName = "/dev/input/js0";
    QFile *file;
    QSocketNotifier *notifier;

public:
    explicit Joystick(QObject *parent = nullptr);
    ~Joystick();

signals:

public slots:
    void handle_readNotification(int socket);
};

#endif // JOYSTICK_H

joystick.cpp

#include "joystick.h"

Joystick::Joystick(QObject *parent)
   : QObject(parent)
{
    file = new QFile();
    file->setFileName(fileName);
    if( !file->exists() ){
        qWarning("file does not exist");
        return;
    }

    if( !file->open(QFile::ReadOnly) ){
        qWarning("can not open file");
        return;
    }

    notifier = new QSocketNotifier( file->handle(),
                                    QSocketNotifier::Read,
                                    this);

    connect( notifier,
             &QSocketNotifier::activated,
             this,
             &Joystick::handle_readNotification );

    if( !notifier->isEnabled() ){
        qInfo("enable notifier");
        notifier->setEnabled(true);
    }
    qInfo("Joystick init ready");
}

void
Joystick::handle_readNotification(int /*socket*/)
{
    static quint64 cnt=0;
    qInfo("cnt: %d",cnt++);

    if( !(file->isOpen()) ){
        qWarning("file closed");
        return;
    }

    char buf[16]; /* tested with different sizes */
    if( file->read(buf,sizeof(buf)) ){
        qInfo("read: %s",buf);
    }
//  QByteArray ba = file->readAll();
//  qInfo("Data: %s", ba.data());
}

然后我运行它,最后一个输出是cnt: 0。似乎readreadAll调用现在阻止了。如果我注释掉读取调用,计数器运行速度非常快。 here a similiar post这不对吗?

最终解决方案

感谢罗德里戈!

joystick.h

#ifndef JOYSTICK_H
#define JOYSTICK_H

#include <QObject>
#include <QFile>
#include <QSocketNotifier>

class Joystick
      : public QObject
{
    Q_OBJECT

    QString fileName = "/dev/input/js0";
    QSocketNotifier *notifier;
    int fd;

public:
    explicit Joystick(QObject *parent = nullptr);
    ~Joystick();

signals:
    void buttonPressed(quint8 number, qint16 value);
    void axisMoved(quint8 number, qint16 value);


public slots:
    void handle_readNotification(int socket);
};

#endif // JOYSTICK_H

joystick.cpp

#include "joystick.h"

#include <fcntl.h>
#include <unistd.h>
#include <linux/joystick.h>

Joystick::Joystick(QObject *parent)
   : QObject(parent)
{
    auto file = new QFile();
    file->setFileName(fileName);
    if( !file->exists() ){
        qWarning("file does not exist");
        return;
    }

    fd = open(fileName.toUtf8().data(), O_RDONLY|O_NONBLOCK);
    if( fd==-1 ){
        qWarning("can not open file");
        return;
    }

    notifier = new QSocketNotifier( fd,
                                    QSocketNotifier::Read,
                                    this);

    connect( notifier,
             &QSocketNotifier::activated,
             this,
             &Joystick::handle_readNotification );
}

Joystick::~Joystick()
{
    if( fd>=0 ){
        close(fd);
    }
}

void
Joystick::handle_readNotification(int /*socket*/)
{
    struct js_event buf;
    while( read(fd,&buf,sizeof(buf))>0 ){
        switch (buf.type) {
        case JS_EVENT_BUTTON:
            emit buttonPressed(buf.number, buf.value);
            break;
        case JS_EVENT_AXIS:
            emit axisMoved(buf.number, buf.value);
            break;
        }
    }
}

2 个答案:

答案 0 :(得分:6)

通常使用工具包的轮询源解决此问题。在Qt的情况下,这是QSocketNotifier。尽管它的名字(历史事故?),它可用于轮询任何文件描述符,而不仅仅是套接字。

因此,您只需打开设备open()即可获取文件描述符,然后在其上创建QSocketNotifier,类型为QSocketNotifier::Read。当有事件需要阅读时,你会收到activate()信号。

答案 1 :(得分:1)

替代解决方案,如果您仍然想使用QFile而不是低级读/写功能:

auto file = new QFile(fileName);

...

// IMPORTANT: You must set the Unbuffered flag below
if(!file->open(QFile::ReadOnly | QFile::Unbuffered))
{
    // handle error
}

auto fd = file.handle();

auto flags = fcntl(fd, F_GETFL, 0);
if(flags == -1)
{
    // handle error
}

flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
if(flags == -1)
{
    // handle error
}

auto notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);

...

struct js_event buf;
while(file->read(&buf, sizeof(buf)) > 0)
{
    // process data
}