(Solved) Serial port in different thread
-
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? -
@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 -
@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? -
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. -
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? -
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.
-
@Ramon
Super. Thank you for updating thread.