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 to send the Data from Qt App to Arduino by serial communication
Forum Updated to NodeBB v4.3 + New Features

How to send the Data from Qt App to Arduino by serial communication

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 8 Posters 4.7k Views 3 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.
  • JonBJ JonB

    @LeLev said in How to send the Data from Qt App to Arduino by serial communication:

    hi, the 2nd line ot his on_send_clicked method doas qDebbug() << "serial opened-"

    You are right. I searched the whole page for serial opened (or even seral opened), I have only just seen it is in a screenshot. However, I still do not see where the baToSend used there is declared or set, I only see it elsewhere (local variable inside on_send_clicked()). Nor do I know what it is the text file read in. So it's still pretty hard to know what is going on....

    sankarapandiyanS Offline
    sankarapandiyanS Offline
    sankarapandiyan
    wrote on last edited by sankarapandiyan
    #16

    @JonB Tried to Edit my code in correct form

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QList>
    #include <QComboBox>
    #include <QString>
    #include <QTextEdit>
    #include <QTime>
    #include <QDebug>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QtSerialPort>
    #include <QFile>
    #include <QList>
    #include <QSerialPort>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
    
          ui->setupUi(this);
          QString filename1="/home/adx-soft1/Shankar/serial/myfile1.txt";
          QFile file(filename1);
          if(!file.exists()){
              qDebug() << "NO FILE "<<filename1;
          }else{
              qDebug() << filename1<<" ...";
          }
          QString line1;
          ui->sender->clear();
          if (file.open(QIODevice::ReadOnly | QIODevice::Text)){ ui->sender->setText(file.readAll()); }
    
          QList<QSerialPortInfo> list;
          list = QSerialPortInfo::availablePorts();
    
          for (int i= 0; i < list.length(); i++)
          {
            ui->comboBox->addItem(list[i].portName());
    
          }
    }
    
    MainWindow::~MainWindow()
    {
        serial->close();
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        serial = new QSerialPort(this);
        serial->setPortName(ui->comboBox->currentText());
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
        connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
    
       ui->progressBar->setValue(100);
    //    ui->receive->clear();
    }
    void MainWindow::checkAndSendData()
    {
        if(!m_lines.isEmpty()){
            baToSend = m_lines.takeFirst().toUtf8();
            serial->write(baToSend);
            //qDebug() << baToSend << "serial Writed---" ;
    
            ui->aknowmnt->setText("Sent Message -->" +  QString(baToSend) + "\n");
        }
        else
        {
            ui->aknowmnt->setText("Receiving ");
        }
    }
    
    
    void MainWindow ::on_send_clicked()
    {
          //serial->open(QIODevice::ReadWrite);
          qDebug () << baToSend << "seral opened ----" ;
    
          qDebug() << "Serial: " << serial->isOpen();
          qDebug() << "Error: " << serial->error() << " | " << serial->errorString();
    
          line = ui->sender->toPlainText();
            QString txt1 = ui->receive->toPlainText();
            /*QString*/ txt3 = ui->aknowmnt->toPlainText();
    
            qDebug() << "m_lines: " << m_lines;
    
             if(m_lines.isEmpty()){
                QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                inputFile.open(QIODevice::ReadOnly);
    
                if (!inputFile.isOpen()) {
                    return;
                }
    
                QTextStream stream(&inputFile);
    
                while(!stream.atEnd()) {
                    QString strline = stream.readLine();
                    m_lines.append(strline + QString("\r\n"));
    
                    qDebug() << "m_lines: " << m_lines;
                }
            }
            checkAndSendData();
    }
    
    
    void MainWindow::serialReceived()
    {
    
        qDebug() << "serial->readAll().trimmed(): " << serial->readAll().trimmed();
    
        QString glen = serial->readAll().trimmed();
    
        qDebug() << "glen: " << glen;
    
         if (glen.length() > 0) {
             ui->receive->append(glen);
            /*QString*/ //line = ui->receive->toPlainText();
            //ui->receive->setText(ui->receive->toPlainText() + line + "\n");
            //ui->receive->setText(line + "\r\n" + glen + "\r\n");
            //line = txt3;
        }
        checkAndSendData();
    }
    

    But i did not get the data received in arduino side

    LematL 1 Reply Last reply
    0
    • ODБOïO ODБOï

      @JonB said in How to send the Data from Qt App to Arduino by serial communication:

      you have a screenshot showing output where the code you have shown simply does not include any such output lines..

      hi, the 2nd line in his on_send_clicked method doas qDebbug() << "serial opened-"

      @sankarapandiyan you should put it in a if statement to call qDebug only if open() returns true

      if(serial->open(QIODevice::ReadWrite))
      
      sankarapandiyanS Offline
      sankarapandiyanS Offline
      sankarapandiyan
      wrote on last edited by
      #17

      @LeLev ok fine i will correct it

      1 Reply Last reply
      0
      • JonBJ JonB

        @LeLev said in How to send the Data from Qt App to Arduino by serial communication:

        hi, the 2nd line ot his on_send_clicked method doas qDebbug() << "serial opened-"

        You are right. I searched the whole page for serial opened (or even seral opened), I have only just seen it is in a screenshot. However, I still do not see where the baToSend used there is declared or set, I only see it elsewhere (local variable inside on_send_clicked()). Nor do I know what it is the text file read in. So it's still pretty hard to know what is going on....

        sankarapandiyanS Offline
        sankarapandiyanS Offline
        sankarapandiyan
        wrote on last edited by
        #18

        @JonB @LeLev @mrjj cant open port : QSerialPort::SerialPortError(PermissionError)
        I get the error like this ..

        Thanks

        jsulmJ 1 Reply Last reply
        0
        • sankarapandiyanS sankarapandiyan

          @JonB @LeLev @mrjj cant open port : QSerialPort::SerialPortError(PermissionError)
          I get the error like this ..

          Thanks

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

          @sankarapandiyan said in How to send the Data from Qt App to Arduino by serial communication:

          PermissionError

          Make sure your user has needed access rights for the serial port device file.
          See for example https://askubuntu.com/questions/58119/changing-permissions-on-serial-port

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

          sankarapandiyanS 1 Reply Last reply
          2
          • jsulmJ jsulm

            @sankarapandiyan said in How to send the Data from Qt App to Arduino by serial communication:

            PermissionError

            Make sure your user has needed access rights for the serial port device file.
            See for example https://askubuntu.com/questions/58119/changing-permissions-on-serial-port

            sankarapandiyanS Offline
            sankarapandiyanS Offline
            sankarapandiyan
            wrote on last edited by
            #20

            @jsulm Is it possible to open the port in both Qt app and Arduino in a same OS ..
            Here if i open the port in Qt means , in Arduino ,i got a error the port is changed or not found

            while if i open the port in Arduino first means , I got a error message in Qt as (PERMISSION ERROR)

            I am Stucked here for a day, I am very new to this arduino topic , So give me some idea abouth this .

            Thanks in advance .

            jsulmJ JKSHJ 2 Replies Last reply
            0
            • sankarapandiyanS sankarapandiyan

              @jsulm Is it possible to open the port in both Qt app and Arduino in a same OS ..
              Here if i open the port in Qt means , in Arduino ,i got a error the port is changed or not found

              while if i open the port in Arduino first means , I got a error message in Qt as (PERMISSION ERROR)

              I am Stucked here for a day, I am very new to this arduino topic , So give me some idea abouth this .

              Thanks in advance .

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

              @sankarapandiyan said in How to send the Data from Qt App to Arduino by serial communication:

              Is it possible to open the port in both Qt app and Arduino in a same OS

              Not sure what you mean? If both devices run same OS then yes.

              "i got a error the port is changed or not found" - then make sure you're using correct port!

              "I got a error message in Qt as (PERMISSION ERROR)" - then make sure your user has access right like I suggested above.

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

              sankarapandiyanS 1 Reply Last reply
              5
              • sankarapandiyanS sankarapandiyan

                @jsulm Is it possible to open the port in both Qt app and Arduino in a same OS ..
                Here if i open the port in Qt means , in Arduino ,i got a error the port is changed or not found

                while if i open the port in Arduino first means , I got a error message in Qt as (PERMISSION ERROR)

                I am Stucked here for a day, I am very new to this arduino topic , So give me some idea abouth this .

                Thanks in advance .

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #22

                @sankarapandiyan said in How to send the Data from Qt App to Arduino by serial communication:

                Is it possible to open the port in both Qt app and Arduino in a same OS ..

                If you have 2 apps on the same computer that want to use the same serial port, then only one of the programs can use the serial port at a time.

                When App 1 opens a serial port, App 2 cannot use the same serial port. App 2 must wait until App 1 closes the port.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                sankarapandiyanS 1 Reply Last reply
                6
                • sankarapandiyanS sankarapandiyan

                  @JonB Tried to Edit my code in correct form

                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  #include <QList>
                  #include <QComboBox>
                  #include <QString>
                  #include <QTextEdit>
                  #include <QTime>
                  #include <QDebug>
                  #include <QtSerialPort/QSerialPortInfo>
                  #include <QtSerialPort>
                  #include <QFile>
                  #include <QList>
                  #include <QSerialPort>
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                      , ui(new Ui::MainWindow)
                  {
                  
                        ui->setupUi(this);
                        QString filename1="/home/adx-soft1/Shankar/serial/myfile1.txt";
                        QFile file(filename1);
                        if(!file.exists()){
                            qDebug() << "NO FILE "<<filename1;
                        }else{
                            qDebug() << filename1<<" ...";
                        }
                        QString line1;
                        ui->sender->clear();
                        if (file.open(QIODevice::ReadOnly | QIODevice::Text)){ ui->sender->setText(file.readAll()); }
                  
                        QList<QSerialPortInfo> list;
                        list = QSerialPortInfo::availablePorts();
                  
                        for (int i= 0; i < list.length(); i++)
                        {
                          ui->comboBox->addItem(list[i].portName());
                  
                        }
                  }
                  
                  MainWindow::~MainWindow()
                  {
                      serial->close();
                      delete ui;
                  }
                  
                  void MainWindow::on_pushButton_clicked()
                  {
                      serial = new QSerialPort(this);
                      serial->setPortName(ui->comboBox->currentText());
                      serial->setBaudRate(QSerialPort::Baud9600);
                      serial->setDataBits(QSerialPort::Data8);
                      serial->setParity(QSerialPort::NoParity);
                      serial->setStopBits(QSerialPort::OneStop);
                      serial->setFlowControl(QSerialPort::NoFlowControl);
                      serial->open(QIODevice::ReadWrite);
                      connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
                  
                     ui->progressBar->setValue(100);
                  //    ui->receive->clear();
                  }
                  void MainWindow::checkAndSendData()
                  {
                      if(!m_lines.isEmpty()){
                          baToSend = m_lines.takeFirst().toUtf8();
                          serial->write(baToSend);
                          //qDebug() << baToSend << "serial Writed---" ;
                  
                          ui->aknowmnt->setText("Sent Message -->" +  QString(baToSend) + "\n");
                      }
                      else
                      {
                          ui->aknowmnt->setText("Receiving ");
                      }
                  }
                  
                  
                  void MainWindow ::on_send_clicked()
                  {
                        //serial->open(QIODevice::ReadWrite);
                        qDebug () << baToSend << "seral opened ----" ;
                  
                        qDebug() << "Serial: " << serial->isOpen();
                        qDebug() << "Error: " << serial->error() << " | " << serial->errorString();
                  
                        line = ui->sender->toPlainText();
                          QString txt1 = ui->receive->toPlainText();
                          /*QString*/ txt3 = ui->aknowmnt->toPlainText();
                  
                          qDebug() << "m_lines: " << m_lines;
                  
                           if(m_lines.isEmpty()){
                              QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                              inputFile.open(QIODevice::ReadOnly);
                  
                              if (!inputFile.isOpen()) {
                                  return;
                              }
                  
                              QTextStream stream(&inputFile);
                  
                              while(!stream.atEnd()) {
                                  QString strline = stream.readLine();
                                  m_lines.append(strline + QString("\r\n"));
                  
                                  qDebug() << "m_lines: " << m_lines;
                              }
                          }
                          checkAndSendData();
                  }
                  
                  
                  void MainWindow::serialReceived()
                  {
                  
                      qDebug() << "serial->readAll().trimmed(): " << serial->readAll().trimmed();
                  
                      QString glen = serial->readAll().trimmed();
                  
                      qDebug() << "glen: " << glen;
                  
                       if (glen.length() > 0) {
                           ui->receive->append(glen);
                          /*QString*/ //line = ui->receive->toPlainText();
                          //ui->receive->setText(ui->receive->toPlainText() + line + "\n");
                          //ui->receive->setText(line + "\r\n" + glen + "\r\n");
                          //line = txt3;
                      }
                      checkAndSendData();
                  }
                  

                  But i did not get the data received in arduino side

                  LematL Offline
                  LematL Offline
                  Lemat
                  wrote on last edited by
                  #23
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • JKSHJ JKSH

                    @sankarapandiyan said in How to send the Data from Qt App to Arduino by serial communication:

                    Is it possible to open the port in both Qt app and Arduino in a same OS ..

                    If you have 2 apps on the same computer that want to use the same serial port, then only one of the programs can use the serial port at a time.

                    When App 1 opens a serial port, App 2 cannot use the same serial port. App 2 must wait until App 1 closes the port.

                    sankarapandiyanS Offline
                    sankarapandiyanS Offline
                    sankarapandiyan
                    wrote on last edited by
                    #24

                    @JKSH Yes You are right .. thanks for your reply

                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @sankarapandiyan said in How to send the Data from Qt App to Arduino by serial communication:

                      Is it possible to open the port in both Qt app and Arduino in a same OS

                      Not sure what you mean? If both devices run same OS then yes.

                      "i got a error the port is changed or not found" - then make sure you're using correct port!

                      "I got a error message in Qt as (PERMISSION ERROR)" - then make sure your user has access right like I suggested above.

                      sankarapandiyanS Offline
                      sankarapandiyanS Offline
                      sankarapandiyan
                      wrote on last edited by
                      #25

                      @jsulm sure

                      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