Qt Forum

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

    Solved make communication using modbus

    General and Desktop
    4
    8
    852
    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.
    • R
      rezaMSLM last edited by

      hi;
      i want to write a simple program to make communication between PC and some slave devices using modbus via serial port. but since i'm a newbie i dont know how to start it!.
      I think that first I have to create an object and then give it the required parameters and the open the port and send commands and receive the answers.(is that right?)
      for first step I think i have to create an object of QModbusDevice but I receive error:

      #include <QCoreApplication>
      #include <QModbusRtuSerialMaster>
      #include <QModbusClient>
      #include <QModbusDevice>
      #include <QModbusServer>
      #include <QModbusDataUnit>
      #include <QModbusReply>
      
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          QModbusDevice my_ModBus_Object;
          return a.exec();
      }
      

      please help and guide me

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

        @rezaMSLM
        have you seen jet the Modbus Example that comes with qt?

        https://doc.qt.io/qt-5/qtserialbus-modbus-master-example.html

        and to your error, without seeing the actual error, my guess is, you did not add QT += serialport serialbus in your *.pro file

        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
        • R
          rezaMSLM last edited by

          my .pro file:

          QT -= gui
          QT += serialbus core serialport
          
          CONFIG += c++11 console
          CONFIG -= app_bundle
          
          # The following define makes your compiler emit warnings if you use
          # any feature of Qt which as been marked deprecated (the exact warnings
          # depend on your compiler). Please consult the documentation of the
          # deprecated API in order to know how to port your code away from it.
          DEFINES += QT_DEPRECATED_WARNINGS
          
          # You can also make your code fail to compile if you use deprecated APIs.
          # In order to do so, uncomment the following line.
          # You can also select to disable deprecated APIs only up to a certain version of Qt.
          #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
          
          SOURCES += \
                  main.cpp
          

          that example is too hard for me
          I'm a newbie and I got confused when opened that example
          I want to write my own and very simple program for start but i need help to do that

          here is the error:

          D:\Projects\Qt_ProjectFolder\ModbusTest\main.cpp:13: error: cannot declare variable 'my_ModBus_Object' to be of abstract type 'QModbusDevice'
               QModbusDevice my_ModBus_Object;
                             ^
          
          aha_1980 1 Reply Last reply Reply Quote 0
          • aha_1980
            aha_1980 Lifetime Qt Champion @rezaMSLM last edited by

            Hi @rezaMSLM,

            can you show your main.cpp file regarding the error?

            that example is too hard for me
            I want to write my own and very simple program for start but i need help to do that

            Sorry to tell you, but for an advanced topic as Modbus you'll need some Qt basics first.

            You'll at least need a Qt event loop running to let the communication run.

            I'd suggest you to get a feeling for the mainwindow.cpp of that example. The function names directly correspond to user interface actions, so the main structure should be easy to get. Then try to understand e.g. what happens on a connection.

            Regards

            Qt has to stay free or it will die.

            1 Reply Last reply Reply Quote 2
            • R
              rezaMSLM last edited by

              my project is a console application and has no mainwindow.cpp file.
              for first step i just want to build an object, this shouldn't be very complex. but i receive error

              aha_1980 1 Reply Last reply Reply Quote 0
              • aha_1980
                aha_1980 Lifetime Qt Champion @rezaMSLM last edited by

                @rezaMSLM

                how should we help you if you don't provide the needed infos?

                so please show your main.cpp.

                Qt has to stay free or it will die.

                R 1 Reply Last reply Reply Quote 0
                • R
                  rezaMSLM @aha_1980 last edited by

                  @aha_1980
                  I have posted it in my first post

                  1 Reply Last reply Reply Quote 0
                  • mranger90
                    mranger90 last edited by

                    That is because QModbusDevice is an abstract base class.
                    You can not instantiate an instance of such.
                    You should try to instantiate either a QModbusServer or QModbusClient.

                    1 Reply Last reply Reply Quote 4
                    • First post
                      Last post