Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can i show data that device sent in QT?
Forum Updated to NodeBB v4.3 + New Features

How can i show data that device sent in QT?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 3 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    segtteee
    wrote on last edited by
    #1

    How can i show data that device sent in QT?

    alt text
    alt text
    This is ui I made in QT. I want see data in data log window.
    alt text

    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");
     }
    
    
    1 Reply Last reply
    -1
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @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?

      (Z(:^

      S 1 Reply Last reply
      1
      • sierdzioS sierdzio

        @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?

        S Offline
        S Offline
        segtteee
        wrote on last edited by
        #3

        @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?

        J.HilkJ sierdzioS 2 Replies Last reply
        0
        • S segtteee

          @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?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @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')+ " ");
          }
          
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          S J.HilkJ 2 Replies Last reply
          2
          • S segtteee

            @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?

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            @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);
            

            (Z(:^

            S 1 Reply Last reply
            1
            • sierdzioS sierdzio

              @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);
              
              S Offline
              S Offline
              segtteee
              wrote on last edited by
              #6

              @sierdzio
              sorry.. my ui is mainwindow.ui and port is mSerialport .
              textedit object's name is datalog , I would like to see the data here

              sierdzioS 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @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')+ " ");
                }
                
                
                S Offline
                S Offline
                segtteee
                wrote on last edited by
                #7

                @J.Hilk
                I modified it as you said but nothing changed..

                1 Reply Last reply
                0
                • S segtteee

                  @sierdzio
                  sorry.. my ui is mainwindow.ui and port is mSerialport .
                  textedit object's name is datalog , I would like to see the data here

                  sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  @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 here

                  Well, you have all the information to make it work, then.

                  QByteArray datalog(mSerialport->readAll());
                  ui->datalog->setText(datalog);
                  

                  (Z(:^

                  S 1 Reply Last reply
                  1
                  • sierdzioS sierdzio

                    @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 here

                    Well, you have all the information to make it work, then.

                    QByteArray datalog(mSerialport->readAll());
                    ui->datalog->setText(datalog);
                    
                    S Offline
                    S Offline
                    segtteee
                    wrote on last edited by
                    #9

                    @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

                    sierdzioS 1 Reply Last reply
                    0
                    • S segtteee

                      @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

                      sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

                      @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 data

                      Sorry, 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.

                      (Z(:^

                      1 Reply Last reply
                      1
                      • J.HilkJ J.Hilk

                        @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')+ " ");
                        }
                        
                        
                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by J.Hilk
                        #11

                        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


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        S 1 Reply Last reply
                        2
                        • J.HilkJ J.Hilk

                          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

                          S Offline
                          S Offline
                          segtteee
                          wrote on last edited by
                          #12

                          @J.Hilk
                          invalid use of incomplete type ‘class QLineEdit’
                          QLineEdit * popUp = new QLineEdit();
                          error occured . Have I done anything wrong?

                          J.HilkJ 1 Reply Last reply
                          0
                          • S segtteee

                            @J.Hilk
                            invalid use of incomplete type ‘class QLineEdit’
                            QLineEdit * popUp = new QLineEdit();
                            error occured . Have I done anything wrong?

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #13

                            @segtteee
                            add
                            #include <QLineEdit>


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            S 1 Reply Last reply
                            1
                            • J.HilkJ J.Hilk

                              @segtteee
                              add
                              #include <QLineEdit>

                              S Offline
                              S Offline
                              segtteee
                              wrote on last edited by
                              #14

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

                              J.HilkJ 1 Reply Last reply
                              0
                              • S segtteee

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

                                J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on last edited by
                                #15

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


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                S 1 Reply Last reply
                                1
                                • J.HilkJ J.Hilk

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

                                  S Offline
                                  S Offline
                                  segtteee
                                  wrote on last edited by
                                  #16

                                  @J.Hilk
                                  thanks, it was not occured error after writing the code "connect(qApp,&QApplication::aboutToQuit,popUp,&QLineEdit::deleteLater);"

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved