Data Acquisition of Wireless Barcode Gun Based on QT

Posted by henry gayman on Mon, 05 Aug 2019 11:15:31 +0200

Recent projects encountered the need to collect product barcode and two-dimensional code to save and display, and upload servers or enterprise ERP system, so for wireless and two-dimensional code scanner data acquisition test.
The javelin itself is an input device, similar to keyboard input. Bar codes are ASCII characters, and the last character is the return key. After the computer is connected with the scanner, the cursor focuses on the edit box, and the data will be automatically input into the edit box after the scanner. This paper mainly explains how to get the specific data of the input barcode by intercepting WM_CHAR messages when there is no edit box.

1. Software and hardware configuration

  • Komi wireless scanner (bar code), model: YX-38
  • Software environment Qt5.9.6

2. Solutions

1. Generally, the scanner is collected through serial port. Its advantage is that it can achieve no focus collection and can be stored in a fixed memory address. The disadvantage is that the serial port must be connected with a serial port line, and the user experience is not good.
2.Qt uses keyboard events to determine the specific content of barcode input, so it can determine the input of the code-sweeping gun by monitoring the input of the virtual keyboard, so as to achieve non-focus acquisition.

Above is the picture of the setup of the scanner, which can set different working modes. When the scanner exceeds the range of wireless transmission, it can use the inventory mode, so that the scanner data can be saved to the scanner, and then the scanner data can be uploaded after the inventory is completed, so that the off-line goods inventory can be realized, which is still a comparative person. Sexual, but I haven't tested all of these working patterns here.

3. Specific Code Implementation

3.1 Qt's own keyboard event QEvent::KeyPress

private slots:
    //Declare KeyPress function
    void keyPressEvent(QKeyEvent* event);

The above is the function declaration of the header file, and its specific implementation code is as follows:

void Widget::keyPressEvent(QKeyEvent* event)
{
    switch(event->key())
        {
            case Qt::Key_Down:
            {
                qDebug()<<"ok";
            }
                break;
            case Qt::Key_F1:
            {
                qDebug()<<"ssss";
            }
        }
    if (0x30 <= event->key() && 0x39 >= event->key())
       {
        char st=event->key();
        QString str_ascii = QString(st);
        qDebug()<<str_ascii;
        qDebug()<<event->key();
       }
     else if (0x41 <= event->key() && 0x48 >= event->key())
       {
       qDebug()<<event->key();
       }else
       {
       qDebug()<<event->key();
       }
}

The corresponding values of keys in keyboard events are shown in the following table:

3.2 Write the global keyboard monitoring function by oneself

Using notify() function to monitor keyboard

bool GlobalApplication::notify(QObject *obj, QEvent *e)
{
    const QMetaObject* objMeta = obj->metaObject();
    QString clName = objMeta->className();
//    qDebug()<<clName;

//if(clName == "Form")
//    {
    if(e->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
            if(keyEvent->key() == 16777220)
            {
                qDebug()<<strinput;
                qDebug() << clName;
                strinput.clear();
            }
            else
            {
                char st=keyEvent->key();
                QString str_ascii = QString(st);
                strinput +=str_ascii;
 //               qDebug()<<strinput;
            }
            return true;
         }
//}

//if(clName == "QWidgetWindow")
//{
//    if(e->type() == QEvent::KeyPress)
//    {
//        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
//        if(keyEvent->key() == Qt::Key_F1)
//        {
//            qDebug() << clName;
//            qDebug() << "F1";
//        }
// //     widget->keyPress(keyEvent);
//     }
//    else if(e->type() == QEvent::MouseButtonPress)
//    {
//        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
//        if(mouseEvent->buttons() == Qt::LeftButton)
//        qDebug() << "left";
//    }
//}

    return QApplication::notify(obj,e);
}

This function can also be used for mouse monitoring, where the comment part involves mouse monitoring. Global monitoring needs to be set in the main.cpp file as the global application class

