Reading data from Serial Port is extremely slow
-
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]); }
-
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]); }
@serkan_tr
Does it make any difference if you do not create a separate thread and usewaitForReadyRead()
but rather just use Qt's asynchronous serial calls instead? -
@serkan_tr
Does it make any difference if you do not create a separate thread and usewaitForReadyRead()
but rather just use Qt's asynchronous serial calls instead? -
@serkan_tr
I do not know why you usestd::thread
, awhile(true)
loop andwaitForReadyRead()
. I suggested you try with Qt'sQSerialPort
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 callsui->slider_1->setValue()
, it is forbidden in Qt to access the main UI widgets from any other thread.... -
S serkan_tr has marked this topic as solved on
-
@serkan_tr
I do not know why you usestd::thread
, awhile(true)
loop andwaitForReadyRead()
. I suggested you try with Qt'sQSerialPort
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 callsui->slider_1->setValue()
, it is forbidden in Qt to access the main UI widgets from any other thread....