繁体   English   中英

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

[英]How to read a file device in Linux using 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调用现在阻塞了。 如果我注释掉读取调用,计数器运行速度非常快。 这里有一个类似的帖子这里错了吗?


最终解决方案

感谢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";
    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;
        }
    }
}

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

所以你只需打开设备,用open()获取文件描述符,然后在其上创建一个QSocketNotifier ,类型为QSocketNotifier::Read 当有事件被读取时,你会得到activate()信号。

替代解决方案,如果您仍想使用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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM