serialport communication through Qthread
-
@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).
-
@ChrisW67 is that not possible to receive 12 bytes of data for every 3ms in timer or thread without losing data
@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.
-
@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).
@Christian-Ehrlicher no i have requirement like that im sending 12 bytes of data every 3ms so i have receive it in 3 ms
-
@Christian-Ehrlicher no i have requirement like that im sending 12 bytes of data every 3ms so i have receive it in 3 ms
-
@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.
-
@sanjay13 Then use a real time operating system and not windows or common linux. Your requirement is not achievable with them.
@Christian-Ehrlicher im doing this in windows
@ChrisW67 the output for the code you have provided
Time: 14043 ms
count: 1
12 sendingg___size
Time: 14053 ms
count: 2
12 receive___size
0 receive___size
0 receive___size
0 receive___size
0 receive___size
0 receive___size
0 receive___sizelike this after some time it is 12 receive then it becomes 0
-
@Christian-Ehrlicher im doing this in windows
@ChrisW67 the output for the code you have provided
Time: 14043 ms
count: 1
12 sendingg___size
Time: 14053 ms
count: 2
12 receive___size
0 receive___size
0 receive___size
0 receive___size
0 receive___size
0 receive___size
0 receive___sizelike this after some time it is 12 receive then it becomes 0
-
@ChrisW67 i have used the connect part and cheched with qdebug
Time: 8164 ms
count: 1
12 receive___size
12 sendingg___size
Time: 8177 ms
count: 2
12 receive___size
12 sendingg___size
Time: 8189 ms
count: 3
12 receive___size
12 sendingg___size
Time: 8204 ms
count: 4
12 receive___size
12 sendingg___size
this is the output for 15,ms it is working perfectly but i need this for 3ms -
@ChrisW67 i have used the connect part and cheched with qdebug
Time: 8164 ms
count: 1
12 receive___size
12 sendingg___size
Time: 8177 ms
count: 2
12 receive___size
12 sendingg___size
Time: 8189 ms
count: 3
12 receive___size
12 sendingg___size
Time: 8204 ms
count: 4
12 receive___size
12 sendingg___size
this is the output for 15,ms it is working perfectly but i need this for 3msSometimes I wonder about the ignorancy of people here. As we already said many times - your requirement is not achievable - neither with Qt nor with windows. There are so many buffers invovlved and the normal time slices / multitasking is doing the rest. Use a real-time operating system if you have this requiremnt. You will never be able to to something with windows and it's also not even needed in any case (except satisfying the paper with the requirement).
-
Sometimes I wonder about the ignorancy of people here. As we already said many times - your requirement is not achievable - neither with Qt nor with windows. There are so many buffers invovlved and the normal time slices / multitasking is doing the rest. Use a real-time operating system if you have this requiremnt. You will never be able to to something with windows and it's also not even needed in any case (except satisfying the paper with the requirement).
@Christian-Ehrlicher ok thankyou for your reply.
-
S sanjay13 has marked this topic as solved on