Qt World Summit: Register Today!

make communication using modbus

  • 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

  • Moderators

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


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

  • 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.
    # 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 += \

    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;

  • Lifetime Qt Champion

    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.


  • 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

  • Lifetime Qt Champion


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

    so please show your main.cpp.

  • @aha_1980
    I have posted it in my first post

  • 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.

Log in to reply