Unsolved How can I store data Received via serial communication to text file?
-
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) -
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; }
-
@ambershark
Should I call this with a signal and a slot? -
@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.
-
@ambershark come on, returning without closing the file is just bad manners ;-)
-
@ambershark
it is hereMainWindow::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); }
-
@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. :)
-
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; }
-
Where should I declare "writeDataTofile"? header's public?
-
@J.Hilk QFile automatically closes the file when it is destroyed :-)
-
@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.
-
@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 -
@segtteee Then declare it in the private section of the class, what is the problem?
-
@jsulm
but ambershark informed them that I wrote them separately. Is it not the declaration that starts with bool? Should I combine these two? -
@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) { ... }