How can I see and storage the data from device in QT?
-
this is my ui
what i want
I am in serial communication qt with my device. I make a ui and I can connect my device using ui. I want get a data log from device and display in qt using QTimer.how can I do it ? and also want save data to csv file.
please help me and let me know how can I solve the problem
below is my code.
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtSerialPort/QSerialPort> #include <QSerialPortInfo> #include <QMessageBox> #include <QObject> #include <QIODevice> #include <QDebug> #include <QPlainTextEdit> #include <QDateTime> QSerialPort serial; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), mSerialport{new QSerialPort} { ui->setupUi(this); connect(this->mSerialport,SIGNAL(readyRead()), this,SLOT(readSerialData())); connect(ui->pushButton_send, &QPushButton::clicked, [=](){ sendMsg(ui->command->toPlainText()); }); } MainWindow::~MainWindow() { delete mSerialport; delete ui; } void MainWindow::on_pushButton_connect_clicked() { mSerialport->setPortName("/dev/ttyUSB0"); mSerialport->setBaudRate(QSerialPort::Baud9600); mSerialport->setDataBits(QSerialPort::Data8); mSerialport->setParity(QSerialPort::NoParity); mSerialport->setStopBits(QSerialPort::OneStop); mSerialport->setFlowControl(QSerialPort::NoFlowControl); if (mSerialport->open(QIODevice::ReadWrite)) { QMessageBox::information(this,tr("connect"), "serialcommunication start"); } else { QMessageBox::information(this,tr("fail"), mSerialport->errorString()); } } void MainWindow::on_pushButton_disconnect_clicked() { QMessageBox::information(this, tr("disconnect"), "serial communication end"); mSerialport->close(); } void MainWindow::sendMsg(const QString &msg) { QString str = msg; str.append("\n"); this->mSerialport->write(str.toLatin1()); ui->comLog->insertPlainText(QDateTime::currentDateTime(). toString("yyyy-MM-dd hh:mm:ss") + " [send] " + msg + "\n"); } void MainWindow::recvMsg(){ QByteArray msg = this->mSerialport->readAll(); ui->comLog->insertPlainText(QDateTime::currentDateTime(). toString("yyyy-MM-dd hh:mm:ss") + " [recieve] " + msg.toHex(). data() + "\n"); }
-
This requires good programming practise.
- Whenever you click on the button, you need to open the device selected. Right now u have hard coded with mSerialport->setPortName("/dev/ttyUSB0");
- use readyRead() signal to check the arrival of data.
- use readAll() method to read all the data from device.
- Display the data in QTextEdit UI control
-
@dheerendra
Thank you for your reply. Does that mean you have to make a full correction my code? otherwise just change
" mSerialport->setPortName("/dev/ttyUSB0");" ?? -
only mSerialport->setPortName("/dev/ttyUSB0") this may be sufficient.
-
@dheerendra
Can not I solve the problem without changing the "mSerialport->setPortName("/dev/ttyUSB0")" ? I do not think I can change it. -
@segtteee Then just go to the documentation: http://doc.qt.io/qt-5/qcombobox.html#currentText-prop
ChangemSerialport->setPortName("/dev/ttyUSB0");
to
mSerialport->setPortName(ui->comboBox->currentText()); // Change the name of the combobox if it's called differently in your code
Really easy, isn't it? :-)
-
@jsulm
I modified the port name as you told me. I also changed baudrate and databite equally, but here I get an error.
"" no matching function for call to ‘QSerialPort::setBaudRate(QString)’
mSerialport->setBaudRate(ui->comboBox_baudrate->currentText()); ""
Can I change only the port name? -
@segtteee See the documentation: http://doc.qt.io/qt-5/qserialport.html#baudRate-prop
setBaudRate() takes an integer not a string.
See again documentation to find out how to convert a string to an integer: http://doc.qt.io/qt-5/qstring.html#toIntmSerialport->setBaudRate(ui->comboBox_baudrate->currentText().toInt());
-
@segtteee said in How can I see and storage the data from device in QT?:
databits is a number, why not toint?
I don't understand. If it is a number then convert it to a number like baud rate.
"And paritybits is a character, but it should not be used" - don't know what you mean. If it should not be used then don't use it.
-
@segtteee Could you please show real code and the error message?
DataBits is an enum, so you can do something like:DataBits dataBits; if (ui->dataBits.currentText() == "Data5") dataBits = DataBits::Data5; else if ...