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



  • 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"); }
    
    


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



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



  • 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

    i use Textbrowser in UI not this??


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.