QT SERIAL PORT THREADING CRASH APP
-
@jsulm Sir,look you have answered my other question same way. I need thread in my specific situation ( can not be explained ) .
@canrollas Also, where do you call setUpSerialPort? You need to understand that you should create all needed variables (like serialPort) inside run() method, else they will live in the thread which starts your io thread.
-
@canrollas Then please run through debugger until it crashes and post the stack trace here. Debugging is the first thing to do in case of a crash.
-
@canrollas As you can see it crashes in executeOrder in file IOThread.cpp line 237 - what is in that line?
-
@canrollas Also, where do you call setUpSerialPort? You need to understand that you should create all needed variables (like serialPort) inside run() method, else they will live in the thread which starts your io thread.
this is execute function
else if(order == 3){ qInfo()<<"This is general monitor stop"; // Kill all jedi masters :D if(connectionStatus == true){ if(monitoringStatus == true){ QByteArray b("0A53"); QByteArray bytes = QByteArray::fromHex(b); serialPort.write(bytes); serialPort.waitForBytesWritten(); qInfo()<<"Stopping the monitoring device!"<<Qt::endl; monitoringStatus = false; emit sendMonitoringStatus(false); }else{ emit sendMessageToSerialDevice("First start the monitoring device!"); } }else{ emit sendMessageToSerialDevice("First connect to the device via makcon!"); }
this is caller function
@jsulm void SerialDevice::connectToCanBus(){ QString portname ; foreach (auto looper, serialPortInfo->availablePorts()) { qInfo()<<"This is looping device on the machine:"<<looper.description()<<looper.portName(); if(looper.description()=="Makersan USB to CAN Converter" || looper.description()=="STMicroelectronics Virtual COM Port" && looper.portName().contains("tty")==true || looper.portName().contains("COM")==true){ qInfo()<<"The device has found in the loop:"<<looper.description(); portname = looper.portName(); thread->setUpSerialPort(looper.portName(),9600); thread->addDataCommandToQList(0); if(thread->isRunning()==false){ thread->start(); } } } if(portname.isEmpty()==true){ emit sendMessageToMainWindow("Device is not plugged or not detected"); } }
-
this is execute function
else if(order == 3){ qInfo()<<"This is general monitor stop"; // Kill all jedi masters :D if(connectionStatus == true){ if(monitoringStatus == true){ QByteArray b("0A53"); QByteArray bytes = QByteArray::fromHex(b); serialPort.write(bytes); serialPort.waitForBytesWritten(); qInfo()<<"Stopping the monitoring device!"<<Qt::endl; monitoringStatus = false; emit sendMonitoringStatus(false); }else{ emit sendMessageToSerialDevice("First start the monitoring device!"); } }else{ emit sendMessageToSerialDevice("First connect to the device via makcon!"); }
this is caller function
@jsulm void SerialDevice::connectToCanBus(){ QString portname ; foreach (auto looper, serialPortInfo->availablePorts()) { qInfo()<<"This is looping device on the machine:"<<looper.description()<<looper.portName(); if(looper.description()=="Makersan USB to CAN Converter" || looper.description()=="STMicroelectronics Virtual COM Port" && looper.portName().contains("tty")==true || looper.portName().contains("COM")==true){ qInfo()<<"The device has found in the loop:"<<looper.description(); portname = looper.portName(); thread->setUpSerialPort(looper.portName(),9600); thread->addDataCommandToQList(0); if(thread->isRunning()==false){ thread->start(); } } } if(portname.isEmpty()==true){ emit sendMessageToMainWindow("Device is not plugged or not detected"); } }
@canrollas Which line in that code is 237 please?
-
@canrollas Which line in that code is 237 please?
-
@canrollas Which line in that code is 237 please?
-
@jsulm @canrollas
I have the same issue both on Windows 10 and Ubuntu 20.04. Crash occurs on call towaitForBytesWritten()
+1 to solve this bug
-
@jsulm @canrollas
I have the same issue both on Windows 10 and Ubuntu 20.04. Crash occurs on call towaitForBytesWritten()
+1 to solve this bug
@Valentin49 said in QT SERIAL PORT THREADING CRASH APP:
I have the same issue
then please create a minimal compileable example which reproduces your crash and create a new thread instead hijacking an old one
-
Hi, I just stumbled acrros the same issue as yours when switching from Qt5.15.2 MinGW 32-bits to Qt6.3.2 MinGW 64-bits. My application was working fine with Qt5, but was constantly crashing when calling QSerialPort::waitForBytesWritten() when compiling using Qt6.
My original code:
// My QSerialPort object runs in its own thread QSerialPort *serial = new QSerialPort(this); // initializationn of serial object ... void MySerialClass::write( const QByteArray& bytes ) { serial->write(bytes); if(!serial->waitForBytesWritten()) { qWarning() << "Write timeout!"; } }
I found a fix by trial and error: I verify that there is indeed some bytes waiting to be written before calling the function:
void MySerialClass::write( const QByteArray& bytes ) { // write() returns the number of bytes that were actually written, or -1 if error quint64 bytes_written = serial->write(bytes); if(bytes_written != bytes.size() && !serial->waitForBytesWritten()) { qWarning() << "Write timeout!"; } }
Hope it works for you!