Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why is usb hid getting slow speed?
Forum Updated to NodeBB v4.3 + New Features

Why is usb hid getting slow speed?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 855 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DQUY05D Offline
    DQUY05D Offline
    DQUY05
    wrote on last edited by
    #1

    Hello !

    I am using hid_device, to read data from incoming usb port (this data I get is transmitted by mcu)
    Here is my receive interrupt function, it works fine, but only at 10ms 1 time,

    QObject::connect(this,SIGNAL(hiddataarrived(unsigned char *, unsigned char)),this,SLOT(Outdata(unsigned char *, unsigned char)));
    
    void MainWindow::Outdata(unsigned char *data, unsigned char length)  // slot
    {
        QString chuoi;
        for(int i=0;i<length;i++)
        {
            chuoi.append(data[i]);
        }
        ui->textEdit_2->append(chuoi);  // show on textedit
    }
    

    when i increase the speed between transmissions, about 5ms 1 time, some data is lost
    Is it because the show function on textedit makes it late? or what is the reason?

    Thank you !

    SGaistS 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @DQUY05 said in Why is usb hid getting slow speed?:

      hiddataarrived

      Where does this signal comes from? It's not a Qt signal and we don't know what it does/where it comes from.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      DQUY05D 1 Reply Last reply
      0
      • DQUY05D DQUY05

        Hello !

        I am using hid_device, to read data from incoming usb port (this data I get is transmitted by mcu)
        Here is my receive interrupt function, it works fine, but only at 10ms 1 time,

        QObject::connect(this,SIGNAL(hiddataarrived(unsigned char *, unsigned char)),this,SLOT(Outdata(unsigned char *, unsigned char)));
        
        void MainWindow::Outdata(unsigned char *data, unsigned char length)  // slot
        {
            QString chuoi;
            for(int i=0;i<length;i++)
            {
                chuoi.append(data[i]);
            }
            ui->textEdit_2->append(chuoi);  // show on textedit
        }
        

        when i increase the speed between transmissions, about 5ms 1 time, some data is lost
        Is it because the show function on textedit makes it late? or what is the reason?

        Thank you !

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @DQUY05 said in Why is usb hid getting slow speed?:

        void MainWindow::Outdata(unsigned char *data, unsigned char length) // slot
        {
        QString chuoi;
        for(int i=0;i<length;i++)
        {
        chuoi.append(data[i]);
        }
        ui->textEdit_2->append(chuoi); // show on textedit
        }

        Hi,

        Beside the information requested by @Christian-Ehrlicher, this loop is unnecessary. QString can be built taking a char * and a length. Check the fromXXX methods.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        DQUY05D 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @DQUY05 said in Why is usb hid getting slow speed?:

          hiddataarrived

          Where does this signal comes from? It's not a Qt signal and we don't know what it does/where it comes from.

          DQUY05D Offline
          DQUY05D Offline
          DQUY05
          wrote on last edited by
          #4

          @Christian-Ehrlicher
          Here is the signal constructor, the interrupt occurs every time there is data from the usb port (64 bytes), and if I remove the command QCoreApplication::processEvents();
          the program freezes

          void MainWindow::run()
          {
              int res,i;
              unsigned char buf1[64];
          
              while(isopen==1)
              {
                  res=hid_read(target, buf1, 64);   // read data from usb_hid
                  if(res==-1)     // if an error occurs 
                  {
                     isopen=0;
                     ui->listWidget->clear();
                     ui->pushButton_2->setText("Not connect");
                  }
                  else if(res==0) // No data
                  {}
                  else  //if(res>0)
                  {
                      unsigned char datas[64];
          
                      for(i=0;i<64;i++)
                      {
                          datas[i]=buf1[i];
                      }
          
                      emit hiddataarrived(datas,64);
                  }
                  QCoreApplication :: processEvents ();   //avoid freezing 
              }
          }
          

          The receive interrupt speed I need it to achieve at 100us per 64 byte transmission, but currently only 10ms per 64 byte transmission, as the baud rate increases, the data will be interrupted

          Thanks!

          1 Reply Last reply
          0
          • SGaistS SGaist

            @DQUY05 said in Why is usb hid getting slow speed?:

            void MainWindow::Outdata(unsigned char *data, unsigned char length) // slot
            {
            QString chuoi;
            for(int i=0;i<length;i++)
            {
            chuoi.append(data[i]);
            }
            ui->textEdit_2->append(chuoi); // show on textedit
            }

            Hi,

            Beside the information requested by @Christian-Ehrlicher, this loop is unnecessary. QString can be built taking a char * and a length. Check the fromXXX methods.

            DQUY05D Offline
            DQUY05D Offline
            DQUY05
            wrote on last edited by
            #5

            @SGaist said in Why is usb hid getting slow speed?:

            QString can be built taking a char * and a length

            Dear Sir !

            Sorry, I'm newbie, and my English is not very good, you mean QString::fromUtf8(data,length);
            I tried, but I don't know if this is correct, but I don't get a QString

            void MainWindow::Outdata(const char *data, unsigned char length) // slot
            {
            QString chuoi;
            //    for(int i=0;i<length;i++)
            //    {
            //        chuoi.append(data[i]);
            //    }
            chuoi=QString::fromUtf8(data,length);
            ui->textEdit_2->append(chuoi); // show on textedit
            }
            

            I don't know how to use this structure, is it because my loop is late and lose data?

            Many thanks!

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @DQUY05 said in Why is usb hid getting slow speed?:

              hid_read

              When this is a blocking function than you must not do this in the main thread. Move this out into another thread.
              And only reading 64 bytes at once can not be "fast".

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              DQUY05D 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @DQUY05 said in Why is usb hid getting slow speed?:

                hid_read

                When this is a blocking function than you must not do this in the main thread. Move this out into another thread.
                And only reading 64 bytes at once can not be "fast".

                DQUY05D Offline
                DQUY05D Offline
                DQUY05
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                Thank you,
                I am using libusb (usb_hid), and also know the max speed is about 64Kbyte/s, here mine is 64Byte/5ms = 12.5Kbyte/s, anyway my project will accept working at this speed.

                When this is a blocking function than you must not do this in the main thread. Move this out into another thread.

                Sorry, I don't know how?

                Many thanks!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Since you are pretty new to all of this, you might want to consider using a library wrapping libusb for Qt.

                  On a side note, many MCUs have serial port to communicate with which is usually way simpler to use. Is it not the case of yours ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  DQUY05D 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Since you are pretty new to all of this, you might want to consider using a library wrapping libusb for Qt.

                    On a side note, many MCUs have serial port to communicate with which is usually way simpler to use. Is it not the case of yours ?

                    DQUY05D Offline
                    DQUY05D Offline
                    DQUY05
                    wrote on last edited by
                    #9

                    @SGaist
                    Thank you !

                    Since you are pretty new to all of this, you might want to consider using a library wrapping libusb for Qt.

                    You are right, I am using this set of libraries.

                    On a side note, many MCUs have serial port to communicate with which is usually way simpler to use. Is it not the case of yours ?

                    Serial port I have successfully done, however it is asynchronous communication, and the speed is usually lower than usb_hid, in this project of mine, which requires using usb_hid, I have increased the speed to 25Kbyte/s, and got the desired result, please write to solve the problem, in the future if anyone can increase the speed, hope to have development

                    Regards !

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved