Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Unable to find the slot of arduino though Qt...
Forum Updated to NodeBB v4.3 + New Features

Unable to find the slot of arduino though Qt...

Scheduled Pinned Locked Moved Solved Mobile and Embedded
15 Posts 4 Posters 3.1k 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.
  • M Mohit Tripathi

    @J.Hilk I have done same thing in my main code as you can check in my code above QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived()));. I have changed the private slots to public slots of void serialReceived();.
    Please check it.
    Can you tell me what i am doing wrong here?

    I have corrected it to QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived()));.

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

    @Mohit-Tripathi
    please post your updated code and the error message you get.


    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.

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

      @Mohit-Tripathi
      please post your updated code and the error message you get.

      M Offline
      M Offline
      Mohit Tripathi
      wrote on last edited by
      #7

      @J.Hilk

      #ifndef DIALOG_H
      #define DIALOG_H
      
      #include <QDialog>
      #include <QSerialPort>
      
      namespace Ui {
      class Dialog;
      }
      
      class Dialog : public QDialog
      {
          Q_OBJECT
      
      public:
          explicit Dialog(QWidget *parent = 0);
          ~Dialog();
          void serialReceived();
      
      private:
          Ui::Dialog *ui;
          QSerialPort *arduino;
          static const quint16 arduino_uno_vendorid =9025;
          static const quint16 arduino_uno_productid =67;
          void updateLCD(const QString);
          QString port_name;
          QByteArray serialData;
          QString serialBuffer;
          bool arduino_is_available;
      
      public:
      
      };
      
      #endif // DIALOG_H
      
      #include "dialog.h"
      #include "ui_dialog.h"
      #include <QSerialPort>
      #include <QSerialPortInfo>
      #include <QDebug>
      #include <QtWidgets>
      #include <QString>
      #include <string>
      #include <QObject>
      
      
      Dialog::Dialog(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::Dialog)
      {
          ui->setupUi(this);
          //ui->degree_lcdNumber->display("---");
         // ui->distance_lcdNumber->display("---");
          arduino_is_available = false;
          port_name = "";
          arduino =new QSerialPort;
          serialBuffer = "";
      
          foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) {
              if(serialPortInfo.hasVendorIdentifier() &&serialPortInfo.hasProductIdentifier()){
                  if(serialPortInfo.vendorIdentifier() == arduino_uno_vendorid){
                      if(serialPortInfo.productIdentifier()== arduino_uno_productid){
                          port_name = serialPortInfo.portName();
                          arduino_is_available = true;
      
                      }
                  }
              }
      
          }
          if(arduino_is_available){
              //open and configure the port
              arduino->setPortName(port_name);
              arduino->open(QSerialPort::ReadOnly);
              arduino->setBaudRate(QSerialPort::Baud9600);
              arduino->setDataBits(QSerialPort::Data8);
              arduino->setParity(QSerialPort::NoParity);
              arduino->setStopBits(QSerialPort::OneStop);
              arduino->setFlowControl(QSerialPort::NoFlowControl);
          QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived()));
      
          }else{
              //give error message
              QMessageBox::warning(this,"Port Error","Couldn't find the Arduino!");
          }
      }
      
      
      Dialog::~Dialog()
      {
          if(arduino->isOpen()){
              arduino->close();
          }
          delete ui;
      }
      
      void Dialog::serialReceived(){
      
          qDebug()<<"works" ;
          QStringList bufferSplit = serialBuffer.split(".");
      
              serialData = arduino->readAll();
              serialBuffer += QString::fromStdString(serialData.toStdString());
      
           serialBuffer  = ",";
                            qDebug()<<bufferSplit;
      }
      void Dialog::updateLCD(const QString sensor_reading){
      //    ui->degree_lcdNumber->display(sensor_reading);
      }
      

      I think, i have corrected it as per your instructions.
      Please let me know what is wrong now.

      J.HilkJ VRoninV 2 Replies Last reply
      0
      • M Mohit Tripathi

        @J.Hilk

        #ifndef DIALOG_H
        #define DIALOG_H
        
        #include <QDialog>
        #include <QSerialPort>
        
        namespace Ui {
        class Dialog;
        }
        
        class Dialog : public QDialog
        {
            Q_OBJECT
        
        public:
            explicit Dialog(QWidget *parent = 0);
            ~Dialog();
            void serialReceived();
        
        private:
            Ui::Dialog *ui;
            QSerialPort *arduino;
            static const quint16 arduino_uno_vendorid =9025;
            static const quint16 arduino_uno_productid =67;
            void updateLCD(const QString);
            QString port_name;
            QByteArray serialData;
            QString serialBuffer;
            bool arduino_is_available;
        
        public:
        
        };
        
        #endif // DIALOG_H
        
        #include "dialog.h"
        #include "ui_dialog.h"
        #include <QSerialPort>
        #include <QSerialPortInfo>
        #include <QDebug>
        #include <QtWidgets>
        #include <QString>
        #include <string>
        #include <QObject>
        
        
        Dialog::Dialog(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::Dialog)
        {
            ui->setupUi(this);
            //ui->degree_lcdNumber->display("---");
           // ui->distance_lcdNumber->display("---");
            arduino_is_available = false;
            port_name = "";
            arduino =new QSerialPort;
            serialBuffer = "";
        
            foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) {
                if(serialPortInfo.hasVendorIdentifier() &&serialPortInfo.hasProductIdentifier()){
                    if(serialPortInfo.vendorIdentifier() == arduino_uno_vendorid){
                        if(serialPortInfo.productIdentifier()== arduino_uno_productid){
                            port_name = serialPortInfo.portName();
                            arduino_is_available = true;
        
                        }
                    }
                }
        
            }
            if(arduino_is_available){
                //open and configure the port
                arduino->setPortName(port_name);
                arduino->open(QSerialPort::ReadOnly);
                arduino->setBaudRate(QSerialPort::Baud9600);
                arduino->setDataBits(QSerialPort::Data8);
                arduino->setParity(QSerialPort::NoParity);
                arduino->setStopBits(QSerialPort::OneStop);
                arduino->setFlowControl(QSerialPort::NoFlowControl);
            QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived()));
        
            }else{
                //give error message
                QMessageBox::warning(this,"Port Error","Couldn't find the Arduino!");
            }
        }
        
        
        Dialog::~Dialog()
        {
            if(arduino->isOpen()){
                arduino->close();
            }
            delete ui;
        }
        
        void Dialog::serialReceived(){
        
            qDebug()<<"works" ;
            QStringList bufferSplit = serialBuffer.split(".");
        
                serialData = arduino->readAll();
                serialBuffer += QString::fromStdString(serialData.toStdString());
        
             serialBuffer  = ",";
                              qDebug()<<bufferSplit;
        }
        void Dialog::updateLCD(const QString sensor_reading){
        //    ui->degree_lcdNumber->display(sensor_reading);
        }
        

        I think, i have corrected it as per your instructions.
        Please let me know what is wrong now.

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

        @Mohit-Tripathi

        again, you need to SLOT-magromacro for it to work with Qt4-Syntax

        //Change it from this
        public:
            explicit Dialog(QWidget *parent = 0);
            ~Dialog();
            void serialReceived();
        
        //to
        public:
            explicit Dialog(QWidget *parent = 0);
            ~Dialog();
        
        public slots:
            void serialReceived();
        

        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.

        1 Reply Last reply
        3
        • M Mohit Tripathi

          @J.Hilk

          #ifndef DIALOG_H
          #define DIALOG_H
          
          #include <QDialog>
          #include <QSerialPort>
          
          namespace Ui {
          class Dialog;
          }
          
          class Dialog : public QDialog
          {
              Q_OBJECT
          
          public:
              explicit Dialog(QWidget *parent = 0);
              ~Dialog();
              void serialReceived();
          
          private:
              Ui::Dialog *ui;
              QSerialPort *arduino;
              static const quint16 arduino_uno_vendorid =9025;
              static const quint16 arduino_uno_productid =67;
              void updateLCD(const QString);
              QString port_name;
              QByteArray serialData;
              QString serialBuffer;
              bool arduino_is_available;
          
          public:
          
          };
          
          #endif // DIALOG_H
          
          #include "dialog.h"
          #include "ui_dialog.h"
          #include <QSerialPort>
          #include <QSerialPortInfo>
          #include <QDebug>
          #include <QtWidgets>
          #include <QString>
          #include <string>
          #include <QObject>
          
          
          Dialog::Dialog(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::Dialog)
          {
              ui->setupUi(this);
              //ui->degree_lcdNumber->display("---");
             // ui->distance_lcdNumber->display("---");
              arduino_is_available = false;
              port_name = "";
              arduino =new QSerialPort;
              serialBuffer = "";
          
              foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) {
                  if(serialPortInfo.hasVendorIdentifier() &&serialPortInfo.hasProductIdentifier()){
                      if(serialPortInfo.vendorIdentifier() == arduino_uno_vendorid){
                          if(serialPortInfo.productIdentifier()== arduino_uno_productid){
                              port_name = serialPortInfo.portName();
                              arduino_is_available = true;
          
                          }
                      }
                  }
          
              }
              if(arduino_is_available){
                  //open and configure the port
                  arduino->setPortName(port_name);
                  arduino->open(QSerialPort::ReadOnly);
                  arduino->setBaudRate(QSerialPort::Baud9600);
                  arduino->setDataBits(QSerialPort::Data8);
                  arduino->setParity(QSerialPort::NoParity);
                  arduino->setStopBits(QSerialPort::OneStop);
                  arduino->setFlowControl(QSerialPort::NoFlowControl);
              QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived()));
          
              }else{
                  //give error message
                  QMessageBox::warning(this,"Port Error","Couldn't find the Arduino!");
              }
          }
          
          
          Dialog::~Dialog()
          {
              if(arduino->isOpen()){
                  arduino->close();
              }
              delete ui;
          }
          
          void Dialog::serialReceived(){
          
              qDebug()<<"works" ;
              QStringList bufferSplit = serialBuffer.split(".");
          
                  serialData = arduino->readAll();
                  serialBuffer += QString::fromStdString(serialData.toStdString());
          
               serialBuffer  = ",";
                                qDebug()<<bufferSplit;
          }
          void Dialog::updateLCD(const QString sensor_reading){
          //    ui->degree_lcdNumber->display(sensor_reading);
          }
          

          I think, i have corrected it as per your instructions.
          Please let me know what is wrong now.

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #9

          @Mohit-Tripathi said in Unable to find the slot of arduino though Qt...:

          i have corrected it as per your instructions.

          Nope, you forgot the slot part: Q_SLOT void serialReceived();

          Or, if you are using Qt5, even better: connect(arduino,&QSerialPort::readyRead,this,&Dialog::serialReceived);

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          M 2 Replies Last reply
          3
          • VRoninV VRonin

            @Mohit-Tripathi said in Unable to find the slot of arduino though Qt...:

            i have corrected it as per your instructions.

            Nope, you forgot the slot part: Q_SLOT void serialReceived();

            Or, if you are using Qt5, even better: connect(arduino,&QSerialPort::readyRead,this,&Dialog::serialReceived);

            M Offline
            M Offline
            Mohit Tripathi
            wrote on last edited by
            #10

            @VRonin Thanks a lot.

            1 Reply Last reply
            0
            • VRoninV VRonin

              @Mohit-Tripathi said in Unable to find the slot of arduino though Qt...:

              i have corrected it as per your instructions.

              Nope, you forgot the slot part: Q_SLOT void serialReceived();

              Or, if you are using Qt5, even better: connect(arduino,&QSerialPort::readyRead,this,&Dialog::serialReceived);

              M Offline
              M Offline
              Mohit Tripathi
              wrote on last edited by
              #11

              @VRonin

              void Dialog::serialReceived()
              {
                QStringList bufferSplit = serialBuffer.split(",");
                  if(bufferSplit.length()<3){
                      serialData = arduino->readAll();
                      serialBuffer +=QString::fromStdString(serialData.toStdString());
                  }else
                  {
                      qDebug()<< bufferSplit;
                      serialBuffer = "";
                  }
              }
              

              Please let me know what is wrong in this code. It is unable to fetch the data from sensor and print after compiling.

              VRoninV J.HilkJ 2 Replies Last reply
              0
              • M Mohit Tripathi

                @VRonin

                void Dialog::serialReceived()
                {
                  QStringList bufferSplit = serialBuffer.split(",");
                    if(bufferSplit.length()<3){
                        serialData = arduino->readAll();
                        serialBuffer +=QString::fromStdString(serialData.toStdString());
                    }else
                    {
                        qDebug()<< bufferSplit;
                        serialBuffer = "";
                    }
                }
                

                Please let me know what is wrong in this code. It is unable to fetch the data from sensor and print after compiling.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #12

                @Mohit-Tripathi said in Unable to find the slot of arduino though Qt...:

                It is unable to fetch the data from sensor

                Can you describe your problem better than just "it's not working"?

                You have 2 major bugs in your code both due to the fact that serialReceived might be called even with just a partial chunk of data:

                • Multi-byte chars split across 2 readyread calls will just be invalid
                • The last part after the second . might be trucated

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2
                • M Mohit Tripathi

                  @VRonin

                  void Dialog::serialReceived()
                  {
                    QStringList bufferSplit = serialBuffer.split(",");
                      if(bufferSplit.length()<3){
                          serialData = arduino->readAll();
                          serialBuffer +=QString::fromStdString(serialData.toStdString());
                      }else
                      {
                          qDebug()<< bufferSplit;
                          serialBuffer = "";
                      }
                  }
                  

                  Please let me know what is wrong in this code. It is unable to fetch the data from sensor and print after compiling.

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

                  additionally to what @VRonin said, I'm pretty sure, according to your previous post, that serialBuffer is initialized empty and therefore the size of bufferSplit will be 0 and if(bufferSplit.length()<3) will therefore never be true.
                  serious read error on my side.


                  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.

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

                    additionally to what @VRonin said, I'm pretty sure, according to your previous post, that serialBuffer is initialized empty and therefore the size of bufferSplit will be 0 and if(bufferSplit.length()<3) will therefore never be true.
                    serious read error on my side.

                    M Offline
                    M Offline
                    Mohit Tripathi
                    wrote on last edited by Mohit Tripathi
                    #14

                    @J.Hilk @VRonin

                    #include "dialog.h"
                    #include <QApplication>
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        Dialog w;
                        w.setFixedSize(540,240);
                        w.setWindowTitle("Temperature Sensor");
                        w.show();
                    
                        return a.exec();
                    }
                    
                    #ifndef DIALOG_H
                    #define DIALOG_H
                    
                    #include <QDialog>
                    #include <QSerialPort>
                    
                    namespace Ui {
                    class Dialog;
                    }
                    
                    class Dialog : public QDialog
                    {
                        Q_OBJECT
                    
                    public:
                        explicit Dialog(QWidget *parent = 0);
                        ~Dialog();
                        void serialReceived();
                    
                    private:
                        Ui::Dialog *ui;
                        QSerialPort *arduino;
                        static const quint16 arduino_uno_vendorid =9025;
                        static const quint16 arduino_uno_productid =67;
                        void updateLCD(const QString);
                        QString port_name;
                        QByteArray serialData;
                        QString serialBuffer;
                        bool arduino_is_available;
                    
                    public:
                    
                    };
                    
                    #endif // DIALOG_H
                    
                    #include "dialog.h"
                    #include "ui_dialog.h"
                    #include <QSerialPort>
                    #include <QSerialPortInfo>
                    #include <QDebug>
                    #include <QtWidgets>
                    #include <QString>
                    #include <string>
                    #include <QObject>
                    
                    
                    Dialog::Dialog(QWidget *parent) :
                        QDialog(parent),
                        ui(new Ui::Dialog)
                    {
                        ui->setupUi(this);
                        ui->temp_lcdNumber->display("-------");
                        arduino_is_available = false;
                        port_name = "";
                        arduino =new QSerialPort;
                        serialBuffer = "";
                    
                        foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) {
                            if(serialPortInfo.hasVendorIdentifier() &&serialPortInfo.hasProductIdentifier()){
                                if(serialPortInfo.vendorIdentifier() == arduino_uno_vendorid){
                                    if(serialPortInfo.productIdentifier()== arduino_uno_productid){
                                        port_name = serialPortInfo.portName();
                                        arduino_is_available = true;
                    
                                    }
                                }
                            }
                    
                        }
                        if(arduino_is_available){
                            //open and configure the port
                            arduino->setPortName(port_name);
                            arduino->open(QSerialPort::ReadOnly);
                            arduino->setBaudRate(QSerialPort::Baud9600);
                            arduino->setDataBits(QSerialPort::Data8);
                            arduino->setParity(QSerialPort::NoParity);
                            arduino->setStopBits(QSerialPort::OneStop);
                            arduino->setFlowControl(QSerialPort::NoFlowControl);
                       connect(arduino,&QSerialPort::readyRead,this,&Dialog::serialReceived);
                    
                        }else{
                            //give error message
                            QMessageBox::warning(this,"Port Error","Couldn't find the Arduino!");
                        }
                    }
                    
                    
                    Dialog::~Dialog()
                    {
                        if(arduino->isOpen()){
                            arduino->close();
                        }
                        delete ui;
                    }
                    
                    
                    void Dialog::readSerial()
                    {
                       qDebug()<< "works";
                       QStringList bufferSplit = serialBuffer.split(",");
                        if(bufferSplit.length()<3){
                            serialData = arduino->readAll();
                            serialBuffer +=QString::fromStdString(serialData.toStdString());
                        }else
                        {
                            qDebug()<< bufferSplit;
                            Dialog::updateLCD(bufferSplit[1]);
                            serialBuffer = "";
                        }
                    }
                    
                    void Dialog::updateLCD(const QString sensor_reading)
                    {
                     ui->temp_lcdNumber->display(sensor_reading);
                    }
                    

                    This is my whole code. I have defined the LCD as temp_lcdNumber in application. I am not able to see any change in value in application or QT. The sensor is temperature sensor(LM35).
                    Please let me know what is wrong in this code. Why I am not able to see any change in value?

                    1 Reply Last reply
                    0
                    • Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #15

                      @Mohit-Tripathi putting aside that your "whole code" (main.cpp, dialog.h and dialog.cpp) as is is not compiling because of

                      no ‘void Dialog::readSerial()’ member function declared in class ‘Dialog’
                       void Dialog::readSerial()
                                              ^
                      

                      which should be serialReceived() instead, what is the output of both qDebug statements in serialReceived()? Are you seeing data actually received at that point?
                      I'd add another qDebug statement in updateLCD() just before ui->temp_lcdNumber->display(sensor_reading); just to be sure what is the value that the LCD should be displaying
                      In addition, are you sure the Arduino board is actually sending any data? Have you tried running another (non Qt) app to check what the device is really sending? For instance in Linux you can try (use the device Arduino is connected to):

                      tail -f /dev/ttyUSB0
                      

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      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