I want to save the received data in txt format



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


  • HI @segtteee
    push data in QByteArray.
    QByteArray arry;

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



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


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



  • This post is deleted!

Log in to reply
 

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