Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    Mobile and Embedded
    4
    15
    2464
    Loading More Posts
    • 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 last edited by Mohit Tripathi

      #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);
      }
      
      

      //your code here(.h)

      #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();
      
      private:
          Ui::Dialog *ui;
          QSerialPort *arduino;
          static const quint16 arduino_uno_vendorid =9025;
          static const quint16 arduino_uno_productid =67;
          void updateLCD(const QString);
          void serialReceived();
          QString port_name;
          //void readSerial();
          QByteArray serialData;
          QString serialBuffer;
          bool arduino_is_available;
      
      };
      
      #endif // DIALOG_H
      

      I just started the Qt. I want to connect Qt with arduino serially. I am reading the data but I am not able to connect with arduino slot.
      I am getting a message after compilation. The message is QObject::connect: No such slot Dialog::&serialReceived() in ..\serial_sensor\dialog.cpp:45
      QObject::connect: (receiver name: 'Dialog').

      Can I know why?

      J.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @Mohit Tripathi last edited by

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

        private:
        Ui::Dialog *ui;
        QSerialPort *arduino;
        static const quint16 arduino_uno_vendorid =9025;
        static const quint16 arduino_uno_productid =67;
        void updateLCD(const QString);
        void serialReceived();

        void serialReceived(); is clearly private and has not the slots magro needed for the Qt4-Style syntax you're using for your connect statement, that won't work;

        use private slots: if you want to access it from within the class via Signal/slot
        use public slots: if you want to access it from outside.

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply Reply Quote 3
        • M
          Mohit Tripathi last edited by

          It is still not working.

          J.Hilk 1 Reply Last reply Reply Quote 0
          • J.Hilk
            J.Hilk Moderators @Mohit Tripathi last edited by

            @Mohit-Tripathi
            QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived()));

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

            Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            M 1 Reply Last reply Reply Quote 1
            • M
              Mohit Tripathi @J.Hilk last edited by 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.Hilk 1 Reply Last reply Reply Quote 0
              • J.Hilk
                J.Hilk Moderators @Mohit Tripathi last edited by

                @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

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                M 1 Reply Last reply Reply Quote 0
                • M
                  Mohit Tripathi @J.Hilk last edited by

                  @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.Hilk VRonin 2 Replies Last reply Reply Quote 0
                  • J.Hilk
                    J.Hilk Moderators @Mohit Tripathi last edited by J.Hilk

                    @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

                    Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply Reply Quote 3
                    • VRonin
                      VRonin @Mohit Tripathi last edited by 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);

                      "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 Reply Quote 3
                      • M
                        Mohit Tripathi @VRonin last edited by

                        @VRonin Thanks a lot.

                        1 Reply Last reply Reply Quote 0
                        • M
                          Mohit Tripathi @VRonin last edited by

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

                          VRonin J.Hilk 2 Replies Last reply Reply Quote 0
                          • VRonin
                            VRonin @Mohit Tripathi last edited by

                            @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 Reply Quote 2
                            • J.Hilk
                              J.Hilk Moderators @Mohit Tripathi last edited by 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.

                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              M 1 Reply Last reply Reply Quote 0
                              • M
                                Mohit Tripathi @J.Hilk last edited by Mohit Tripathi

                                @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 Reply Quote 0
                                • Pablo J. Rogina
                                  Pablo J. Rogina last edited by

                                  @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 Reply Quote 1
                                  • First post
                                    Last post