1

我必须发送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):(空):0((空)):qt.modbus:(TCP客户端)发送的TCP PDU:0x0300000001,tId:2

这是正确的:0x0300000001

但不幸的是,我的电子卡,我无法修改的集成固件,是正确的Modbus RTU,所以我应该改为0x0300000001C10x010300000001C1C2和 C2 是校验和。

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

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

谢谢

4

0 回答 0