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 receive data from device and Store in a different form in QT
Forum Updated to NodeBB v4.3 + New Features

How can I receive data from device and Store in a different form in QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.9k Views
  • 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

    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->textEdit->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());
         //qDebug(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
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      multiple questions here.

      1. U have already specified readread signal with slot readSerialData(). Is this called whenever the data comes ?
      2. Using timer you can connect to slot. Inside the slot open the serial device and read the data using readall() function.
      3. You must be knowing the format of data received. Append each piece of data into string separated by comma. write this string in to a file. This is more file and string operations & nothing to do with qt.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      S 1 Reply Last reply
      1
      • dheerendraD dheerendra

        multiple questions here.

        1. U have already specified readread signal with slot readSerialData(). Is this called whenever the data comes ?
        2. Using timer you can connect to slot. Inside the slot open the serial device and read the data using readall() function.
        3. You must be knowing the format of data received. Append each piece of data into string separated by comma. write this string in to a file. This is more file and string operations & nothing to do with qt.
        S Offline
        S Offline
        segtteee
        wrote on last edited by
        #3

        @dheerendra
        Thank you for your reply.
        If i input some command and push the send button , device can recognize command and conduct command.
        I want to see data sented by device . I think the data is in numerice form.
        How can i see data by device at my QT UI?

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          when you do readall() it must returning data in byte array format. Just print th data using qdebug(). U should see the data. All the data may be in integer format. U need to convert the data to ascii format to make meaningful data.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          S 1 Reply Last reply
          0
          • dheerendraD dheerendra

            when you do readall() it must returning data in byte array format. Just print th data using qdebug(). U should see the data. All the data may be in integer format. U need to convert the data to ascii format to make meaningful data.

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

            @dheerendra
            I added the code

            void MainWindow::readData()
            {
                QByteArray devicedata ;
                devicedata=mSerialport->readAll();
            
                qDebug() << "Received data: " << devicedata ;
            
            }
            

            but how can i see the data in my ui?

            1 Reply Last reply
            -1
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              to see the data in ui you need to use QTexEdit object. It has method called setText set the data u read from readall() function.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              S 1 Reply Last reply
              0
              • dheerendraD dheerendra

                to see the data in ui you need to use QTexEdit object. It has method called setText set the data u read from readall() function.

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

                @dheerendra

                i use Textbrowser in UI not this??

                1 Reply Last reply
                -1

                • Login

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