QSerialPort
-
Hi
I am running qthread. In this thread I run infinitive loop. In loop I am reading USB0tty port. On usb port I have arduino nano with sensors writing values to usb port.
After work is done in loop I emit signal to another object. If I use clear function with QSerialPort object everything work fine in almost real time but I get error.QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread.
If I do not use clear method the error goes away but the message/signal from qthread (signal/slot) comes slow (values do not change) from qtgread object to another object in my qt app.
I am not expert in USB port anf QSerialPort but I think it has to do with buffer clearance.Any Ideas?
Thomas..h file #ifndef READTTYUSB0_H #define READTTYUSB0_H #include <QObject> #include "QtSerialPort/QSerialPort" class readttyUSB0 : public QObject { Q_OBJECT public: readttyUSB0(); ~readttyUSB0(); public slots: void doWork(); signals: void usb0Data(QString); private: QSerialPort* sPort = new QSerialPort; QByteArray data; QString str; }; #endif // READTTYUSB0_H .cpp file #include "readttyusb0.h" #include "QThread" readttyUSB0::readttyUSB0() { sPort->setPortName("/dev/ttyUSB0"); sPort->setBaudRate(9600,QSerialPort::AllDirections); sPort->setDataBits(QSerialPort::Data8); sPort->setFlowControl(QSerialPort::NoFlowControl); sPort->setParity(QSerialPort::NoParity); sPort->setStopBits(QSerialPort::OneStop); if(sPort->open(QIODevice::ReadWrite)) { sPort->setDataTerminalReady(QSerialPort::NoSignal); } } readttyUSB0::~readttyUSB0() { delete sPort; } void readttyUSB0::doWork() { while(1) { if (QThread::currentThread()->isInterruptionRequested()) { return; } if(sPort->isOpen()) { sPort->waitForReadyRead(); data=sPort->readLine(); str=QString::fromStdString(data.toStdString()); emit usb0Data(str); sPort->clear(); } } } -
Hi
I am running qthread. In this thread I run infinitive loop. In loop I am reading USB0tty port. On usb port I have arduino nano with sensors writing values to usb port.
After work is done in loop I emit signal to another object. If I use clear function with QSerialPort object everything work fine in almost real time but I get error.QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread.
If I do not use clear method the error goes away but the message/signal from qthread (signal/slot) comes slow (values do not change) from qtgread object to another object in my qt app.
I am not expert in USB port anf QSerialPort but I think it has to do with buffer clearance.Any Ideas?
Thomas..h file #ifndef READTTYUSB0_H #define READTTYUSB0_H #include <QObject> #include "QtSerialPort/QSerialPort" class readttyUSB0 : public QObject { Q_OBJECT public: readttyUSB0(); ~readttyUSB0(); public slots: void doWork(); signals: void usb0Data(QString); private: QSerialPort* sPort = new QSerialPort; QByteArray data; QString str; }; #endif // READTTYUSB0_H .cpp file #include "readttyusb0.h" #include "QThread" readttyUSB0::readttyUSB0() { sPort->setPortName("/dev/ttyUSB0"); sPort->setBaudRate(9600,QSerialPort::AllDirections); sPort->setDataBits(QSerialPort::Data8); sPort->setFlowControl(QSerialPort::NoFlowControl); sPort->setParity(QSerialPort::NoParity); sPort->setStopBits(QSerialPort::OneStop); if(sPort->open(QIODevice::ReadWrite)) { sPort->setDataTerminalReady(QSerialPort::NoSignal); } } readttyUSB0::~readttyUSB0() { delete sPort; } void readttyUSB0::doWork() { while(1) { if (QThread::currentThread()->isInterruptionRequested()) { return; } if(sPort->isOpen()) { sPort->waitForReadyRead(); data=sPort->readLine(); str=QString::fromStdString(data.toStdString()); emit usb0Data(str); sPort->clear(); } } }What do you expect from emitting a signal in awhile(1)loop?!
It will never get processedEdit:
Ah havent seen your comment. You only emit once when the loop has ended.Have a look at the synchronous and asynchronous
QSerialPortexamples here: -
Hi,
Is it expected that you read only one line ?
You should rather get everything from the serial port, accumulate in a buffer and then process the data once you have a full frame (or more). -
Hi,
Is it expected that you read only one line ?
You should rather get everything from the serial port, accumulate in a buffer and then process the data once you have a full frame (or more). -
Can you share also the thread related code ?
-
What do you expect from emitting a signal in awhile(1)loop?!
It will never get processedEdit:
Ah havent seen your comment. You only emit once when the loop has ended.Have a look at the synchronous and asynchronous
QSerialPortexamples here:@Pl45m4 Hi.
I am sorry but the code is correct on syntax, semantic and semiotic/pragmatic level. The creation of and object has to happen inside the working loop I concluded; (in run loop). I think it has to do with usb port access on low level (QSerialPort c/c++ Qt code). I think. The error is gone now.I got it from one of SGaist comments/replies.
Perhaps SGaist could give us more enlightenment why it has to be inside run loop (or custom loop like I have)? Perhaps because it is made on heap?
regards, Thomas
-
@Pl45m4 Hi.
I am sorry but the code is correct on syntax, semantic and semiotic/pragmatic level. The creation of and object has to happen inside the working loop I concluded; (in run loop). I think it has to do with usb port access on low level (QSerialPort c/c++ Qt code). I think. The error is gone now.I got it from one of SGaist comments/replies.
Perhaps SGaist could give us more enlightenment why it has to be inside run loop (or custom loop like I have)? Perhaps because it is made on heap?
regards, Thomas
-
Where is readttyUSB0 instantiated / is it moved to a thread somewhere? If yes then you forgot to move sPort (btw: very strange name for a member var) as it has no parent.
-
Where is readttyUSB0 instantiated / is it moved to a thread somewhere? If yes then you forgot to move sPort (btw: very strange name for a member var) as it has no parent.
@Christian-Ehrlicher Hi. Sorry for my late reply. Well it is in mainwindow constructor. This is in constructor.
rttyUSB0->moveToThread(threadttyUSB0); connect(threadttyUSB0,SIGNAL(started()),rttyUSB0,SLOT(doWork())); connect(rttyUSB0,SIGNAL(usb0Data(QString)),this,SLOT(readSerialCom(QString))); threadttyUSB0->start();```Declared in .h file in private part.
What I do in readSerialCom(QString) is not important.
I let this loop to run for a week with no problem. No compile error. Should be ok.
Thomas
-
@SGaist Hi. SGaist. One question. What is the best way (board?) to boot Linux into Qt graphic App (I need access to /dev- USB) in max 2 to 3 seconds? But to be on open source non commercial side. I tried raspberry Pi and eglfs but it needs 10 seconds. regards Thomas
-
@SGaist Hi. SGaist. One question. What is the best way (board?) to boot Linux into Qt graphic App (I need access to /dev- USB) in max 2 to 3 seconds? But to be on open source non commercial side. I tried raspberry Pi and eglfs but it needs 10 seconds. regards Thomas
@Tomaz if you are using yocto, you can disable certain services at start or change the boot order by prioritising your app
-
@SGaist Hi. SGaist. One question. What is the best way (board?) to boot Linux into Qt graphic App (I need access to /dev- USB) in max 2 to 3 seconds? But to be on open source non commercial side. I tried raspberry Pi and eglfs but it needs 10 seconds. regards Thomas