PDA

View Full Version : QtSerialPort : missing received datas / Console application



Awawa
31st May 2016, 16:58
Hi everyone,

I hope you are fine.

I started this thread because I am facing with a QtSerialPort problem. I have a computer with two serial ports (in 2016!!!) that I have connected together with a crossed subd9 cable. Now I am trying to send informations from one to other.

The problem is that they are always missing datas (at the end of the vector). This is confirmed with an external program (Device monitoring studio) wich is a serial sniffer (hahaha).

I wrote a small program to illustrate this problem :




#include <QCoreApplication>
#include <QtSerialPort/QtSerialPort>
#include<QDebug>
#include "windows.h"
#include <iostream>

using namespace std;



int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);


int MessageLength=120;
char *MessageToSend= new char[MessageLength];

int MessageLengthToRead=MessageLength;
char *dataToReceive= new char [MessageLengthToRead];


//************************************************** ************************************************** *************
//Here I create two serial ports : the first one to emit and the second one to receive
QSerialPort *SerialEmitter = new QSerialPort;
QSerialPort *SerialReceiver = new QSerialPort;

//************************************************** ************************************************** *************
//Here we initialize serial ports


SerialEmitter->setPortName("COM9");
SerialEmitter->open(QSerialPort::OpenModeFlag::ReadWrite);
SerialEmitter->setBaudRate(QSerialPort::Baud115200);
SerialEmitter->setDataBits(QSerialPort::Data8);
SerialEmitter->setParity(QSerialPort::NoParity);
SerialEmitter->setStopBits(QSerialPort::OneStop);

//************************************************** ************************************************** *************
//Here I initialize the message i am going to send

SerialReceiver->setPortName("COM12");
SerialReceiver->open(QSerialPort::OpenModeFlag::ReadWrite);
SerialReceiver->setBaudRate(QSerialPort::Baud115200);
SerialReceiver->setDataBits(QSerialPort::Data8);
SerialReceiver->setParity(QSerialPort::NoParity);
SerialReceiver->setStopBits(QSerialPort::OneStop);
SerialReceiver->setFlowControl(QSerialPort::NoFlowControl);
SerialReceiver->setReadBufferSize(MessageLength);

SerialReceiver->clear();
SerialEmitter->clear();

//************************************************** ************************************************** *************

//Initialisation of the data to send
for (int i =0; i<MessageLength; i++)
{
MessageToSend[i]=(char)(i);
}

//Displaying Datas
for(int i=0;i<MessageLength;i++)
{
cout<<(int)((unsigned char)MessageToSend[i])<<" ";

}
cout<<endl;

cout<< "************************************************** *************"<<endl;
//Here I write datas
cout<< "The value retrun of Serial emitter wtrite is "<< SerialEmitter->write(MessageToSend,MessageLength)<<endl;

//Here I wait loooong time!
SerialEmitter->waitForBytesWritten(-1);
SerialReceiver->waitForReadyRead(1000);


//Here I read datas
cout<< "Number of readed bytes "<<SerialReceiver->read(dataToReceive, MessageLength)<<endl;


for(int i=0;i<(MessageLengthToRead);i++)
{
cout<<(int)((unsigned char)dataToReceive[i])<< " ";

}
cout<<endl;

SerialEmitter->close();
SerialReceiver->close();

return a.exec();
}



Here is what the program write in the console : here normally both vector stop @119 but the program read only 65 bytes (even if I wait more time with the waitForReadyRead commad).



0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
************************************************** *************
The value retrun of Serial emitter wtrite is 120
Number of readed bytes 65
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 0 51 0 50 0 92 0 119 0 105 0 110 0 109 0 109 0 46 0 100 0 108 0 108 0 0 0 192 0 3 7 116 0 95 0 53 0 95 0 54 0 95 0 48 0 95 0 77 0 105 0 110 0 71 0


If any of you has an idea to fix this, I will really appreciate because I am really freezing.

QSerialPort

anda_skoa
31st May 2016, 17:09
Looks like you only wait for the first readyRead, only get part of the data, but instead iof waiting for more you continue with what you have.

Cheers,
_

Lesiok
31st May 2016, 17:10
From QIODevice::waitForReadyRead doc : Blocks until new data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed. -
which means don't wait if any data (even one byte) are ready.

Awawa
1st June 2016, 11:20
Hi Guys,

Thank you very much for your answers. I rode the QIODevice::waitForReadyRead definition too fast and I misunderstood this point.

For guys who will read this thread and who will be faced with the same problem, i can suggest this solution (which is a blocking one). This solution is exposed in qt example.




QByteArray dataToReceiveQBA;

char* dataToReceive= new char;



cout<< "the device is open Y/N: "<< SerialEmitter->isOpen()<<endl;
cout<< "The value retrun of this function is "<< SerialEmitter->write(TheMessage,TailleMessage*sizeof(char))<<endl;
SerialEmitter->waitForBytesWritten(200);

dataToReceiveQBA=SerialEmitter->readAll();

while (SerialReceiver->waitForReadyRead(250))
{
dataToReceiveQBA.append(SerialEmitter->readAll());
}


dataToReceive= dataToReceiveQBA.data();