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. How can I store data Received via serial communication to text file?
Forum Updated to NodeBB v4.3 + New Features

How can I store data Received via serial communication to text file?

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 3.3k 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
    segtteee
    wrote on last edited by
    #1

    I am doing serial communication and receiving data. i want if i Press any button , save the received data in txt format. How can I implement this?
    (The format of the data is a integer)

    A 1 Reply Last reply
    -1
    • S segtteee

      I am doing serial communication and receiving data. i want if i Press any button , save the received data in txt format. How can I implement this?
      (The format of the data is a integer)

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @segtteee

      bool writeDataToFile(const QByteArray &data)
      {
         QFile f("yourfile.txt");
         if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
            return false; // failed to open
      
         QTextStream out(&f);
         out << data << endl;
      
         return true;
      }
      

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      S J.HilkJ 2 Replies Last reply
      4
      • A ambershark

        @segtteee

        bool writeDataToFile(const QByteArray &data)
        {
           QFile f("yourfile.txt");
           if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
              return false; // failed to open
        
           QTextStream out(&f);
           out << data << endl;
        
           return true;
        }
        
        S Offline
        S Offline
        segtteee
        wrote on last edited by
        #3

        @ambershark
        Should I call this with a signal and a slot?

        A 1 Reply Last reply
        0
        • S segtteee

          @ambershark
          Should I call this with a signal and a slot?

          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @segtteee Sure you can but you don't have to. I don't really know the design of your app so I can't say if it would be appropriate to call it as a slot or not.

          If you decide to you need to change it's type to void and change the returns.

          If you want to share your code (the part that reads from the serial device) I can help you more.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          S 1 Reply Last reply
          0
          • A ambershark

            @segtteee

            bool writeDataToFile(const QByteArray &data)
            {
               QFile f("yourfile.txt");
               if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
                  return false; // failed to open
            
               QTextStream out(&f);
               out << data << endl;
            
               return true;
            }
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @ambershark come on, returning without closing the file is just bad manners ;-)


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            A jsulmJ 2 Replies Last reply
            0
            • A ambershark

              @segtteee Sure you can but you don't have to. I don't really know the design of your app so I can't say if it would be appropriate to call it as a slot or not.

              If you decide to you need to change it's type to void and change the returns.

              If you want to share your code (the part that reads from the serial device) I can help you more.

              S Offline
              S Offline
              segtteee
              wrote on last edited by
              #6

              @ambershark
              it is here

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
                  mSerialport = new QSerialPort (this);
                  connect(mSerialport,SIGNAL(readyRead()),this,SLOT(read_device_data()));
              }
              ////
              void MainWindow::on_pushButton_connect_clicked()
              {
              if (mSerialport->open(QIODevice::ReadWrite))
                  {
                      QMessageBox::information(this,tr("connect"),"Serial communication start");
                  }
                  else
                  {
                      QMessageBox::critical(this,tr("error"),mSerialport->errorString());
                  }
              }
              ///
              void MainWindow::read_device_data()
              {
                  QByteArray device_data(mSerialport->readAll());
                  ui->data_show->setText(device_data);
              
              
                  double number = QString(device_data).remove('\n').toDouble() * 1000;
                  qDebug() << number;
                  ui->dial->setValue(number);
              }
              
              A 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @ambershark come on, returning without closing the file is just bad manners ;-)

                A Offline
                A Offline
                ambershark
                wrote on last edited by
                #7

                @J.Hilk Lol yea I was being lazy.. In real code I never do that. To be fair though it will close automatically when QFile goes out of scope. :)

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                1 Reply Last reply
                2
                • S segtteee

                  @ambershark
                  it is here

                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                      mSerialport = new QSerialPort (this);
                      connect(mSerialport,SIGNAL(readyRead()),this,SLOT(read_device_data()));
                  }
                  ////
                  void MainWindow::on_pushButton_connect_clicked()
                  {
                  if (mSerialport->open(QIODevice::ReadWrite))
                      {
                          QMessageBox::information(this,tr("connect"),"Serial communication start");
                      }
                      else
                      {
                          QMessageBox::critical(this,tr("error"),mSerialport->errorString());
                      }
                  }
                  ///
                  void MainWindow::read_device_data()
                  {
                      QByteArray device_data(mSerialport->readAll());
                      ui->data_show->setText(device_data);
                  
                  
                      double number = QString(device_data).remove('\n').toDouble() * 1000;
                      qDebug() << number;
                      ui->dial->setValue(number);
                  }
                  
                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

                  Ok so here is how I would do it to fit with your code:

                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                      mSerialport = new QSerialPort (this);
                      connect(mSerialport,SIGNAL(readyRead()),this,SLOT(read_device_data()));
                  }
                  ////
                  void MainWindow::on_pushButton_connect_clicked()
                  {
                  if (mSerialport->open(QIODevice::ReadWrite))
                      {
                          QMessageBox::information(this,tr("connect"),"Serial communication start");
                      }
                      else
                      {
                          QMessageBox::critical(this,tr("error"),mSerialport->errorString());
                      }
                  }
                  ///
                  void MainWindow::read_device_data()
                  {
                      QByteArray device_data(mSerialport->readAll());
                      ui->data_show->setText(device_data);
                      if (!writeDataToFile(device_data))
                          qDebug() << "Failed to open file";
                  
                  
                      double number = QString(device_data).remove('\n').toDouble() * 1000;
                      qDebug() << number;
                      ui->dial->setValue(number);
                  }
                  
                  bool MainWindow::writeDataToFile(const QByteArray &data)
                  {
                     QFile f("yourfile.txt");
                     if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
                        return false; // failed to open
                  
                     QTextStream out(&f);
                     out << data << endl;
                  
                     f.close() // for you J.Hilk ;)
                  
                     return true;
                  }
                  

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  S 1 Reply Last reply
                  0
                  • A ambershark

                    Ok so here is how I would do it to fit with your code:

                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                        mSerialport = new QSerialPort (this);
                        connect(mSerialport,SIGNAL(readyRead()),this,SLOT(read_device_data()));
                    }
                    ////
                    void MainWindow::on_pushButton_connect_clicked()
                    {
                    if (mSerialport->open(QIODevice::ReadWrite))
                        {
                            QMessageBox::information(this,tr("connect"),"Serial communication start");
                        }
                        else
                        {
                            QMessageBox::critical(this,tr("error"),mSerialport->errorString());
                        }
                    }
                    ///
                    void MainWindow::read_device_data()
                    {
                        QByteArray device_data(mSerialport->readAll());
                        ui->data_show->setText(device_data);
                        if (!writeDataToFile(device_data))
                            qDebug() << "Failed to open file";
                    
                    
                        double number = QString(device_data).remove('\n').toDouble() * 1000;
                        qDebug() << number;
                        ui->dial->setValue(number);
                    }
                    
                    bool MainWindow::writeDataToFile(const QByteArray &data)
                    {
                       QFile f("yourfile.txt");
                       if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
                          return false; // failed to open
                    
                       QTextStream out(&f);
                       out << data << endl;
                    
                       f.close() // for you J.Hilk ;)
                    
                       return true;
                    }
                    
                    S Offline
                    S Offline
                    segtteee
                    wrote on last edited by
                    #9

                    @ambershark

                    Where should I declare "writeDataTofile"? header's public?

                    jsulmJ 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @ambershark come on, returning without closing the file is just bad manners ;-)

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @J.Hilk QFile automatically closes the file when it is destroyed :-)

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • S segtteee

                        @ambershark

                        Where should I declare "writeDataTofile"? header's public?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @segtteee said in How can I store data Received via serial communication to text file?:

                        header's public

                        Why public? It is only needed inside MainWindow, so it should not be public.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        S 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @segtteee said in How can I store data Received via serial communication to text file?:

                          header's public

                          Why public? It is only needed inside MainWindow, so it should not be public.

                          S Offline
                          S Offline
                          segtteee
                          wrote on last edited by
                          #12

                          @jsulm said in How can I store data Received via serial communication to text file?:

                          @segtteee said in How can I store data Received via serial communication to text file?:

                          header's public

                          but "‘writeDataToFile’ was not declared in this scope
                          if (!writeDataToFile(device_data))" is displayed.
                          declaring "writeDataTofile" is need

                          jsulmJ 1 Reply Last reply
                          0
                          • S segtteee

                            @jsulm said in How can I store data Received via serial communication to text file?:

                            @segtteee said in How can I store data Received via serial communication to text file?:

                            header's public

                            but "‘writeDataToFile’ was not declared in this scope
                            if (!writeDataToFile(device_data))" is displayed.
                            declaring "writeDataTofile" is need

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @segtteee Then declare it in the private section of the class, what is the problem?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            S 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @segtteee Then declare it in the private section of the class, what is the problem?

                              S Offline
                              S Offline
                              segtteee
                              wrote on last edited by
                              #14

                              @jsulm
                              but ambershark informed them that I wrote them separately. Is it not the declaration that starts with bool? Should I combine these two?

                              jsulmJ 1 Reply Last reply
                              0
                              • S segtteee

                                @jsulm
                                but ambershark informed them that I wrote them separately. Is it not the declaration that starts with bool? Should I combine these two?

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by jsulm
                                #15

                                @segtteee You should learn C++.

                                // Header file
                                class MainWindow
                                {
                                private:
                                    // This is declaration
                                    bool writeDataToFile(const QByteArray &data)
                                };
                                
                                // C++ file
                                // This is definition
                                bool MainWindow::writeDataToFile(const QByteArray &data)
                                {
                                    ...
                                }
                                

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                1

                                • Login

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