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. I want to save the received data in txt format
Qt 6.11 is out! See what's new in the release blog

I want to save the received data in txt format

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.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.
  • S Offline
    S Offline
    segtteee
    wrote on last edited by
    #1

    I am doing serial communication. I want to set up the port, press on the monitor and connect to receive data from the device (A repeated numeric value)
    I want to implement the ability to save the data as a txt file. How do I do that?
    below is my code. thank you

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    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()));
    
        connect(ui->radioButton_on,SIGNAL(clicked(bool)),this,SLOT(on_radioButton_on_clicked()));
    
        connect(ui->radioButton_off,SIGNAL(clicked(bool)),this,SLOT(on_radioButton_off_clicked()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_connect_clicked()
    {
        mSerialport->setPortName(ui->comboBox_port->currentText());
    
        if (ui->comboBox_baudrate->currentText() == "9600 bps")
            mSerialport->setBaudRate(QSerialPort::Baud9600);
        else if (ui->comboBox_baudrate->currentText() == "19200 bps")
            mSerialport->setBaudRate(QSerialPort::Baud19200);
        else if (ui->comboBox_baudrate->currentText() == "38400 bps")
            mSerialport->setBaudRate(QSerialPort::Baud38400);
        else if (ui->comboBox_baudrate->currentText() == "57600 bps")
            mSerialport->setBaudRate(QSerialPort::Baud57600);
        else if (ui->comboBox_baudrate->currentText() == "115200 bps")
            mSerialport->setBaudRate(QSerialPort::Baud115200);
    
        mSerialport->setDataBits(QSerialPort::Data8);
        mSerialport->setStopBits(QSerialPort::OneStop);
    
        if (mSerialport->open(QIODevice::ReadWrite))
        {
            QMessageBox::information(this,tr("connect"),"Serial communication start");
        }
        else
        {
            QMessageBox::critical(this,tr("error"),mSerialport->errorString());
        }
    }
    
    void MainWindow::on_pushButton_disconnect_clicked()
    {
        QMessageBox::information(this,tr("disconnect"),"Serial communication end");
        mSerialport->close();
    }
    
    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);
    }
    
    void MainWindow::on_radioButton_on_clicked()
    {
        qDebug() << "written->" << mSerialport->write("mon 1\n");
    }
    
    void MainWindow::on_radioButton_off_clicked()
    {
        qDebug() << "written->" << mSerialport->write("mon 0\n");
    }
    
    
    1 Reply Last reply
    0
    • yuvaramY Offline
      yuvaramY Offline
      yuvaram
      wrote on last edited by
      #2

      HI @segtteee
      push data in QByteArray.
      QByteArray arry;

      QFile file("RxData.txt");
      if(file.open(QIODevice::WriteOnly)){
      file.write(arry);
      file.close();
      }

      Yuvaram Aligeti
      Embedded Qt Developer
      : )

      S 1 Reply Last reply
      2
      • yuvaramY yuvaram

        HI @segtteee
        push data in QByteArray.
        QByteArray arry;

        QFile file("RxData.txt");
        if(file.open(QIODevice::WriteOnly)){
        file.write(arry);
        file.close();
        }

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

        @yuvaram
        thank you for reply
        you mean add the code this?

        void MainWindow::save()
        {
            QByteArray arry;
            QFile file("datasave.txt");
            if(file.open(QIODevice::WriteOnly)){
                file.write(arry);
                file.close();
            }
        }
        
        
        yuvaramY 1 Reply Last reply
        0
        • S segtteee

          @yuvaram
          thank you for reply
          you mean add the code this?

          void MainWindow::save()
          {
              QByteArray arry;
              QFile file("datasave.txt");
              if(file.open(QIODevice::WriteOnly)){
                  file.write(arry);
                  file.close();
              }
          }
          
          
          yuvaramY Offline
          yuvaramY Offline
          yuvaram
          wrote on last edited by
          #4

          @segtteee
          Yes. But array must be pushed with data from serial communication

          Yuvaram Aligeti
          Embedded Qt Developer
          : )

          S 1 Reply Last reply
          1
          • yuvaramY yuvaram

            @segtteee
            Yes. But array must be pushed with data from serial communication

            S Offline
            S Offline
            segtteee
            wrote on last edited by
            #5
            This post is deleted!
            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