使用QT - QModbusTcpClient将“Modbus TCP”修改为“Modbus RTU”

时间:2017-06-23 10:14:52

标签: c++ qt tcp modbus modbus-tcp

我必须发送Modbus特定数据的请求,我的问题是我必须使用移动通信,wifi,连接到自定义电子卡,这是在Modbus RTU

我的工作代码连接到电子板:

#include "connessione.h"
#include <QModbusTcpClient>
#include <QVariant>
#include <QModbusDataUnit>
#include <QDebug>

connessione::connessione(QObject *parent) : QObject(parent)
{
    qDebug() << "here debug " << "ok";

    clientX = new QModbusTcpClient();
    clientX->setConnectionParameter(QModbusDevice::NetworkAddressParameter, "192.168.222.1");
    clientX->setConnectionParameter(QModbusDevice::NetworkPortParameter, 5555);

    if (clientX->connectDevice())
    {
        qDebug()  << "connected: " << clientX->state();
    }
    else
    {
        qDebug()  << "ERRORE" << clientX->errorString();
    }
}

void connessione::clickButton(){

    QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, 0, 1); // just read input register 40006
    //qDebug() << "readUnit" << readUnit.RegisterType;
    qDebug() << "readUnit" << clientX->state();


    if (auto *reply = clientX->sendReadRequest(readUnit, 255)) // client id 255
    {
        if (!reply->isFinished())
        {
            // connect the finished signal of the request to your read slot
            qDebug() << "connected" << reply->errorString();
            connect(reply, &QModbusReply::finished, this, &connessione::readReady);
        }
        else
        {
            qDebug() << "Errore" << reply->errorString();
            delete reply; // broadcast replies return immediately
        }
    }
    else
    {
        qDebug() << "Errore" << reply->errorString();
        // request error
    }
}

void connessione::readReady()
{
    QModbusReply *reply = qobject_cast<QModbusReply *>(sender());
    if (!reply)
        return;

    if (reply->error() == QModbusDevice::NoError)
    {
        const QModbusDataUnit unit = reply->result();
        int startAddress = unit.startAddress(); // the start address,
        int value = unit.value(0); // value of the start address + 0

        qDebug() << "NESSUN ERRORE" << reply->errorString();
    }
    else
    {
        qDebug() << "Errore readReady" << reply->errorString();
        // reply error
    }

    reply->deleteLater(); // delete the reply
}

日志字符串TCP发送:

  

D / libmodbusMobile.so(8042):( null):0((null)):qt.modbus:(TCP   client)发送TCP PDU:0x0300000001,tId:2

这是对的: 0x0300000001

但遗憾的是,我的电子卡(我无法修改的集成固件)与Modbus RTU一致,因此我应将0x0300000001更改为0x010300000001C1C2,其中C1和C2是校验和。

我相信 QModbusDataUnit 会生成要发送的缓冲区。那么如何改变呢?在我构建缓冲区时是否存在手动解决方案?

如何更改它并像示例一样创建自定义发送缓冲区?

由于

0 个答案:

没有答案