Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SerialPort Test
Forum Updated to NodeBB v4.3 + New Features

SerialPort Test

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ivan1120
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Ivan1120
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Ivan1120
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Are you still using a loop anywhere in your code ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Ivan1120
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    Ivan1120
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Can you show your complete test code ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        Ivan1120
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved