Skip to content

硬件接口

示教器上存在着一部分按钮和旋钮,下面示例演示如何获取到他们的状态:

建立一个Qt窗口应用

mainwindow.h

cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QMainWindow>
#include <QSocketNotifier>


namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


private slots:
    void onDataAvailable();
    void getKeyValue();
    void getEnableValue();
private:
    Ui::MainWindow *ui;
    int switchfd = -1;
    QSocketNotifier *switch_notifier;
    int buttonfd = -1;
    QSocketNotifier *button_notifier;
    int enablefd = -1;// 使能
    QSocketNotifier *enable_notifier;
};


#endif // MAINWINDOW_H

mainwindow.cpp

cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"


#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <unistd.h>
#include <linux/input.h>


#include <QFile>
#include <QDebug>
#include <QTime>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 打开旋钮设备文件
    switchfd = ::open("/dev/buttons", O_RDONLY | O_NONBLOCK);
    if (switchfd < 0) {
        qCritical() << "Failed to open device:" << strerror(errno);
        QCoreApplication::exit(1);
        return;
    }
    switch_notifier = new QSocketNotifier(switchfd, QSocketNotifier::Read, this);
    connect(switch_notifier, SIGNAL(activated(int)), this, SLOT(onDataAvailable()));


    //打开按键设备文件
    buttonfd = ::open("/dev/input/event0", O_RDWR);
    if (buttonfd < 0) {
        qCritical() << "Failed to open device:" << strerror(errno);
        QCoreApplication::exit(1);
        return;
    }
    button_notifier = new QSocketNotifier(buttonfd,QSocketNotifier::Read, this);
    connect(button_notifier, SIGNAL(activated(int)), this, SLOT(getKeyValue()));


    //打开使能设备文件
    enablefd = ::open("/dev/buttonstop", O_RDWR);
    if (enablefd < 0) {
        qCritical() << "Failed to open device:" << strerror(errno);
        QCoreApplication::exit(1);
        return;
    }
    enable_notifier = new QSocketNotifier(enablefd,QSocketNotifier::Read, this);
    connect(enable_notifier, SIGNAL(activated(int)), this, SLOT(getEnableValue()));
}



MainWindow::~MainWindow()
{
    delete ui;
    if (switchfd >= 0) ::close(switchfd);
    if (buttonfd >= 0) ::close(buttonfd);
    if (enablefd >= 0) ::close(enablefd);
}



void MainWindow::onDataAvailable()
{
    char switchbuttons[8]={'0','0','0','0','0','0','0','0'};
    ssize_t bytesRead;
    // 读取设备数据
    while ((bytesRead = ::read(switchfd, switchbuttons, sizeof(switchbuttons))) > 0) {
        qDebug() << "Data received:" << QByteArray(switchbuttons, bytesRead);
        QString CurMode = "";
        QString teachMode="00001000";//示教模式
        QString runMode="00000100";//运行模式
        QString remoteMode="00010000";//远程模式
        for(unsigned int i=0;i<sizeof(switchbuttons);i++)
        {
            CurMode.append(QString(QChar(switchbuttons[i])));
        }
        qDebug() << CurMode;
        if(CurMode == teachMode)
        {
            qDebug() << "示教模式";
        }
        else if(CurMode == runMode)
        {
            qDebug() << "运行模式";
        }
        else if(CurMode == remoteMode)
        {
            qDebug() << "远程模式";
        }
        else
        {
            //向上 00100000
            //向下 01000000
            if(CurMode == QString("00100000"))
            {
                //上转
                qDebug() << "侧边滚轮向上";
            }
            else if(CurMode == QString("01000000"))
            {
                //下转
                qDebug() << "侧边滚轮向下";
            }
        }
    }


    if (bytesRead < 0 && errno != EAGAIN) {
        qWarning() << "Read error:" << strerror(errno);
    }
}



void MainWindow::getKeyValue()
{
    struct input_event keyBuf;
    unsigned int bytesRead = ::read(buttonfd,&keyBuf, sizeof(keyBuf));
    if(bytesRead < sizeof(keyBuf))
    {
        return;
    }
    if(keyBuf.type==EV_KEY)
    {
        if(keyBuf.value==2)
        {
            return;
        }
        qDebug()<<"keyBuf.code = "<<keyBuf.code<<"keyBuf.value = "<<keyBuf.value<<"time = "<<QTime::currentTime().toString("hh:mm:ss.zzz")<<endl;
    }
}



void MainWindow::getEnableValue()
{
    char enablebuttons[8]={'1','1','1','1','1','1','1','1'};
    if(::read(enablefd,enablebuttons,sizeof(enablebuttons))!=sizeof(enablebuttons))
    {
        return;
    }


    QString CurEnable = "";
    for(unsigned int i=0;i<sizeof(enablebuttons);i++)
    {
        CurEnable.append(QString(QChar(enablebuttons[i])));
    }
    if(CurEnable == "11000000" ||CurEnable == "10000000" ||CurEnable == "01000000")
    {
        //上电
        qDebug() << "上电";
    }
    else if(CurEnable == "00000000")
    {
        //下电
        qDebug() << "下电";
    }
}