I miss some bytes from Serial Port using Qt
-
Dear Sirs
I have a problem with a GUI that I implemented to get a stream of byte from Arduino Uno using Serial ComI transmit every 2 seconds a packet made by 20 byte from Arduino to the Serial Port of my laptop
I used the SIGNAL/SLOT method in order to get every single byte every time there's a new byte pending on Serial Port.
I would like to read each byte then pass it to another function and process is.
I just got every time 5 bytes, then stop and I'm no more able to process the rest of themCould you suggest me any design strategy in order to avoid this problem
Best Regards
-
Of Course:
void MainWindow::readData()
{
char inchar = 0X00;qint64 read = serial->read(&inchar,1); uchar b = static_cast<uchar>(inchar); processing(b); // This is the methods that will process the UNSIGNED CHAR b //qDebug() << inchar << endl;
}
I post also the code in the constructor where I connect SIGNAL TO SLOT
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
// That's in order to open the function readData() every time I have a new byte incoming from serial
connect(this,SIGNAL(processing(uchar)),downlink,SLOT(tlmStateMachine(uchar)));
// That's is order to connect the methods "processing" of THIS object to the method "tlmStateMachine" of DOWNLINK objectThat's All
-
there is a issue here.
you usedreadyRead()
signal which will emit every time you have data ready on serial port.
then if your for example 5 bytes arrives together then it will emit only one time!
so you just read one single byte and your signal came only one time, so it means you only can see that one byte! you need to usereadAll()
method in order to get all of the available bytes.
if you don't want to usereadAll()
method you should know your serial port device baud rate and set a timer to get data from it every time a data is ready! -
@LostInSpace I would like something in order to process byte by byte
-
-
@Hamed said:
No, don't use thread for reading from serial port.
you have two way to have your bytes :
1- usereadAll()
and store your data inQbyteArray
and read by byte from it
2- use timer with for example 1ms refresh time to get your data byte by byteI recommend option #1.
Option #2 is very wasteful of CPU resources. Also, Windows laptops don't provide good support for 1 ms timers.
-
#2 is wasteful way indeed!
but in low baud rates it will help, but in this case I don't think this is a low baud rate.EDIT : I said for example 1ms, you really don't need it this fast, it depends on the frequency of getting bytes, for example for 20Hz you will have 20bytes every second which means you need a timer with at least 50ms refresh time and of course you read from serial port in asynchronous way then you can't 100% trust this way to read byte by byte, So as @JKSH said you better don't use this way if you don't have a specific reason.