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 Com

    I 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 them

    Could you suggest me any design strategy in order to avoid this problem

    Best Regards



  • Hi
    Can you post the part of your code that you read bytes from serial port?



  • 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 object

    That's All



  • there is a issue here.
    you used readyRead() 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 use readAll() method in order to get all of the available bytes.
    if you don't want to use readAll() 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!



  • @Hamed Ok I got it
    In this way I'm not able to process byte-by-byte but I ll get a QByteArray and I have to process it using a Thread

    Is it correct?



  • @LostInSpace I would like something in order to process byte by byte



  • No, don't use thread for reading from serial port.
    you have two way to have your bytes :
    1- use readAll() and store your data in QbyteArray and read by byte from it
    2- use timer with for example 1ms refresh time to get your data byte by byte


  • Moderators

    @Hamed said:

    No, don't use thread for reading from serial port.
    you have two way to have your bytes :
    1- use readAll() and store your data in QbyteArray and read by byte from it
    2- use timer with for example 1ms refresh time to get your data byte by byte

    I 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.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.