Make a Qtconcurrent thread for a USB communication between computer and microcontoller



  • Hello,

    I have written a Qt software that communicate with a microcontroller by USB.
    I want to put the function pollUSB in a separate thread to avoid frozen of the GUI. I have read that it is possible using Qtconcurrent but I don't how I have to do that in my case because the function pollUSB is not directly in the main.cpp file.

    My software is structured like this :
    Main.cpp : Contain the main function and the instanciation of a GUI object define by a specific class.
    This GUI class instanciate USB communication object from anioher class (USB class).
    And finaly, the function pollUSB is amember funciton of this USB Class.

    I don't where and how i have to write the line :
    @Qfuture <void> future = Qtconcurent::run(???)@

    Could someone now and help me to solve my problem. Thank you for advance.


  • Lifetime Qt Champion

    Without more information from your side it's difficult to help. From your description you should probably use a worker object with QThread which is described in the documentation.



  • bq. I have written a Qt software that communicate with a microcontroller by USB.

    In a common case, there are not need to use threads for this, since OS (as Windows, as Linux and so on) provides async API to communicate with USB directly (to read/write endpoints and so on).



  • see whether it helps you.
    https://github.com/bimetek/QUSB



  • Hi, I use this for my communication with usb device.

    For me I launch at the end of constructor of my GUI

    something like that

    @
    ...//constructor end
    QtConcurrent::run(Caller class (this), &MyClass::threadedFunction, parameters);
    @

    You have to put threadedFunction as slot on your MyClass

    if you want to send a parameters to threadedFunction you can make someThing like this:

    @
    public slot:
    void threadedFunction(Parameters );

    // ....

    Parameters b;
    QtConcurrent::run(this, &MyClass::threadedFunction, b);
    @

    To keep hands of "thread" you need to get an handle in FutureWatcher to stop your thread if it runs while(1).

    Good luck ...



  • Thank you very much for all reply but I still still do not understand how integrate the thread for HID_PnP::PollUSB() function in my code. You could find below my files. Thank you for help if you know where and how write code to make a thread for function HID_PnP::PollUSB() :

    *main.cpp : *
    @#include <QApplication>
    #include "usb_motor_control_app.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    USB_Motor_Control_App w;
    w.show();
    return a.exec();
    }@

    usb_motor_control_app.h :
    @#ifndef USB_MOTOR_CONTROL_APP_H
    #define USB_MOTOR_CONTROL_APP_H

    #include <QMainWindow>
    #include "hid_pnp.h"

    namespace Ui {
    class USB_Motor_Control_App;
    }

    class USB_Motor_Control_App : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::USB_Motor_Control_App *ui;
    HID_PnP *plugNPlay;

    public slots:
    void update_gui(bool isConnected, bool isPressed, int potentiometerValue);

    signals:
    void toggle_leds_button_pressed(); //Exemple de base
    void start_button_pressed();
    void stop_button_pressed();

    private slots:
    void on_pushButton_clicked(); //Exemple de base
    void onpButton_start_clicked();
    void onpButton_stop_clicked();
    void onrButton_limited_rotation_clicked();
    void onrButton_Continuous_rotation_clicked();

    };

    #endif // USB_MOTOR_CONTROL_APP_H@

    hid_pnp.h :
    @#ifndef HID_PNP_H
    #define HID_PNP_H

    #include <QObject>
    #include <QTimer>
    #include "../HIDAPI/hidapi.h"

    #include <wchar.h>
    #include <string.h>
    #include <stdlib.h>
    #include "define.h"

    class HID_PnP : public QObject
    {
    Q_OBJECT
    public:
    explicit HID_PnP(QObject *parent = 0);
    ~HID_PnP();

    signals:
    void hid_comm_update(bool isConnected, bool isPressed, int potentiometerValue);

    public slots:
    void toggle_leds();
    void PollUSB();

    private:
    bool isConnected;
    bool pushbuttonStatus;
    bool toggleLeds;
    int potentiometerValue;

    hid_device *device;
    QTimer *timer;
    unsigned char hid_buffer[HID_BUFFER_SIZE];
    
    void CloseDevice();
    

    };

    #endif // HID_PNP_H
    @


  • Lifetime Qt Champion

    What does PollUSB do ?



  • You could fin below the code of PollUSB()

    @void HID_PnP::PollUSB()
    {
    hid_buffer[0] = 0x00;
    memset((void*)&hid_buffer[2], 0x00, sizeof(hid_buffer) - 2);

    if (isConnected == false) {
        device = hid_open(BOARD_PID, BOARD_VID, NULL);
    
        if (device) {
            isConnected = true;
            hid_set_nonblocking(device, false);             //Initialement à true
            timer->start(15);
        }
    }
    else {
        if(toggleLeds == true) {
            toggleLeds = false;
    
            hid_buffer[1] = TOGGLE_LED;
    
            if (hid_write(device, hid_buffer, sizeof(hid_buffer)) == -1)
            {
                CloseDevice();
                return;
            }
    
            hid_buffer[0] = 0x00;
            hid_buffer[1] = READ_POTENTIOMETER_VALUE;
            memset((void*)&hid_buffer[2], 0x00, sizeof(hid_buffer) - 2);
        }
    
        if (hid_write(device, hid_buffer, sizeof(hid_buffer)) == -1)
        {
            CloseDevice();
            return;
        }
        if(hid_read(device, hid_buffer, sizeof(hid_buffer)) == -1)
        {
            CloseDevice();
            return;
        }
        if(hid_buffer[0] == READ_POTENTIOMETER_VALUE) {
            potentiometerValue = (hid_buffer[2]<<8) + hid_buffer[1];
            hid_buffer[1] = READ_PUSH_BUTTON_STATUS;
        }
        else if(hid_buffer[0] == READ_PUSH_BUTTON_STATUS) {
            pushbuttonStatus = (hid_buffer[1] == 0x00);
            hid_buffer[1] = READ_POTENTIOMETER_VALUE;
        }
    }
    
    hid_comm_update(isConnected, pushbuttonStatus, potentiometerValue);
    

    }@



  • I add the code of the constructor of the HID_PnP class :

    @HID_PnP::HID_PnP(QObject *parent) : QObject(parent) {
    isConnected = false;
    pushbuttonStatus = false;
    potentiometerValue = 0;
    toggleLeds = 0;

    device = NULL;
    hid_buffer[0] = 0x00;
    hid_buffer[1] = READ_POTENTIOMETER_VALUE;
    memset((void*)&hid_buffer[2], 0x00, sizeof(hid_buffer) - 2);
    
    timer = new QTimer();
    connect(timer, SIGNAL(timeout()), this, SLOT(PollUSB()));
    
    timer->start(250);
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.