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. (Solved) Serial port in different thread
Forum Updated to NodeBB v4.3 + New Features

(Solved) Serial port in different thread

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.1k Views 3 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.
  • R Offline
    R Offline
    Ramon
    wrote on last edited by Ramon
    #1

    Hi,
    I have a Qt application which sends a command to a microcontroller to measure some voltage for 'x' seconds. Once the command is sent, a progress bar will increment from 0-100% corresponding to 0-'(x+3)' seconds. Meanwhile, the micro will store the voltage in binary and send it together in ASCII format (around 20 kilobytes) after 'x' seconds via serialport. End of data will be signified by ';' (semicolon). Qt should check the received data for ';' and keep storing it in a file till ';' is detected. The additional 3 seconds is for data reception (20kB/115200 baud = 1.5sec). How should the code be written?

    I tried calling the SIGNAL-SLOT mechanism first

    void PortListener::SerialRead(QString fileNameASCII, int ExperimentDuration){
        NoOfBytes = 0;
        qDebug() <<"Entered SerialRead";
        activeFileName = fileNameASCII;
        connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
        }
    
    void PortListener::onReadyRead(){
        qDebug() << "Entered onReadyRead code section";
        QByteArray bytes;
        int a = port->bytesAvailable();
        bytes.resize(a);
        port->read(bytes.data(), bytes.size());
        QString b = bytes;
        if(b == ";"){
            disconnect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
        }
        else{
            Write(activeFileName, &bytes);
        }
    }
    void PortListener::Write (QString activeFileName, QByteArray a[]){
        int i=0;
        QFile mFile(activeFileName);
        if(!mFile.open(QFile::Append | QFile::Text)){
            qDebug() << "Could not open file for writing";
            return;
        }
        qDebug() << "Opened file for writing" << activeFileName;
        QTextStream out(&mFile);
            out << a[i];
    
        mFile.flush();
        mFile.close();
    
    

    followed by the display of progress bar from MainWindow as shown:

    timer_progress->start((ExperimentDuration+3));
    connect (timer_progress, SIGNAL(timeout()), this, SLOT(move_progressbar()));
    progress.exec();
    
    void MainWindow::move_progressbar(){
        progress.setModal(true);
        progress.show();
        progress.setProgressValue();
    
        if(progress.getProgressValue() >= 100){
            progress.setProgressValueZero();
            progress.close();
            timer_progress->stop();
        }
    }
    
    void ProgressDialog::setProgressValue(){
        int value = ui->progressBar->value();
        ui->progressBar->setValue(value+1);
    }
    

    I expect the progress bar incrementing to take place in parallel along with the reception of serialdata & checking for ';' symbol. But it doesn't. Why?
    Is it necessary to run Serialport in a parallel thread?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @Ramon said:

      progress.setModal(true);

      Do you use a modal dialog ?

      I think it has its own message loop and you would then
      block the one that drives main while dialog is showing.
      But You dont seems to use exec() so not sure that is the issue.

      Did you read about Progress Dialog and modeless etc ?
      http://doc.qt.io/qt-5.5/qprogressdialog.html#details

      R 1 Reply Last reply
      1
      • mrjjM mrjj

        @Ramon said:

        progress.setModal(true);

        Do you use a modal dialog ?

        I think it has its own message loop and you would then
        block the one that drives main while dialog is showing.
        But You dont seems to use exec() so not sure that is the issue.

        Did you read about Progress Dialog and modeless etc ?
        http://doc.qt.io/qt-5.5/qprogressdialog.html#details

        R Offline
        R Offline
        Ramon
        wrote on last edited by Ramon
        #3

        @mrjj said:

        But You dont seems to use exec() so not sure that is the issue.
        Did you read about Progress Dialog and modeless etc ?

        Hi,
        I do use progress.exec() to display the progressdialog window. It's after the connect (timer/move_progressbar) line in the first post.
        If this is the reason why serialport doesn't work, the show() function should work, right?

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          well since you call setModal
          im not 100% sure using only show is enough.

          Normally one would make sure Dialog is modeless and show it with show() to allow normal window processing. (of mainwin)

          I have not tried with QSerial but I sort of tried the same with some TCP/IP where I want to show progress and modeless
          QProgressDialog worked without blocking.

          1 Reply Last reply
          1
          • HamedH Offline
            HamedH Offline
            Hamed
            wrote on last edited by Hamed
            #5

            Hi
            Sorry I didn't get the question exactly, Here you have problem in receiving from serial port or extracting received data to the model you want or showing it on a progress bar?
            Can you tell this by debugging you code?

            HamedBabaeyan@yahoo.com
            for more interesting stuff

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Ramon
              wrote on last edited by
              #6

              Hi,

              Thanks to mrjj, the issue is solved. :)
              Setting the window as modeless and using show() worked. Thank you!

              @Hamed : I appreciate your response; the issue has been solved.

              mrjjM 1 Reply Last reply
              0
              • R Ramon

                Hi,

                Thanks to mrjj, the issue is solved. :)
                Setting the window as modeless and using show() worked. Thank you!

                @Hamed : I appreciate your response; the issue has been solved.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Ramon
                Super. Thank you for updating thread.

                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