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. Reading continuous from Console in qt5
Forum Updated to NodeBB v4.3 + New Features

Reading continuous from Console in qt5

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 6 Posters 2.5k 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.
  • W Offline
    W Offline
    Wasee
    wrote on 3 Nov 2021, 06:12 last edited by
    #1

    Dear all;
    I need to read from console in qt5. Basically I need to implement it behind the push-button when I will press the button My QT application start reading when I need to stop it then it should be stop. Any help please guide me in right way how it is possible?

    Thanks in Advance!

    J 1 Reply Last reply 3 Nov 2021, 06:46
    0
    • W Wasee
      3 Nov 2021, 06:12

      Dear all;
      I need to read from console in qt5. Basically I need to implement it behind the push-button when I will press the button My QT application start reading when I need to stop it then it should be stop. Any help please guide me in right way how it is possible?

      Thanks in Advance!

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 3 Nov 2021, 06:46 last edited by jsulm 11 Mar 2021, 06:46
      #2

      @Wasee See https://doc.qt.io/qt-5/qtextstream.html

      QTextStream stream(stdin);
      QString line;
      while (stream.readLineInto(&line)) {
          ...
      }
      

      But keep in mind: this is blocking the event loop!
      You can put this code into a thread to not to block main thread (https://doc.qt.io/qt-5/qthread.html).

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

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on 3 Nov 2021, 06:57 last edited by
        #3

        @Wasee How does your GUI program, with its push button, have a "console" to read from?

        1 Reply Last reply
        1
        • W Offline
          W Offline
          Wasee
          wrote on 3 Nov 2021, 07:17 last edited by
          #4

          @ChrisW67 Hi;
          Yes I am printing messages in console which is reading from hardware. Now i want to reading printed messages in my console from hardware which basically are the MAC address of wifi devices present in my building. Now I want to read these MAC address in qt from console.
          Thanks

          J 1 Reply Last reply 3 Nov 2021, 07:20
          0
          • W Wasee
            3 Nov 2021, 07:17

            @ChrisW67 Hi;
            Yes I am printing messages in console which is reading from hardware. Now i want to reading printed messages in my console from hardware which basically are the MAC address of wifi devices present in my building. Now I want to read these MAC address in qt from console.
            Thanks

            J Offline
            J Offline
            JonB
            wrote on 3 Nov 2021, 07:20 last edited by JonB 11 Mar 2021, 07:22
            #5

            @Wasee
            Still don't understand. Yes from a UI program you can write to a console, say for debug messages. But you cannot "read back" printed text from that console, if that is what you mean.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              Wasee
              wrote on 3 Nov 2021, 07:33 last edited by
              #6

              @JonB Hi;
              Thanks for your help! My GUI will run in start up of the system. After running up my system I will press the push button in my GUI, after that my GUI will read from console output. How it is possible in easy way?

              J K 2 Replies Last reply 3 Nov 2021, 07:34
              0
              • W Wasee
                3 Nov 2021, 07:33

                @JonB Hi;
                Thanks for your help! My GUI will run in start up of the system. After running up my system I will press the push button in my GUI, after that my GUI will read from console output. How it is possible in easy way?

                J Offline
                J Offline
                JonB
                wrote on 3 Nov 2021, 07:34 last edited by JonB 11 Mar 2021, 07:43
                #7

                @Wasee said in Reading continuous from Console in qt5:

                will read from console output

                It can't.

                I can only guess what you mean here. If you have some device which is outputting messages which you see in e.g. the Application Output pane while running your Qt application in the debugger, that would indicate the device is sending to either standard output or standard error. If you wish to capture those messages in your app, you would need it to redirect stdout/err from within the app so that they arrive into the program instead of going to the console. But I do not know if that is the situation or what you mean.

                1 Reply Last reply
                0
                • W Wasee
                  3 Nov 2021, 07:33

                  @JonB Hi;
                  Thanks for your help! My GUI will run in start up of the system. After running up my system I will press the push button in my GUI, after that my GUI will read from console output. How it is possible in easy way?

                  K Offline
                  K Offline
                  KroMignon
                  wrote on 3 Nov 2021, 07:53 last edited by KroMignon 11 Mar 2021, 07:53
                  #8

                  @Wasee said in Reading continuous from Console in qt5:

                  Thanks for your help! My GUI will run in start up of the system. After running up my system I will press the push button in my GUI, after that my GUI will read from console output. How it is possible in easy way?

                  Your application has 3 standard I/O:

                  • stdin: console input (read only)
                  • stdout: console output (write only)
                  • stderr: error output (write only)

                  So you can use QTextStream or QDataStream with stdin and check if there is something to read (cf. post from @jsulm) and write to stdout.

                  Here a mean less code example:

                  int main(int argc, char *argv[])
                  {
                      QCoreApplication app(argc, argv);
                  
                     QTextStream in(stdin);
                     QTextStream out(stdout);
                  
                     QTimer tmr;
                     tmr.setSingleshot(false);
                     tmr.setInterval(500);
                     QObject::connect(&tmr, &QTimer::timeout, [&in, &out]() {
                         if(!in.atEnd()) {
                             const auto data = in.readAll();
                             out << "recieved: " << data;
                         }
                     });
                     tmr.start();
                     return app.exec();
                  }
                  

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 3 Nov 2021, 20:30 last edited by
                    #9

                    Hi,

                    Do you mean you want to read the output of some application that returns the information you want ?

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

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      Wasee
                      wrote on 4 Nov 2021, 03:01 last edited by
                      #10

                      @SGaist Hi;
                      Thanks for your reply! I design the control of a system in qtcreator. My system returns the acknowledgement to my GUI. Now I want to add new feature in my GUI application, for which I need to read the output of my system which is currently I am seeing in serial monitor putty. Now I need to read this serial monitor output in my GUI behind the button when I needed.
                      Thanks

                      J J 2 Replies Last reply 4 Nov 2021, 07:10
                      0
                      • W Wasee
                        4 Nov 2021, 03:01

                        @SGaist Hi;
                        Thanks for your reply! I design the control of a system in qtcreator. My system returns the acknowledgement to my GUI. Now I want to add new feature in my GUI application, for which I need to read the output of my system which is currently I am seeing in serial monitor putty. Now I need to read this serial monitor output in my GUI behind the button when I needed.
                        Thanks

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 4 Nov 2021, 07:10 last edited by
                        #11

                        @Wasee said in Reading continuous from Console in qt5:

                        read the output of my system which is currently I am seeing in serial monitor putty

                        Yre you talking about reading from a serial port?
                        Please explain clearly what you want to do.

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

                        1 Reply Last reply
                        0
                        • W Wasee
                          4 Nov 2021, 03:01

                          @SGaist Hi;
                          Thanks for your reply! I design the control of a system in qtcreator. My system returns the acknowledgement to my GUI. Now I want to add new feature in my GUI application, for which I need to read the output of my system which is currently I am seeing in serial monitor putty. Now I need to read this serial monitor output in my GUI behind the button when I needed.
                          Thanks

                          J Offline
                          J Offline
                          JonB
                          wrote on 4 Nov 2021, 07:11 last edited by JonB 11 Apr 2021, 07:24
                          #12

                          @Wasee
                          I'm afraid this does not make it any clearer.

                          I design the control of a system in qtcreator.

                          We understand this. You are designing a Qt application there.

                          My system returns the acknowledgement to my GUI.

                          We do not know what this actually means. Is your system running as a library linked into your application's code, or is it running as a separate, external process, perhaps invoked via Qt's QProcess?

                          read the output of my system which is currently I am seeing in serial monitor putty. Now I need to read this serial monitor output in my GUI

                          So "serial monitor putty" means running the putty as an external command, to read what is on the serial port and output it on stdout/err to the terminal? And if that is so, do you need to use putty at all from your Qt application, would you be happy/better reading from that serial port with Qt serial code?

                          We need to understand clearly whether you mean you have to have your application read the output from another process (putty), which seems to be what you say, or whether you mean your application can read the output directly, from the serial port, which seems simpler and preferable?

                          One thing you cannot do, if this is what you have in mind, is: run the putty process from your Qt app, have that come up in its own terminal/console window so you can see it, have it send its output there like you are used to seeing, and then at the same time have your Qt application capture the output you see into your Qt application for further processing. You can, however, have your application receive the output --- whether directly or via redirecting the putty output --- and choose to show it in its own window if you want to see it too. However, as said earlier, it sounds to me like you do not need putty at all if your own application does the talking to the serial port directly.

                          1 Reply Last reply
                          0
                          • W Offline
                            W Offline
                            Wasee
                            wrote on 4 Nov 2021, 07:32 last edited by
                            #13

                            @JonB Hi;
                            Thanks your help! Make it simple please tell me how we save terminal output to .txt file and read it back continuously?

                            J 1 Reply Last reply 4 Nov 2021, 07:37
                            0
                            • W Wasee
                              4 Nov 2021, 07:32

                              @JonB Hi;
                              Thanks your help! Make it simple please tell me how we save terminal output to .txt file and read it back continuously?

                              J Offline
                              J Offline
                              JonB
                              wrote on 4 Nov 2021, 07:37 last edited by JonB 11 Apr 2021, 07:46
                              #14

                              @Wasee said in Reading continuous from Console in qt5:

                              save terminal output to .txt file

                              Like I said, you cannot. Once output has gone to a terminal, say via an external process's output, it has gone there, and cannot be recaptured by your Qt application. That's what we are trying to tell you.

                              Which is why we asking you what is going on. If you are trying to read from and write to a serial port, do not use putty or anything else, do it directly from your Qt program. Then you can do what you like with the output received, including saving it to a text file. Why do you need to run putty at all?

                              Have you seen that Qt already has Qt Serial Port?

                              W 1 Reply Last reply 4 Nov 2021, 09:05
                              1
                              • J JonB
                                4 Nov 2021, 07:37

                                @Wasee said in Reading continuous from Console in qt5:

                                save terminal output to .txt file

                                Like I said, you cannot. Once output has gone to a terminal, say via an external process's output, it has gone there, and cannot be recaptured by your Qt application. That's what we are trying to tell you.

                                Which is why we asking you what is going on. If you are trying to read from and write to a serial port, do not use putty or anything else, do it directly from your Qt program. Then you can do what you like with the output received, including saving it to a text file. Why do you need to run putty at all?

                                Have you seen that Qt already has Qt Serial Port?

                                W Offline
                                W Offline
                                Wasee
                                wrote on 4 Nov 2021, 09:05 last edited by
                                #15

                                @JonB Hi;
                                I am just using putty to check output of my system at serial port not need it anymore. My system giving me MAC address at serial port continuously that the reason I want to read it from serial port or to store them in Qfile and read it back in my qt application that's all.

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 4 Nov 2021, 09:09 last edited by
                                  #16

                                  Then why not just use QSerialPort to read the serial port directly ?

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

                                  J 1 Reply Last reply 4 Nov 2021, 09:13
                                  2
                                  • SGaistS SGaist
                                    4 Nov 2021, 09:09

                                    Then why not just use QSerialPort to read the serial port directly ?

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 4 Nov 2021, 09:13 last edited by
                                    #17

                                    @SGaist I have tried to ask/suggest that to the OP several times now....

                                    1 Reply Last reply
                                    0

                                    1/17

                                    3 Nov 2021, 06:12

                                    • Login

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