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. Reading continuous data from Serial Port
Forum Updated to NodeBB v4.3 + New Features

Reading continuous data from Serial Port

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.4k 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.
  • S Offline
    S Offline
    StayCool
    wrote on last edited by StayCool
    #1

    Hi friends,
    I am new for QT C++ and am trying to write a program, which could continuous read data from Serial Port. My Program works sometimes smoothly but sometimes it read wrong data. I have run debug and here is what I got:

    "61\x00\x00"
    "61\x00\x00"
    "61\x00\x00"
    "62\x00\x00"
    "62\x00\x00"
    "62\x00\x00"
    "62\x00\x00"
    "63\x00\x00"
    "63\x00\x00"
    "5"
    "3\x00\x00"
    "5"
    "2\x00\x00"
    "5"
    "2\x00\x00"
    
    "54\x00\x00"
    "54\x00\x00"
    "54\x00\x00"
    "55\x00\x00"
    "54\x00\x00"
    "54\x00\x00"
    "55\x00\x00"
    "55\x00\x00"
    "5"
    "1\x00\x00"
    "51"
    "\x00\x00"
    "5"
    "2\x00\x00"
    "5"
    "1\x00\x00"
    "5"
    "1\x00\x00"
    

    Someway QTSerialPort didn't read all data. And here is my code, where data should be read and display.

       if(serial->isOpen() && serial->isReadable()){
           QByteArray receivedData;
           receivedData = serial->readAll();
           qDebug()<<receivedData;
           ui->lcdDistance->display(receivedData.toInt());
        }else{
           QMessageBox::critical(this, tr("Error"), tr("Doesn't receive data"));
        }
    

    I think the problem came from QT, not the microcontroller because I also run debug with my microcontroller and it worked very well.

    Please help me to solve this problem. Thanks in advance.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QSerialPort is async - connect to the readyRead() signal and read the data data. What you do here can't work since readAll() is non-blocking (and that's fine)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • S Offline
        S Offline
        StayCool
        wrote on last edited by
        #3

        I used readyRead() for the very first time but it still happened

         connect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()));
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Serial port data are not guaranteed to arrive as full frames.

          You should cumulate the data you get and then check if a full frame can be found in that buffer, process it and then go on.

          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
          3

          • Login

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