SerialPort Test



  • Hi guys:
    I had a weird problems with QSerialPort, maybe I use wrong way on this or not.I have a GUI project, so I create another thread to communicate with another slave devices through serialPort. Following is parts of my code.
    @
    //Following is repeatable code in my program
    bool ReadEnd = false;
    QByteArray ReadBuffer;
    int RxTimeoutCount= 0;
    int RxTimeout = 10;
    serialPort->write(Something);
    serialPort->flush();
    while(!ReadEnd)
    {
    serialPort->waitForReadyRead(0);
    if(serialPort->bytesAvailable() != 0)
    {
    ReadBuffer += serialPort->read(serialPort->bytesAvailable());
    RxTimeoutCount = 0;
    }
    else
    {
    QThread::msleep(1);
    RxTimeoutCount ++;
    }
    if(RxTimeoutCount == RxTimeout)
    {
    //Parse data
    ReadEnd = true;
    }
    }@

    Something makes me confused, I found sometimes byte and byte would delay about 0~15 ms or more and this situation will make my program parse wrong data If i gave a short time-out.Then, I also used the oscilloscope to check packet on serial port, I never seen there is a delay inside between byte and byte, next I also used a serial port sniffer to check data in my computer. There is also no delay inside between byte and byte. But why my program sometimes would not detect any bytes come in for a long time?

    P.S Finally, I only use one thread doing action aforementioned, this problem is still existing.



  • Just guessing: but maybe try reducing the speed of your serial port.


  • Lifetime Qt Champion

    Hi,

    Depending on your Qt version, there was a problem with the blocking implementation. The usual way of using QSerialPort is the asynchronous mode.



  • My Qt version is 5.2.0, I will try asynchronous mode today. Wish it could solve this problem.



  • Hi, I had another problem now. When I use the asynchronous method like this
    @
    connect(cSerialPort,SIGNAL(readyRead()),this,SLOT(ReadData()));
    @
    Firstly, nothing happened in my program, there is no data received. Next I add
    a line like following
    @
    serialPort->waitForReadyRead(0);
    @
    My program can receive data. Is this usage appropriate?.


  • Lifetime Qt Champion

    Are you still using a loop anywhere in your code ?



  • Yes, I have a loop in "Read data" Slot function. They are like this
    @
    void worker::ReadData()
    {
    while(!serialPort->atEnd())
    DataReceive += serialPort->read(serialPort->bytesAvailable());
    }
    @
    The result still as same as synchronous mode. Byte and byte sometimes would delay a random time(0~15ms).


  • Lifetime Qt Champion

    Don't do it like that. Just call readAll and you'll get all what is currently available



  • Hi, I modified it and tested again. This problem still was existing.... By the way, I used another language(C#) to implement function like I wrote by Qt. I never found this problem occurred. Is it a potential bug in using QSerialPort?


  • Lifetime Qt Champion

    Can you show your complete test code ?



  • Sure, but it's a little big. Do you have e-mail? I can mail the project to you.


Log in to reply
 

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