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. Show low level Modbus packet in UI

Show low level Modbus packet in UI

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.6k Views
  • 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 Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 8 Nov 2018, 14:40 last edited by
    #2

    Hi
    you mean like
    http://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler
    so all QDebug goes to your own function ?

    1 Reply Last reply
    1
    • M Offline
      M Offline
      Mark81
      wrote on 8 Nov 2018, 14:50 last edited by
      #3

      It might be a solution if there's no way to retrieve the raw data (the whole packets) directly from the Modbus classes. Like the sendData and reveiceData of EasyModbus.

      I'll try it. Thanks.

      M 1 Reply Last reply 8 Nov 2018, 14:55
      0
      • M Mark81
        8 Nov 2018, 14:50

        It might be a solution if there's no way to retrieve the raw data (the whole packets) directly from the Modbus classes. Like the sendData and reveiceData of EasyModbus.

        I'll try it. Thanks.

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 8 Nov 2018, 14:55 last edited by
        #4

        @Mark81
        Hi
        I have not used the modBus so i cant say.
        Maybe Mr. @aha_1980 knows if possible.

        A 1 Reply Last reply 8 Nov 2018, 16:24
        0
        • M Offline
          M Offline
          Mark81
          wrote on 8 Nov 2018, 15:03 last edited by
          #5

          Following this method, I can put myMessageOutput inside my MainWindow class? Otherwise I don't understand how I can show the data in the UI.

          int main(int argc, char *argv[])
          {   
              QLoggingCategory::setFilterRules(QStringLiteral("qt.modbus* = true"));
          
              QApplication a(argc, argv);
          
              MainWindow w;
              qInstallMessageHandler(w.myMessageOutput);
              w.showMaximized();
          
              return a.exec();
          }
          

          it says:

          error: reference to non-static member function must be called

          but that function is not static:

          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
              void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);
          }
          
          M 1 Reply Last reply 8 Nov 2018, 15:17
          0
          • M Mark81
            8 Nov 2018, 15:03

            Following this method, I can put myMessageOutput inside my MainWindow class? Otherwise I don't understand how I can show the data in the UI.

            int main(int argc, char *argv[])
            {   
                QLoggingCategory::setFilterRules(QStringLiteral("qt.modbus* = true"));
            
                QApplication a(argc, argv);
            
                MainWindow w;
                qInstallMessageHandler(w.myMessageOutput);
                w.showMaximized();
            
                return a.exec();
            }
            

            it says:

            error: reference to non-static member function must be called

            but that function is not static:

            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
                void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);
            }
            
            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 8 Nov 2018, 15:17 last edited by
            #6

            @Mark81
            Hi
            It has to a be a global function. Cannot be a member. sadly.
            I used a global QObject class (pointer) as as proxy so i could emit from the global function.
            Not pretty but only other alternative is a file or ifstream redirection
            with cout and a custom std::streambuf child.

            1 Reply Last reply
            1
            • M Offline
              M Offline
              Mark81
              wrote on 8 Nov 2018, 15:50 last edited by
              #7

              Tried with no luck, surely due to lack of my knowledge:

              class MessageHandler: public QObject
              {
                  Q_OBJECT
                  MessageHandler() {}
              
              public:
                  static MessageHandler *instance()
                  {
                      if (!_instance) _instance = new MessageHandler;
                      return _instance;
                  }
                  static void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
                  {
                      emit messageOutput_received(type, context, msg);
                  }
              
              signals:
                  static void messageOutput_received(QtMsgType type, const QMessageLogContext &context, const QString &msg);
              
              protected:
                  static MessageHandler *_instance;
              };
              
              MessageHandler *MessageHandler::_instance = nullptr;
              
              int main(int argc, char *argv[])
              {   
                  QLoggingCategory::setFilterRules(QStringLiteral("qt.modbus* = true"));
              
                  QApplication a(argc, argv);
                  MainWindow w;
                  MessageHandler *mh = MessageHandler::instance();
                  qInstallMessageHandler(&mh->messageOutput);
                  QObject::connect(mh, &MessageHandler::messageOutput_received, &w, &MainWindow::myMessageOutput);
                  w.showMaximized();
              
                  return a.exec();
              }
              

              But it does't like the connect function:

              error: no matching function for call to 'QObject::connect(MessageHandler*&, void ()(QtMsgType, const QMessageLogContext&, const QString&), MainWindow, void (MainWindow::*)(QtMsgType, const QMessageLogContext&, const QString&))'
              QObject::connect(mh, &MessageHandler::messageOutput_received, &w, &MainWindow::myMessageOutput);
              ^

              M 1 Reply Last reply 8 Nov 2018, 15:56
              0
              • M Mark81
                8 Nov 2018, 15:50

                Tried with no luck, surely due to lack of my knowledge:

                class MessageHandler: public QObject
                {
                    Q_OBJECT
                    MessageHandler() {}
                
                public:
                    static MessageHandler *instance()
                    {
                        if (!_instance) _instance = new MessageHandler;
                        return _instance;
                    }
                    static void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
                    {
                        emit messageOutput_received(type, context, msg);
                    }
                
                signals:
                    static void messageOutput_received(QtMsgType type, const QMessageLogContext &context, const QString &msg);
                
                protected:
                    static MessageHandler *_instance;
                };
                
                MessageHandler *MessageHandler::_instance = nullptr;
                
                int main(int argc, char *argv[])
                {   
                    QLoggingCategory::setFilterRules(QStringLiteral("qt.modbus* = true"));
                
                    QApplication a(argc, argv);
                    MainWindow w;
                    MessageHandler *mh = MessageHandler::instance();
                    qInstallMessageHandler(&mh->messageOutput);
                    QObject::connect(mh, &MessageHandler::messageOutput_received, &w, &MainWindow::myMessageOutput);
                    w.showMaximized();
                
                    return a.exec();
                }
                

                But it does't like the connect function:

                error: no matching function for call to 'QObject::connect(MessageHandler*&, void ()(QtMsgType, const QMessageLogContext&, const QString&), MainWindow, void (MainWindow::*)(QtMsgType, const QMessageLogContext&, const QString&))'
                QObject::connect(mh, &MessageHandler::messageOutput_received, &w, &MainWindow::myMessageOutput);
                ^

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 8 Nov 2018, 15:56 last edited by
                #8

                Hi
                The MessageHandler should not be as a singleton ( with static)
                Just a small proxy that can be used to send in the global function.
                MessageHandler *globalMSG; // and new it in main.cpp after QApplication
                // the new function
                void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
                ...
                globalMSG->emit(xxxxx)
                }

                Globals are not pretty so maybe someone has a more clean solution !
                ( i didnt find one so i used this )

                1 Reply Last reply
                1
                • M mrjj
                  8 Nov 2018, 14:55

                  @Mark81
                  Hi
                  I have not used the modBus so i cant say.
                  Maybe Mr. @aha_1980 knows if possible.

                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 8 Nov 2018, 16:24 last edited by
                  #9

                  @mrjj said in Show low level Modbus packet in UI:

                  Maybe Mr. @aha_1980 knows if possible.

                  I'm not aware of an official way to do this. qInstallMessageHandler might indeed by a solution to that.

                  Regards

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  2
                  • M Offline
                    M Offline
                    Mark81
                    wrote on 8 Nov 2018, 17:22 last edited by
                    #10

                    Got it. It works, but a native way to retrieve the raw data will be very appreciated!

                    A 1 Reply Last reply 8 Nov 2018, 17:38
                    0
                    • M Mark81
                      8 Nov 2018, 17:22

                      Got it. It works, but a native way to retrieve the raw data will be very appreciated!

                      A Offline
                      A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on 8 Nov 2018, 17:38 last edited by
                      #11

                      @Mark81

                      Then please file a suggestion at bugreports.qt.io (and give a link to it here).

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      1

                      11/11

                      8 Nov 2018, 17:38

                      • Login

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