How can i show data that device sent in QT?
-
How can i show data that device sent in QT?
This is ui I made in QT. I want see data in data log window.
It is example that 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 i 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) { ui->setupUi(this); mSerialport = new QSerialPort(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::critical(this, tr("Error"), 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"); }
-
@segtteee said in How can i show data that device sent in QT?:
connect(this->mSerialport,SIGNAL(readyRead()),
this,SLOT(readSerialData()));In this connect statement you tell Qt that readSerialData() function will read data from your serial port (when you get a reply). You have not shown that method in your code snippet. Have you implemented it?
In general (skipping error handling, packet reconstruction etc.), it should be enough to do something like:
void readSerialData() { QByteArray reply(mSerialport->readAll()); // put reply to your UI. I don't know your UI component names so I won't write it down }
Or do you mean something else?
-
@sierdzio
Thank you for your reply.
I add the code
void MainWindow::readSerialData()
{
QByteArray datalog(mSerialport->readAll());
}
at the end and excute, but I did not see anything in datalog,QT.
Is there any way I can get the data as in the third picture? -
@segtteee we still don't know in what form your data arrives.
Here's a general, quick & dirty way to convert your raw data - byte by byte - into a nicely formated user readable hex -string, that you than can send to one of your ui-Elements.
void MainWindow::readSerialData() { QString s; QByteArray datalog(mSerialport->readAll()); for(int i = 0; i < datalog.size(); i++) s.append(QString::number(static_cast<unsigned char>(datalog[i]),16).toUpper().rightJustified(2,'0')+ " "); }
-
@segtteee said in How can i show data that device sent in QT?:
at the end and excute, but I did not see anything in datalog,QT.
Is there any way I can get the data as in the third picture?You are not setting it in the UI anywhere. What you written only took the serial data and saved it into a (local) datalog variable. Which is then discarded when the function gets out of scope. You need to set the contents (or formatted contents like @J-Hilk has shown) on some UI element you have - QLabel, QTextEdit etc. We don't know what are the names of your UI components so we can't help you write that code, at least no more than:
QByteArray datalog(mSerialport->readAll()); ui->someLabelOrTextField->setText(datalog);
-
@segtteee said in How can i show data that device sent in QT?:
@sierdzio
sorry.. my ui is mainwindow.ui and port is mSerialport .
textedit object's name is datalog , I would like to see the data hereWell, you have all the information to make it work, then.
QByteArray datalog(mSerialport->readAll()); ui->datalog->setText(datalog);
-
@sierdzio
void readSerialData() {
QByteArray reply(mSerialport->readAll());
// put reply to your UI. I don't know your UI component names so I won't write it down
}
What you say here "reply" is what I think. I think it is object's name like combo box , push-button or text edit.
So I thought the reply was datalog , which object I want to view the data -
@segtteee said in How can i show data that device sent in QT?:
@sierdzio
void readSerialData() {
QByteArray reply(mSerialport->readAll());
// put reply to your UI. I don't know your UI component names so I won't write it down
}
What you say here "reply" is what I think. I think it is object's name like combo box , push-button or text edit.
So I thought the reply was datalog , which object I want to view the dataSorry, but I don't understand you comment at all.
In QByteArray reply, the "reply" is only a name of a local variable (defined inside the function). It has no relation to any other part of your program, not until you make it related by passing it to some other function or control. I hope that explanation helps you, I'm not sure, your replies are very confusing to me.
-
We still don't know what you have in your ui-file, so here a solution without one:
void MainWindow::readSerialData()
{
QString s;
QByteArray datalog(mSerialport->readAll());
for(int i = 0; i < datalog.size(); i++)
s.append(QString::number(static_cast<unsigned char>(datalog[i]),16).toUpper().rightJustified(2,'0')+ " ");QLineEdit * popUp = new QLineEdit();
popUp->setReadOnly(true);
popUp->setText(s);
popUp->show();//cleanup
popUp->setAttribute(Qt::WA_DeleteOnClose,true);
connect(qApp, &QApplication::aboutToQuit, popUp, QLineEdit::deleteLater);
}Edit: deleted double show call
-
@J.Hilk
It was resolved but another error occurred.
../myserial/mainwindow.cpp:102:76: error: cannot call member function ‘void QObject::deleteLater()’ without object
connect(qApp, &QApplication::aboutToQuit,popUp, QLineEdit::deleteLater());
^ -
@segtteee said in How can i show data that device sent in QT?:
@J.Hilk
It was resolved but another error occurred.
../myserial/mainwindow.cpp:102:76: error: cannot call member function ‘void QObject::deleteLater()’ without object
connect(qApp, &QApplication::aboutToQuit,popUp, QLineEdit::deleteLater());
^Thats a typing mistake from my side.
connect(qApp, &QApplication::aboutToQuit,popUp, &QLineEdit::deleteLater());