int main(int argc, char *argv[])
{
    GlobalApplication a(argc, argv);
 //   QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

3.3 Attach all codes and test results

globalapplication.h

#ifndef GLOBALAPPLICATION_H
#define GLOBALAPPLICATION_H
#include<QApplication>

class GlobalApplication : public QApplication
{
public:
    GlobalApplication(int&argc,char **argv);
    ~GlobalApplication();

    bool notify(QObject*,QEvent *);
    void setWindowInstance(QWidget *wnd);
private:
    QWidget *widget;
       QString strinput;
};

#endif // GLOBALAPPLICATION_H

globalapplication.cpp

#include "globalapplication.h"
#include <QApplication>
#include <QKeyEvent>
#include <QDebug>

GlobalApplication::GlobalApplication(int &argc,char **argv):
QApplication(argc,argv)
{

}

GlobalApplication::~GlobalApplication()
{

}

void GlobalApplication::setWindowInstance(QWidget *wnd)
{
    widget = wnd;
}

bool GlobalApplication::notify(QObject *obj, QEvent *e)
{
    const QMetaObject* objMeta = obj->metaObject();
    QString clName = objMeta->className();
//    qDebug()<<clName;

//if(clName == "Form")
//    {
    if(e->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
            if(keyEvent->key() == 16777220)
            {
                qDebug()<<strinput;
                qDebug() << clName;
                strinput.clear();
            }
            else
            {
                char st=keyEvent->key();
                QString str_ascii = QString(st);
                strinput +=str_ascii;
 //               qDebug()<<strinput;
            }
            return true;

         }

//}

//if(clName == "QWidgetWindow")
//{
//    if(e->type() == QEvent::KeyPress)
//    {
//        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
//        if(keyEvent->key() == Qt::Key_F1)
//        {
//            qDebug() << clName;
//            qDebug() << "F1";
//        }
// //     widget->keyPress(keyEvent);
//     }
//    else if(e->type() == QEvent::MouseButtonPress)
//    {
//        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
//        if(mouseEvent->buttons() == Qt::LeftButton)
//        qDebug() << "left";
//    }
//}


    return QApplication::notify(obj,e);

}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "dialog.h"
#include "form.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    Ui::Widget *ui;
    Dialog *p_dialog;
    Form *p_form;

private slots:

    void on_pushButton_Load_clicked();
    void on_pushButton_test_clicked();
    void keyPressEvent(QKeyEvent* event);
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "dialog.h"
#include <QDebug>
#include<QKeyEvent>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    p_dialog = NULL;
    p_form = NULL;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_Load_clicked()
{
    if(p_dialog == NULL)
    {
        p_dialog =new Dialog();
        p_dialog->setWindowTitle(QString::fromLocal8Bit("Virtual Keyboard Testing"));
        p_dialog->show();
        qDebug()<<"Test"<<endl;
    }
    else
    {
        p_dialog->show();

    }
}

void Widget::on_pushButton_test_clicked()
{
    if(p_form == NULL)
    {
        p_form =new Form();
        p_form->setWindowTitle(QString::fromLocal8Bit("test"));
        p_form->show();
//        qDebug()<<"Test"<<endl;
    }
    else
    {
        p_form->show();

    }
}

void Widget::keyPressEvent(QKeyEvent* event)
{
    switch(event->key())
        {
            case Qt::Key_Down:
            {
                qDebug()<<"ok";
            }
                break;
            case Qt::Key_F1:
            {
                qDebug()<<"ssss";
            }
        }


//    if (0x30 <= event->key() && 0x39 >= event->key())
//       {
//        char st=event->key();
//        QString str_ascii = QString(st);
//        qDebug()<<str_ascii;

//           qDebug()<<event->key();
//       }
//       else if (0x41 <= event->key() && 0x48 >= event->key())
//       {

//       qDebug()<<event->key();
//       }
//       else
//       {

//       qDebug()<<event->key();
//       }

}

3.4 Code Warehouse

Code Cloud Warehouse Address
How to distinguish between keyboard input and javelin input in the next section?

Topics: C++ Qt ascii Qt5