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. Using QSerialPort in multi widget application
Forum Updated to NodeBB v4.3 + New Features

Using QSerialPort in multi widget application

Scheduled Pinned Locked Moved Unsolved General and Desktop
27 Posts 6 Posters 4.4k Views 2 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.
  • V viniltc

    Hi,
    As per the previous suggestion I was trying to create a singleton class for my serial port module. I have one serial port that needs to be able to read and write in multiple forms.

    My serial module class as follows :

    #include "tetraserial.h"
    
    tetraSerial::tetraSerial(QObject *parent) : QObject(parent)
    {
    
       serialObject = new QSerialPort();
       serialObject->setPortName("com5");
       serialObject->setBaudRate(1000000);
       serialObject->setDataBits(QSerialPort::Data8);
       serialObject->setParity(QSerialPort::NoParity);
       serialObject->setStopBits(QSerialPort::OneStop);
       serialObject->setFlowControl(QSerialPort::HardwareControl); /
       serialObject->open(QIODevice::ReadWrite);
        
    }
    

    and my serial.h as follows

    #ifndef TETRASERIAL_H
    #define TETRASERIAL_H
    
    #include <QObject>
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QDebug>
    
    class tetraSerial : public QObject
    {
        Q_OBJECT
    public:
        explicit tetraSerial(QObject *parent = nullptr);
      static QSerialPort *serialObject;
    
    signals:
    
    public slots:
    };
    
    #endif // TETRASERIAL_H
    

    I was trying to access serialObject from MainWindow as follows:

     connect(tetraSerial::serialObject, SIGNAL(readyRead()), this, SLOT(serialRecived()))
    

    I'm getting an error saying undefined refernce to 'tetraSerial::serialObject'

    what I'm doing wrong here?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #11

    @viniltc
    With the usual proviso that my C++ is shaky, and I'm not sure whether it could cause the error if it's against the connect() line as opposed to from the linker, but: you do not show, but in your tetraserial.cpp (not .h) have you placed the definition static QSerialPort *tetraSerial::serialObject; somewhere?

    1 Reply Last reply
    1
    • V Offline
      V Offline
      viniltc
      wrote on last edited by
      #12

      @JonB said in Using QSerialPort in multi widget application:

      static QSerialPort *tetraSerial::serialObject;

      @JonB
      The error is thrown from the mainWindow.cpp

      Also, I can't place static QSerialPort *tetraSerial::serialObject; in the class definition in tetraserial.h either ; it says extra qualification on member 'serialObject'

      Any thought!?

      JonBJ 1 Reply Last reply
      0
      • V viniltc

        @JonB said in Using QSerialPort in multi widget application:

        static QSerialPort *tetraSerial::serialObject;

        @JonB
        The error is thrown from the mainWindow.cpp

        Also, I can't place static QSerialPort *tetraSerial::serialObject; in the class definition in tetraserial.h either ; it says extra qualification on member 'serialObject'

        Any thought!?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #13

        @viniltc
        I did state that line was specifically not to be put in tetraserial.h, it belongs in tetraserial.cpp if you do not already have it there.

        I don't know now about your error in mainWindow.cpp --- unless you have failed to include tetraSerial.h there, you wouldn't have failed to do that would you??

        1 Reply Last reply
        1
        • V Offline
          V Offline
          viniltc
          wrote on last edited by viniltc
          #14

          @JonB

          I modified the .cpp file as follows:

          #include "tetraserial.h"
          
           QSerialPort *tetraSerial::serialObject;
          
          
          tetraSerial::tetraSerial(QObject *parent) : QObject(parent)
          {
          
             serialObject = new QSerialPort(this);
             serialObject->setPortName("com5");
             serialObject->setBaudRate(1000000);
             serialObject->setDataBits(QSerialPort::Data8);
             serialObject->setParity(QSerialPort::NoParity);
             serialObject->setStopBits(QSerialPort::OneStop);
             serialObject->setFlowControl(QSerialPort::HardwareControl);
             serialObject->open(QIODevice::ReadWrite);
          
          }
          

          I can't defineQSerialPort *tetraSerial::serialObject as static because it is allowed only inside the class definition.

          Now It is building but I'm getting a runtime error, it says in the console:

          cannot connect <null>::readyReady() Mainwindow::serialRecived

          my connect statement in the main as follows:

          connect(tetraSerial::serialObject, SIGNAL(readyRead()), this, SLOT(serialRecived()));

          Any suggestion??

          JonBJ 1 Reply Last reply
          0
          • V viniltc

            @JonB

            I modified the .cpp file as follows:

            #include "tetraserial.h"
            
             QSerialPort *tetraSerial::serialObject;
            
            
            tetraSerial::tetraSerial(QObject *parent) : QObject(parent)
            {
            
               serialObject = new QSerialPort(this);
               serialObject->setPortName("com5");
               serialObject->setBaudRate(1000000);
               serialObject->setDataBits(QSerialPort::Data8);
               serialObject->setParity(QSerialPort::NoParity);
               serialObject->setStopBits(QSerialPort::OneStop);
               serialObject->setFlowControl(QSerialPort::HardwareControl);
               serialObject->open(QIODevice::ReadWrite);
            
            }
            

            I can't defineQSerialPort *tetraSerial::serialObject as static because it is allowed only inside the class definition.

            Now It is building but I'm getting a runtime error, it says in the console:

            cannot connect <null>::readyReady() Mainwindow::serialRecived

            my connect statement in the main as follows:

            connect(tetraSerial::serialObject, SIGNAL(readyRead()), this, SLOT(serialRecived()));

            Any suggestion??

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #15

            @viniltc

            cannot connect <null>::readydata() Mainwindow::serialRecived

            readydata() or readyRead()? Did you copy & paste the error message?

            TBH this is getting to be serious C++ stuff, so I ought bow out. You need someone who knows his C++ better than I!

            1 Reply Last reply
            1
            • V Offline
              V Offline
              viniltc
              wrote on last edited by
              #16

              @viniltc said in Using QSerialPort in multi widget application:

              cannot connect <null>::readydata() Mainwindow::serialRecived
              @JonB
              Sorry my bad yes, that's readyRead()
              Btw thanks a lot @JonB for your suggestions.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                viniltc
                wrote on last edited by viniltc
                #17

                @SGaist @mrjj

                Any help would be welcome..

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #18

                  There's no need to make your serial port static.

                  You don't check whether the open call was successful.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  V 1 Reply Last reply
                  2
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #19

                    hI
                    Also, im a little concerned by sharing the serial idea.
                    Its fine for writing to it, however, if you connect say 3 other classed to
                    readyRead() signal, they all get the signal but whom of them will then read the data ?
                    and how will you know if that class was the intended receiver of the data?

                    It would help if you explained more what the app does and its intended design.

                    V 1 Reply Last reply
                    3
                    • SGaistS SGaist

                      There's no need to make your serial port static.

                      You don't check whether the open call was successful.

                      V Offline
                      V Offline
                      viniltc
                      wrote on last edited by
                      #20

                      @SGaist

                      If it's non-static, I'm getting an error saying invalid use of non-static data member 'serialObject

                      The program is getting crashed from the mainWindow itself

                      connect(tetraSerial::serialObject, SIGNAL(readyRead()), this, SLOT(serialRecived()));
                      

                      Can't find what's wrong with my class definition.

                      jsulmJ 1 Reply Last reply
                      0
                      • V viniltc

                        @SGaist

                        If it's non-static, I'm getting an error saying invalid use of non-static data member 'serialObject

                        The program is getting crashed from the mainWindow itself

                        connect(tetraSerial::serialObject, SIGNAL(readyRead()), this, SLOT(serialRecived()));
                        

                        Can't find what's wrong with my class definition.

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

                        @viniltc You need to use an instance of tetraSerial class

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

                        V 1 Reply Last reply
                        2
                        • mrjjM mrjj

                          hI
                          Also, im a little concerned by sharing the serial idea.
                          Its fine for writing to it, however, if you connect say 3 other classed to
                          readyRead() signal, they all get the signal but whom of them will then read the data ?
                          and how will you know if that class was the intended receiver of the data?

                          It would help if you explained more what the app does and its intended design.

                          V Offline
                          V Offline
                          viniltc
                          wrote on last edited by
                          #22

                          @mrjj

                          It is a GUI to communicate with an FTDI based serial device.

                          • There will be setup stages having three windows. At the end of setup, the GUI should send a 'config file' to the serial device (say CPU -A)

                          • During those setup stages, GUI should also read the serial device to get real-time updates of some sensors connected to the device (CPU-B). Also writing to some registers of the device.

                          • For example, I have three windows - window1, window2, window3. The 'config file' will be sent at window 3. The user will be seeing some sensor values in window2 and updating its parameters by writing into some short registers.

                          All the above operations will be done through a single FTDI based serial (UART).
                          So what I need is to get an instance of opened serial port' in any of those windows to read or write

                          Any thoughts?

                          1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @viniltc You need to use an instance of tetraSerial class

                            V Offline
                            V Offline
                            viniltc
                            wrote on last edited by
                            #23

                            @jsulm

                            I try to modify the class file as follows:

                            #include "tetraserial.h"
                            
                            
                            tetraSerial::tetraSerial(QObject *parent) : QObject(parent)
                            {
                            
                            }
                            
                            void tetraSerial::serialOpen()
                            {
                                
                                serialObject = new QSerialPort();
                                serialObject->setPortName("com5");
                                serialObject->setBaudRate(1000000);
                                serialObject->setDataBits(QSerialPort::Data8);
                                serialObject->setParity(QSerialPort::NoParity);
                                serialObject->setStopBits(QSerialPort::OneStop);
                                serialObject->setFlowControl(QSerialPort::HardwareControl);
                                serialObject->open(QIODevice::ReadWrite);
                                
                                
                            }
                            
                            void tetraSerial::serialRecived2()
                            {
                                
                            }
                            
                            

                            and the header as follows:

                            #ifndef TETRASERIAL_H
                            #define TETRASERIAL_H
                            
                            #include <QObject>
                            #include <QtSerialPort/QSerialPort>
                            #include <QtSerialPort/QSerialPortInfo>
                            #include <QDebug>
                            #include <QFile>
                            #include <QMessageBox>
                            
                            class tetraSerial : public QObject
                            {
                                Q_OBJECT
                            public:
                                explicit tetraSerial(QObject *parent = nullptr);
                            
                              //  static QSerialPort *serialObject;
                              QSerialPort *serialObject;
                              
                              void serialOpen()
                                
                            
                            signals:
                            
                            public slots:
                                      void serialRecived2();
                            };
                            

                            Then I use this in the main as:

                            MainWindow::MainWindow(QWidget *parent) :
                                QMainWindow(parent),
                                ui(new Ui::MainWindow) 
                            {
                                ui->setupUi(this);
                             
                              tetraSerial serial;
                              serial.serialOpen();
                              
                              connect(serial, SIGNAL(readyRead()), this, SLOT(serialRecived2()));
                            
                            
                            }
                            

                            I've got an error says:

                            no matching member function for call to 'connect'

                            I don't understand why the connect function can't find a matching function.

                            jsulmJ 1 Reply Last reply
                            0
                            • V viniltc

                              @jsulm

                              I try to modify the class file as follows:

                              #include "tetraserial.h"
                              
                              
                              tetraSerial::tetraSerial(QObject *parent) : QObject(parent)
                              {
                              
                              }
                              
                              void tetraSerial::serialOpen()
                              {
                                  
                                  serialObject = new QSerialPort();
                                  serialObject->setPortName("com5");
                                  serialObject->setBaudRate(1000000);
                                  serialObject->setDataBits(QSerialPort::Data8);
                                  serialObject->setParity(QSerialPort::NoParity);
                                  serialObject->setStopBits(QSerialPort::OneStop);
                                  serialObject->setFlowControl(QSerialPort::HardwareControl);
                                  serialObject->open(QIODevice::ReadWrite);
                                  
                                  
                              }
                              
                              void tetraSerial::serialRecived2()
                              {
                                  
                              }
                              
                              

                              and the header as follows:

                              #ifndef TETRASERIAL_H
                              #define TETRASERIAL_H
                              
                              #include <QObject>
                              #include <QtSerialPort/QSerialPort>
                              #include <QtSerialPort/QSerialPortInfo>
                              #include <QDebug>
                              #include <QFile>
                              #include <QMessageBox>
                              
                              class tetraSerial : public QObject
                              {
                                  Q_OBJECT
                              public:
                                  explicit tetraSerial(QObject *parent = nullptr);
                              
                                //  static QSerialPort *serialObject;
                                QSerialPort *serialObject;
                                
                                void serialOpen()
                                  
                              
                              signals:
                              
                              public slots:
                                        void serialRecived2();
                              };
                              

                              Then I use this in the main as:

                              MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::MainWindow) 
                              {
                                  ui->setupUi(this);
                               
                                tetraSerial serial;
                                serial.serialOpen();
                                
                                connect(serial, SIGNAL(readyRead()), this, SLOT(serialRecived2()));
                              
                              
                              }
                              

                              I've got an error says:

                              no matching member function for call to 'connect'

                              I don't understand why the connect function can't find a matching function.

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

                              @viniltc What is serial in

                              connect(serial, SIGNAL(readyRead()), this, SLOT(serialRecived2()));
                              

                              ?
                              It needs to be a pointer:

                              connect(&serial, SIGNAL(readyRead()), this, SLOT(serialRecived2()));
                              

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

                              V 1 Reply Last reply
                              3
                              • jsulmJ jsulm

                                @viniltc What is serial in

                                connect(serial, SIGNAL(readyRead()), this, SLOT(serialRecived2()));
                                

                                ?
                                It needs to be a pointer:

                                connect(&serial, SIGNAL(readyRead()), this, SLOT(serialRecived2()));
                                
                                V Offline
                                V Offline
                                viniltc
                                wrote on last edited by viniltc
                                #25

                                @jsulm

                                I'm getting a runtime error saying
                                QObject::connect: No such signal tetraSerial::readyRead() in ..\serialPortTest\mainwindow.cpp:54 QObject::connect: (receiver name: 'MainWindow')

                                I already configured the serial port in the tetraSerial class as shown above. What might be the cause?

                                Also
                                I'm a bit confused about where to define signals and slots. As you can see I've opened the device in my tetraSerial class in a method called serialOpen. Now I want to communicate with for example two forms. When I press Read button in form1 , the data shluld be displayed on a textbox in form 1. In form 2, when I press Write, it shluld write a file into the opened device.

                                My confusion where should I define signals and slots and where should I connect them?

                                aha_1980A 1 Reply Last reply
                                0
                                • V viniltc

                                  @jsulm

                                  I'm getting a runtime error saying
                                  QObject::connect: No such signal tetraSerial::readyRead() in ..\serialPortTest\mainwindow.cpp:54 QObject::connect: (receiver name: 'MainWindow')

                                  I already configured the serial port in the tetraSerial class as shown above. What might be the cause?

                                  Also
                                  I'm a bit confused about where to define signals and slots. As you can see I've opened the device in my tetraSerial class in a method called serialOpen. Now I want to communicate with for example two forms. When I press Read button in form1 , the data shluld be displayed on a textbox in form 1. In form 2, when I press Write, it shluld write a file into the opened device.

                                  My confusion where should I define signals and slots and where should I connect them?

                                  aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #26

                                  @viniltc

                                  My confusion where should I define signals and slots and where should I connect them?

                                  • signals: in the sender
                                  • slots: in the receiver
                                  • connect: in a class that knows both other classes or in the receiver (but not in the sender!)

                                  Qt has to stay free or it will die.

                                  1 Reply Last reply
                                  3
                                  • mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #27

                                    Hi

                                    QObject::connect: No such signal tetraSerial::readyRead() in ..\serialPortTest\mainwindow.cpp:54 QObject::connect: (receiver name: 'MainWindow')

                                    its the serialObject inside tetraSerial that has the signal so that must be used in the connect

                                    connect(serial.serialObject, SIGNAL(readyRead()), this, SLOT(serialRecived2()));

                                    Also, here the tetraSerial serial; is a local variable and will not survive
                                    long enough to get any data as it will be deleted as soon as constructor ends.
                                    it should be as a member of MainWindow

                                    tetraSerial serial; <<< im local. 
                                    serial.serialOpen();  
                                    connect(seriall.serialObject, SIGNAL(readyRead()), this, SLOT(serialRecived2()));
                                    
                                    1 Reply Last reply
                                    4

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved