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 see and storage the data from device in QT?
Qt 6.11 is out! See what's new in the release blog

How can I see and storage the data from device in QT?

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 5 Posters 9.5k 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

    this is my ui
    alt text
    what i want
    alt text
    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");
    }
    
    
    
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      This requires good programming practise.

      1. Whenever you click on the button, you need to open the device selected. Right now u have hard coded with mSerialport->setPortName("/dev/ttyUSB0");
      2. use readyRead() signal to check the arrival of data.
      3. use readAll() method to read all the data from device.
      4. Display the data in QTextEdit UI control

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

      S 1 Reply Last reply
      1
      • dheerendraD dheerendra

        This requires good programming practise.

        1. Whenever you click on the button, you need to open the device selected. Right now u have hard coded with mSerialport->setPortName("/dev/ttyUSB0");
        2. use readyRead() signal to check the arrival of data.
        3. use readAll() method to read all the data from device.
        4. Display the data in QTextEdit UI control
        S Offline
        S Offline
        segtteee
        wrote on last edited by
        #3

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

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

          only mSerialport->setPortName("/dev/ttyUSB0") this may be sufficient.

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

          S 1 Reply Last reply
          1
          • dheerendraD dheerendra

            only mSerialport->setPortName("/dev/ttyUSB0") this may be sufficient.

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

            @dheerendra
            Can not I solve the problem without changing the "mSerialport->setPortName("/dev/ttyUSB0")" ? I do not think I can change it.

            jsulmJ 1 Reply Last reply
            -1
            • S segtteee

              @dheerendra
              Can not I solve the problem without changing the "mSerialport->setPortName("/dev/ttyUSB0")" ? I do not think I can change it.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @segtteee Why can't you change it?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              1
              • jsulmJ jsulm

                @segtteee Why can't you change it?

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

                @jsulm
                I can write code by specifying one port, but I can not write one after choosing multiple ports...i am a beginner sorry..

                jsulmJ 1 Reply Last reply
                0
                • S segtteee

                  @jsulm
                  I can write code by specifying one port, but I can not write one after choosing multiple ports...i am a beginner sorry..

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @segtteee Then you should learn how to do it.
                  Where does the user select the port? Is it a QComboBox? Something else?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  S 1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @segtteee Then you should learn how to do it.
                    Where does the user select the port? Is it a QComboBox? Something else?

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

                    @jsulm
                    it is combobox

                    jsulmJ 1 Reply Last reply
                    0
                    • S segtteee

                      @jsulm
                      it is combobox

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @segtteee Then just go to the documentation: http://doc.qt.io/qt-5/qcombobox.html#currentText-prop
                      Change

                      mSerialport->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? :-)

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      S 1 Reply Last reply
                      2
                      • jsulmJ jsulm

                        @segtteee Then just go to the documentation: http://doc.qt.io/qt-5/qcombobox.html#currentText-prop
                        Change

                        mSerialport->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? :-)

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

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

                        jsulmJ 1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          Kicer86
                          wrote on last edited by
                          #12

                          @segtteee said in How can I see and storage the data from device in QT?:

                          Can I change only the port name?

                          If you want to hardcode all values in code, why do you put comboboxes in ui?

                          1 Reply Last reply
                          0
                          • S segtteee

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

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by jsulm
                            #13

                            @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#toInt

                            mSerialport->setBaudRate(ui->comboBox_baudrate->currentText().toInt());
                            

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            S 1 Reply Last reply
                            2
                            • jsulmJ jsulm

                              @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#toInt

                              mSerialport->setBaudRate(ui->comboBox_baudrate->currentText().toInt());
                              
                              S Offline
                              S Offline
                              segtteee
                              wrote on last edited by
                              #14

                              @jsulm
                              I understand that the port name is a string, and baudrate is a number, so toint is an integer, but databits is a number, why not toint? And paritybits is a character, but it should not be used.

                              jsulmJ 1 Reply Last reply
                              0
                              • S segtteee

                                @jsulm
                                I understand that the port name is a string, and baudrate is a number, so toint is an integer, but databits is a number, why not toint? And paritybits is a character, but it should not be used.

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

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

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                S 1 Reply Last reply
                                2
                                • jsulmJ jsulm

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

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

                                  @jsulm
                                  it mean
                                  port name = string so currentText()
                                  baud rate = integer so currentText().toint()

                                  data bits = integer so ??
                                  Is not the toint() correct for the data bits? but error occured

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • S segtteee

                                    @jsulm
                                    it mean
                                    port name = string so currentText()
                                    baud rate = integer so currentText().toint()

                                    data bits = integer so ??
                                    Is not the toint() correct for the data bits? but error occured

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by jsulm
                                    #17

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

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    S 1 Reply Last reply
                                    2
                                    • jsulmJ jsulm

                                      @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 ...
                                      
                                      S Offline
                                      S Offline
                                      segtteee
                                      wrote on last edited by
                                      #18

                                      @jsulm
                                      alt text
                                      alt text

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • S segtteee

                                        @jsulm
                                        alt text
                                        alt text

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @segtteee See my previous post

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        S 1 Reply Last reply
                                        1
                                        • jsulmJ jsulm

                                          @segtteee See my previous post

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

                                          @jsulm

                                          if (ui->comboBox_databits->currentText() == "Data8")
                                          mSerialport->setDataBits("Data8") ;
                                          error occured ..

                                          jsulmJ 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