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



  • 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);
    }
    

  • Moderators

    @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



  • @jsulm
    Thanks. I solved the problem. :D


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.