Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to Save Continued Data received by device during serial communication?
Qt 6.12 LTS is out! See what's new in the release blog

How to Save Continued Data received by device during serial communication?

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

    I am doing serial communication with the equipment. If i set up the port, press on the monitor and connect button , connect pc and device. I have been receiving continuous data from the equipment and tried to save it as txt (numeric) implementation. However, when I run the code I wrote, only the last value is written in the txt file. I want to record all of the ongoing data. What should I do?

    below is total my code.

    #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);
    
        QFile file("/home/mycdc/datasave.txt");
        if(file.open(QIODevice::WriteOnly)){
            file.write(device_data);
            
            file.close();
        }
    
        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");
    }
    
    

    Here is where I implemented the data storage

    void MainWindow::read_device_data()
    {
        QByteArray device_data(mSerialport->readAll());
        ui->data_show->setText(device_data);
    
        QFile file("/home/mycdc/datasave.txt");
        if(file.open(QIODevice::WriteOnly)){
            file.write(device_data);
            
            file.close();
        }
    
        double number = QString(device_data).remove('\n').toDouble() * 1000;
        qDebug() << number;
        ui->dial->setValue(number);
    }
    
    jsulmJ 1 Reply Last reply
    0
    • S segtteee

      I am doing serial communication with the equipment. If i set up the port, press on the monitor and connect button , connect pc and device. I have been receiving continuous data from the equipment and tried to save it as txt (numeric) implementation. However, when I run the code I wrote, only the last value is written in the txt file. I want to record all of the ongoing data. What should I do?

      below is total my code.

      #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);
      
          QFile file("/home/mycdc/datasave.txt");
          if(file.open(QIODevice::WriteOnly)){
              file.write(device_data);
              
              file.close();
          }
      
          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");
      }
      
      

      Here is where I implemented the data storage

      void MainWindow::read_device_data()
      {
          QByteArray device_data(mSerialport->readAll());
          ui->data_show->setText(device_data);
      
          QFile file("/home/mycdc/datasave.txt");
          if(file.open(QIODevice::WriteOnly)){
              file.write(device_data);
              
              file.close();
          }
      
          double number = QString(device_data).remove('\n').toDouble() * 1000;
          qDebug() << number;
          ui->dial->setValue(number);
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @segtteee That' because you overwrite the file each time you open it. You need to use

      if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
      

      QIODevice::Append - to append to the file (default is to overwrite).
      QIODevice::Text - you said you want to write text file.
      Explained here: http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum

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

      S 1 Reply Last reply
      3
      • jsulmJ jsulm

        @segtteee That' because you overwrite the file each time you open it. You need to use

        if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
        

        QIODevice::Append - to append to the file (default is to overwrite).
        QIODevice::Text - you said you want to write text file.
        Explained here: http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum

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

        @jsulm
        Thanks. I solved the problem. :D

        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
        • Unsolved