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. Reading data from Serial Port is extremely slow

Reading data from Serial Port is extremely slow

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 469 Views
  • 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.
  • S Offline
    S Offline
    serkan_tr
    wrote on 7 Feb 2023, 14:05 last edited by
    #1

    On the QT C++ side, I want to transfer the potentiometer data I got from Arduino to the slider on the screen. but 3-5 minutes after moving the potentiometer, the slider starts to move this is a huge delay codes i think it's an error in the code part:
    arduino Code:

    void setup() {
      Serial.begin(115200);
    }
    void loop(){
    void loop() {
      uint8_t data = map(analogRead(A0),0,1023,0,255);
      Serial.write(data);
    }
    

    QT C++ code

    joy = std::thread{std::bind(&MainWindow::setRxData, this)};
    .
    .
    bool MainWindow::rxData(){
            while(true){
                if(serial_.waitForReadyRead(10)){
                    uint8_t value;
                    quint64 size = serial_.read((char*)&value, sizeof(value));
                    qDebug() << "Read" << value << "bytes:";
                    data_ch[0] =  value;
                    sliderChange();
    }}}
    
    void MainWindow::sliderChange(){
        ui->slider_1->setValue(data_ch[0]);
    }
    
    
    J 1 Reply Last reply 7 Feb 2023, 14:15
    0
    • S serkan_tr
      7 Feb 2023, 14:05

      On the QT C++ side, I want to transfer the potentiometer data I got from Arduino to the slider on the screen. but 3-5 minutes after moving the potentiometer, the slider starts to move this is a huge delay codes i think it's an error in the code part:
      arduino Code:

      void setup() {
        Serial.begin(115200);
      }
      void loop(){
      void loop() {
        uint8_t data = map(analogRead(A0),0,1023,0,255);
        Serial.write(data);
      }
      

      QT C++ code

      joy = std::thread{std::bind(&MainWindow::setRxData, this)};
      .
      .
      bool MainWindow::rxData(){
              while(true){
                  if(serial_.waitForReadyRead(10)){
                      uint8_t value;
                      quint64 size = serial_.read((char*)&value, sizeof(value));
                      qDebug() << "Read" << value << "bytes:";
                      data_ch[0] =  value;
                      sliderChange();
      }}}
      
      void MainWindow::sliderChange(){
          ui->slider_1->setValue(data_ch[0]);
      }
      
      
      J Offline
      J Offline
      JonB
      wrote on 7 Feb 2023, 14:15 last edited by
      #2

      @serkan_tr
      Does it make any difference if you do not create a separate thread and use waitForReadyRead() but rather just use Qt's asynchronous serial calls instead?

      S 1 Reply Last reply 7 Feb 2023, 14:18
      0
      • J JonB
        7 Feb 2023, 14:15

        @serkan_tr
        Does it make any difference if you do not create a separate thread and use waitForReadyRead() but rather just use Qt's asynchronous serial calls instead?

        S Offline
        S Offline
        serkan_tr
        wrote on 7 Feb 2023, 14:18 last edited by
        #3

        @JonB I do not fully understand

        J 1 Reply Last reply 7 Feb 2023, 14:39
        0
        • S serkan_tr
          7 Feb 2023, 14:18

          @JonB I do not fully understand

          J Offline
          J Offline
          JonB
          wrote on 7 Feb 2023, 14:39 last edited by JonB 2 Jul 2023, 14:40
          #4

          @serkan_tr
          I do not know why you use std::thread, a while(true) loop and waitForReadyRead(). I suggested you try with Qt's QSerialPort asynchronous signals/slots and no separate thread to see whether that affected performance. I do not know whether that is an issue.

          Furthermore (all related to your thread usage): do I understand that your MainWindow::rxData() runs in some other thread? But it calls ui->slider_1->setValue(), it is forbidden in Qt to access the main UI widgets from any other thread....

          S 1 Reply Last reply 22 Feb 2023, 04:59
          4
          • S serkan_tr has marked this topic as solved on 13 Feb 2023, 12:50
          • J JonB
            7 Feb 2023, 14:39

            @serkan_tr
            I do not know why you use std::thread, a while(true) loop and waitForReadyRead(). I suggested you try with Qt's QSerialPort asynchronous signals/slots and no separate thread to see whether that affected performance. I do not know whether that is an issue.

            Furthermore (all related to your thread usage): do I understand that your MainWindow::rxData() runs in some other thread? But it calls ui->slider_1->setValue(), it is forbidden in Qt to access the main UI widgets from any other thread....

            S Offline
            S Offline
            serkan_tr
            wrote on 22 Feb 2023, 04:59 last edited by
            #5

            @JonB
            Thank you

            1 Reply Last reply
            0

            3/5

            7 Feb 2023, 14:18

            • Login

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