dimanche 4 avril 2021

Modbusclient as a console application, "Stuck in Connectingstate loop"

I'm trying hardly to convert the Modbusmaster example (qt example) which is a widget application to a console application. I wanted to build a connection between a local slave and my master. The problem I'm facing is that my code is changing its state to “Connecting state” ,gets stuck and doesn't want to build a connection. That's why, I recon that the Modbus library is limited and it's only compatible with the widget form. Could someone tell me if my guesses are right. down below you will find my code:

#include <QCoreApplication>
#include <QDebug>
#include <QModbusDataUnit>
#include<iostream>
#include <QTimer>// this bib was add to the 50ms Loop check
#include <string>
#include <QString>
#include <QThread>
#include <QModbusTcpClient>
#include <QModbusDataUnit>
#include <QUrl>




int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Mainmodbus T ;
    T.modbusDevice = new QModbusTcpClient();




    if (!T.modbusDevice) qDebug()<<"error 1";


    if (T.modbusDevice->state() != QModbusDevice::ConnectedState) {


            T.modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter, "127.0.0.1");
            T.modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter, 502);

            T.modbusDevice->connectDevice();


            
            while(true){
            qDebug()<<T.modbusDevice->state();}



            }

    return a.exec();
}

enter image description here

Aucun commentaire:

Enregistrer un commentaire