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 from SerialPort and write to txt file.
Forum Updated to NodeBB v4.3 + New Features

Reading from SerialPort and write to txt file.

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 462 Views
  • 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.
  • R Offline
    R Offline
    Rika
    wrote on last edited by
    #1

    I want to read data from COM port, then write to txt file and display them. But the data I received is incomplete. Here my code:

    bool finished(false);
    QByteArray datas = serialPort->readAll();
    if(datas.size()>0)
    {
    if(file.isOpen())
    {
    QTextStream in(&file);
    in<<datas;
    file.close();
    finished=true;
    }
    }
    else {
    QMessageBox::warning(this, "Cảnh báo"+ file.errorString());
    return;
    }
    if(finished){
    doc();
    }

    doc() is a function that displays file data.

    jsulmJ 1 Reply Last reply
    0
    • R Rika

      I want to read data from COM port, then write to txt file and display them. But the data I received is incomplete. Here my code:

      bool finished(false);
      QByteArray datas = serialPort->readAll();
      if(datas.size()>0)
      {
      if(file.isOpen())
      {
      QTextStream in(&file);
      in<<datas;
      file.close();
      finished=true;
      }
      }
      else {
      QMessageBox::warning(this, "Cảnh báo"+ file.errorString());
      return;
      }
      if(finished){
      doc();
      }

      doc() is a function that displays file data.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Rika readAll() will give you what currently is available (what was received so far). This code will not work as it is now. You either use a loop with waitForReadyRead(), read then and add the data to a buffer. Or you use signals slots to read when there is something to read. Also, how do you know that you got all the data? Do you have some protocol?
      You should take a look at the examples: https://doc.qt.io/qt-5/qtserialport-examples.html

      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