serialport communication through Qthread
-
void Recivingdatata::run()
{
while (!stopped) {while (m_serial->waitForReadyRead(15)) { QByteArray data; data.append(m_serial->readAll()); qDebug() <<data.size()<<"receive___size"; { QMutexLocker locker(&dataMutex); dataQueue.enqueue(data); } // Emit signal for asynchronous data processing emit dataReceived(QList<QByteArray>() << data); data.clear(); msleep(15); // Reduced sleep duration to 1 millisecond } }
}
This is my code now i need to reduce msleep to 3 ms and waitforreadyread also to 3ms but if i do im not getting data properly -
@Ronel_qtmaster i have tried with timer of 3ms but the receiving is not properly happening i need to read 12 bytes of data from serialport that's my requirement.
-
@sanjay13 The only problem description we have is, "the receiving is not properly happening." Since your requirement can be met entirely without a separate thread, delays, or timeouts it is hard to justify fixing this logic. Why have you decided you need to do this?
-
@Ronel_qtmaster no i need to read 12byts of data every 3ms from serialport it works correctly for 15 ms wait time but when i try for 3ms the data is getting appended before or after like im getting 24 byts sometime 48 ,36,0 like that i need 12 byts every 3ms
@sanjay13 said in serialport communication through Qthread:
@Ronel_qtmaster i have tried with timer of 3ms but the receiving is not properly happening i need to read 12 bytes of data from serialport that's my requirement.
-
@Ronel_qtmaster yess i have also used timer with 3ms but the issue is the data appending before or after like i said sometimes 12 bytes is coming sometimes 36 ,48,0,60 like that its coming but im sending 12bytes of data every 3ms in receiving i have this issue i dont know why.
This is not only happening im also sending 12bytes of data to serialport plotting it in graph and this receving part is the issue now
-
@sanjay13 Receiving data that arrives from the serial port happens when data is received, not to some schedule of consumption you are trying to impose. At 9600 baud you might receive two or three bytes in 3ms, or you might receive none. At 115200 baud you could receive up to 30-ish, or none. It is unlikely you will receive precisely 12 bytes every 3 ms with asynchronous serial.
Consuming that data happens at a different rate, in your case you insist that is 12 bytes every 3 ms.
Since the two rates do no match you must decouple one from the other.
-
@sanjay13 said in serialport communication through Qthread:
the baud rate here in my code is 921600
So what? This just makes the receive/consume disconnect even greater (approx 92160 bytes/sec received versus 4000 bytes/sec) . Did you consider anything else written in this thread?
-
@sanjay13 said in serialport communication through Qthread:
is that not possible to receive 12 bytes of data for every 3ms in timer or thread without losing data
You can - QSerialPort will not loose any data. There is absolutely no need to use a QThread for such a task here.
-
@Christian-Ehrlicher ok then how to do this every 3ms i need to receive data from serialport (12 byts)data
-
@sanjay13 As already explained you simply have to connect to the readyRead() signal and in the slot read all data with readAll(). You won't have / get a dedicated 3ms read pattern (and this has nothing to do with Qt).
-
@sanjay13 Of course it is possible. Your serial source may be sending far more than that and trying to handle it with a busy-wait polling approach is unnecessary.
Here is a basic (inefficient) example:
#include <QCoreApplication> #include <QObject> #include <QSerialPort> #include <QTimer> #include <QDebug> class SerialHandler: public QObject { Q_OBJECT public: SerialHandler(QObject *p = nullptr) : QObject(p) , m_serial(nullptr) , m_timer(nullptr) { m_serial = new QSerialPort(this); connect(m_serial, &QSerialPort::readyRead, this, &SerialHandler::receiveData); initSerial(); m_timer = new QTimer(this); connect(m_timer, &QTimer::timeout, this, &SerialHandler::sendData); m_timer->start(3); // this will only be approximate } ~SerialHandler() {} signals: void dataForProcessing(const QByteArray &data); private: void initSerial() { // setup serial port } private slots: void receiveData() { m_buffer+= m_serial->readAll(); } void sendData() { if (m_buffer.length() >= 12) { const QByteArray data = m_buffer.left(12); m_buffer = m_buffer.mid(12); emit dataForProcessing(data); } } private: QSerialPort *m_serial; QTimer *m_timer; QByteArray m_buffer; // ^^ used to decouple reception rate from consumption rate. }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); SerialHandler serial; return a.exec(); } #include "main.moc"
The buffer in the example will grow indefinitely if more than ~4000 bytes are received every second.
-
@Christian-Ehrlicher no i have requirement like that im sending 12 bytes of data every 3ms so i have receive it in 3 ms
-
@sanjay13 Then use a real time operating system and not windows or common linux. Your requirement is not achievable with them.