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 share Serial port object between two classes
Forum Updated to NodeBB v4.3 + New Features

How to share Serial port object between two classes

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 6 Posters 3.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.
  • S Offline
    S Offline
    saurabh162
    wrote on last edited by
    #5

    Hello mrjj and MrShawn,

    signals and slot approach has solved my problem cleanly.

    Thank you very much for help :)

    kind regards
    Saurabh Jain

    1 Reply Last reply
    3
    • S Offline
      S Offline
      saurabh162
      wrote on last edited by
      #6

      Hello Developers,

      As mentioned above I was to send command using signals and slot approach to main window.

      Now I want also to read data received from serial port in MainWindow from Dialog box.

      I have read that signal and slot approach is not good to get data or return values. So can you please guide me what can I do ?

      thanks you very much :)

      mrjjM jsulmJ aha_1980A 3 Replies Last reply
      0
      • S saurabh162

        Hello Developers,

        As mentioned above I was to send command using signals and slot approach to main window.

        Now I want also to read data received from serial port in MainWindow from Dialog box.

        I have read that signal and slot approach is not good to get data or return values. So can you please guide me what can I do ?

        thanks you very much :)

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #7

        @saurabh162
        Hi
        You mean after serial have read all in main, you want to
        send it to a dialog you open ?
        Or do you mean that dialog all reads from serial while data is coming in ?

        S 1 Reply Last reply
        0
        • S saurabh162

          Hello Developers,

          As mentioned above I was to send command using signals and slot approach to main window.

          Now I want also to read data received from serial port in MainWindow from Dialog box.

          I have read that signal and slot approach is not good to get data or return values. So can you please guide me what can I do ?

          thanks you very much :)

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

          @saurabh162 Add a getter method to your dialog:

          Dialog dialog(this);
          dialog.exec();
          MyData data = dialog.data();
          

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

          S 1 Reply Last reply
          0
          • mrjjM mrjj

            @saurabh162
            Hi
            You mean after serial have read all in main, you want to
            send it to a dialog you open ?
            Or do you mean that dialog all reads from serial while data is coming in ?

            S Offline
            S Offline
            saurabh162
            wrote on last edited by
            #9

            @mrjj

            Hello Mrji,

            thank you for fast reply

            Yes, I my application data received from serial port is first stored in buffer in Main Window. I want read this saved data from Dialog box.

            In dialog box I am sending serial port commands to Main Window using signal and slot. But do not know what is right way to read serial data saved in Main window from Dialog Box.

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @saurabh162 Add a getter method to your dialog:

              Dialog dialog(this);
              dialog.exec();
              MyData data = dialog.data();
              
              S Offline
              S Offline
              saurabh162
              wrote on last edited by
              #10

              @jsulm

              Hello jsulm

              Thank you for your fast response.

              I am sorry but i could not understand your solution. I have serial data collected in Main window. This i want to read inside Dialog box.

              So can you please explain your answer in little details

              Thank you :)

              jsulmJ 1 Reply Last reply
              0
              • S saurabh162

                Hello Developers,

                As mentioned above I was to send command using signals and slot approach to main window.

                Now I want also to read data received from serial port in MainWindow from Dialog box.

                I have read that signal and slot approach is not good to get data or return values. So can you please guide me what can I do ?

                thanks you very much :)

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

                @saurabh162 said in How to share Serial port object between two classes:

                I have read that signal and slot approach is not good to get data or return values. So can you please guide me what can I do ?

                Where have you read this? You can surely use signals&slots to exchange data. That's what signals&slots are all about :)

                Qt has to stay free or it will die.

                S 1 Reply Last reply
                2
                • S saurabh162

                  @jsulm

                  Hello jsulm

                  Thank you for your fast response.

                  I am sorry but i could not understand your solution. I have serial data collected in Main window. This i want to read inside Dialog box.

                  So can you please explain your answer in little details

                  Thank you :)

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

                  @saurabh162 You should explain better what you want to do.
                  Pass the data from main window to dialog via constructor:

                  Dialog dialog(this, mySerialData);
                  dialog.exec();
                  

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

                  1 Reply Last reply
                  0
                  • aha_1980A aha_1980

                    @saurabh162 said in How to share Serial port object between two classes:

                    I have read that signal and slot approach is not good to get data or return values. So can you please guide me what can I do ?

                    Where have you read this? You can surely use signals&slots to exchange data. That's what signals&slots are all about :)

                    S Offline
                    S Offline
                    saurabh162
                    wrote on last edited by
                    #13

                    @aha_1980

                    Thank you aha for reply

                    I have read it in following posts

                    https://forum.qt.io/topic/11000/getting-a-return-value-from-an-emitted-signal/3

                    https://stackoverflow.com/questions/5842124/can-qt-signals-return-a-value

                    According to above posts we should be cautious when we are returning data from a slot.

                    Additionally I have tried it and got segmentation fault. Following are the steps I have taken.

                    1. I have a array " char receive_buffer[RBSIZE]" declared in Main window class with private access. This buffer is always filled with new data when we send serial command from Dialog Box using signal and slot.
                    2. In order to read this buffer, I declared char pointer in my Dialog Box and intialize this pointer to address of "receive_buffer" in Serial communication slot in Main Window.
                    3. After this when I try to read data in receive_buffer[RBSIZE] from Dialog Box using char pointer I get segementation fault.

                    Please let me know if you need any other information.

                    thank you :)

                    aha_1980A 1 Reply Last reply
                    0
                    • S saurabh162

                      @aha_1980

                      Thank you aha for reply

                      I have read it in following posts

                      https://forum.qt.io/topic/11000/getting-a-return-value-from-an-emitted-signal/3

                      https://stackoverflow.com/questions/5842124/can-qt-signals-return-a-value

                      According to above posts we should be cautious when we are returning data from a slot.

                      Additionally I have tried it and got segmentation fault. Following are the steps I have taken.

                      1. I have a array " char receive_buffer[RBSIZE]" declared in Main window class with private access. This buffer is always filled with new data when we send serial command from Dialog Box using signal and slot.
                      2. In order to read this buffer, I declared char pointer in my Dialog Box and intialize this pointer to address of "receive_buffer" in Serial communication slot in Main Window.
                      3. After this when I try to read data in receive_buffer[RBSIZE] from Dialog Box using char pointer I get segementation fault.

                      Please let me know if you need any other information.

                      thank you :)

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

                      @saurabh162

                      https://forum.qt.io/topic/11000/getting-a-return-value-from-an-emitted-signal/3

                      Yes, thats bad design. But you don't need to do it like this. You can emit another signal from your dialog when data is ready, and connect it to a slot in your main window.

                      Regards

                      Qt has to stay free or it will die.

                      S 1 Reply Last reply
                      2
                      • aha_1980A aha_1980

                        @saurabh162

                        https://forum.qt.io/topic/11000/getting-a-return-value-from-an-emitted-signal/3

                        Yes, thats bad design. But you don't need to do it like this. You can emit another signal from your dialog when data is ready, and connect it to a slot in your main window.

                        Regards

                        S Offline
                        S Offline
                        saurabh162
                        wrote on last edited by
                        #15

                        @aha_1980

                        ok aha_1980 I try this and will update you ..thanks :)

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          saurabh162
                          wrote on last edited by
                          #16

                          hello aha_1980,

                          Thank you very much it worked.

                          I have taken following steps to achieve it. It is working but I also want to ensure whether it is correct way of programming.

                          1. I have created signal (in Dialog Box) and slot (in main window) with same name
                            "void ReadSerialBuffer(void);"

                          2. Declared a array char serialReceivebuffer[RBSIZE]; in dialog box. Additionally a public function "UpdateSerialReadBuffer"in Dialog box to copy data from source buffer to serialReceivebuffer in Dialog Box.

                          3. After this I defined slot in main window which is calling "UpdateSerialReadBuffer" function of dialog box to copy data from buffer in Main window to buffer in Dialog box.

                          4. In end I connected both signal and slot using "connect" method

                          Thank you very much :)

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

                            @saurabh162

                            I also came across a similar situation. I'm a beginner of Qt and c++. It will be very helpful if you could share a sample code to communicate the serial port object in two forms.

                            many thanks

                            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