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 Serial port and writing data to a file
Forum Updated to NodeBB v4.3 + New Features

reading from Serial port and writing data to a file

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.1k Views 2 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.
  • MuzabM Offline
    MuzabM Offline
    Muzab
    wrote on last edited by Muzab
    #1

    I find it strange. I can read from the serial port and I can see that data is read in the application output window. There is no error, meaning it is writing to the file, But when I check the file, there is nothing in it. Can someone suggest me why this is happening ? thanks!

    void MainWindow::readSerial()
    {
    QStringList buffer_split = serialBuffer.split(";");

    if(buffer_split.length()<4)
    {
        serialData = Board->readAll();
        serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
        serialData.clear();
    }
    else
    {
        serialBuffer = "";
        qDebug() <<QDateTime::currentDateTime().toString("hh:mm:ss.zzz")<<endl;
        qDebug() <<"Buffer Split ="<<buffer_split<<endl;  
        sensor_Value= (buffer_split[0]).toDouble();;       
        qDebug() <<"Sensor Value= "<<sensor_Value<<endl;
    

    /* Write buffer to the file */
    QFile file("data.txt");
    QString input;
    QTextStream stream (&file);
    input = sensor_Value;
    while(file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
    {
    stream<<input;
    }
    file.close();
    }
    }

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

      Hi,

      The correct flow for QTextStream is rather:

      QFile data("data.txt");
      if (data.open(QFile::WriteOnly | QFile::Truncate)) {
          QTextStream out(&data);
          out << sensor_Value;
      } else {
          qDebug() << "Failed to open file" << data.error();
      }
      

      On a side note, why not just do serialData += Board->readAll(); ? You're doing multiple useless conversions.

      [edit: Fixed code SGaist]

      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
      1
      • MuzabM Muzab

        I find it strange. I can read from the serial port and I can see that data is read in the application output window. There is no error, meaning it is writing to the file, But when I check the file, there is nothing in it. Can someone suggest me why this is happening ? thanks!

        void MainWindow::readSerial()
        {
        QStringList buffer_split = serialBuffer.split(";");

        if(buffer_split.length()<4)
        {
            serialData = Board->readAll();
            serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
            serialData.clear();
        }
        else
        {
            serialBuffer = "";
            qDebug() <<QDateTime::currentDateTime().toString("hh:mm:ss.zzz")<<endl;
            qDebug() <<"Buffer Split ="<<buffer_split<<endl;  
            sensor_Value= (buffer_split[0]).toDouble();;       
            qDebug() <<"Sensor Value= "<<sensor_Value<<endl;
        

        /* Write buffer to the file */
        QFile file("data.txt");
        QString input;
        QTextStream stream (&file);
        input = sensor_Value;
        while(file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
        {
        stream<<input;
        }
        file.close();
        }
        }

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @Muzab said:

        here is no error, meaning it is writing to the file

        To add to @SGaist, how do you know there's no error when no checks against the stream's status flag are made?

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • MuzabM Offline
          MuzabM Offline
          Muzab
          wrote on last edited by
          #4

          The code that I am using to write to a file works if i run it independently and it won't when I use it in my project file.
          Thanks for the corrections.

          qDebug() << "Failed to open file" << data.error(); // seems to be correct instead of data->error()
          

          The problem was with

          QString input;
          

          It was getting reinitialized each time. Sorry ! dudes.. for the blunder! I truly appreciate your kind response.

          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