QModbusRequest usage issues

32 Views Asked by At

When I use QModbusRequest to send mobus data, I get a strange appearance.

    if (file.open(QIODevice::ReadOnly))
    {
        int address = 0;
        while (!file.atEnd()) {
            QByteArray data = file.read(1024);
            if (data.length() > 0) {
                QByteArray combinedData;
                combinedData.append(static_cast<char>((address >> 8) & 0xFF));
                combinedData.append(static_cast<char>(address & 0xFF));
                combinedData.append(static_cast<char>((data.length() >> 8) & 0xFF));
                combinedData.append(static_cast<char>(data.length() & 0xFF));
                combinedData.append(data);
                if (data.length() < 1024) {
                    combinedData.append(QByteArray(1024 - data.length(), '\xFF'));
                }
                SelfDataTransmission(combinedData);//*************
                address += data.length();
            }
        }
        file.close();
    }

I want to try to open a .bin file, read its contents, and send the content. So I simply created a function called "SelfDataTransmission".

void tdialogperformance::SelfDataTransmission(QByteArray Data)
{
    QModbusReply *reply = nullptr;
    reply = PerForManceDevice->sendRawRequest(QModbusRequest(QModbusRequest::FunctionCode(0x0060), Data), Serveradds);
    if (reply)
        return;
}

But when I use it, it always gives an error. Qt.modbus: (Client) Refused to send invalid request. It cannot directly substitute data into QModbusRequest (QModbusRequest :: Function Code (0x0060), Data)

But when I refer to a routine, here's what it does.

void tdialogperformance::SelfDataTransmission(QString Data)
{
    QModbusReply *reply = nullptr;
    QByteArray pduData = QByteArray::fromHex(Data.toLatin1());
    reply = PerForManceDevice->sendRawRequest(QModbusRequest(QModbusRequest::FunctionCode(0x0060), pduData), Serveradds);
    if (reply)
        return;
}

And this function is called externally SelfDataTransmission("00402004084B01000000000000"); After this, the content can be sent normally. After I debugged, I found that the content in QByteArray is also represented by Hex. I would like to know what caused this problem and how to solve it.

1

There are 1 best solutions below

0
Brits On

As per the comments OP was attempting to send a packet containing 1024 bytes.

The Modbus spec sets out the format of each packet (but uses the term PDU; protocol data unit) and states:

The size of the MODBUS PDU is limited by the size constraint inherited from the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256 bytes).
...
TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes

So a reducing the packet size may help. sendRawRequest verifies the size:

The only check performed before sending is therefore the validity check, see QModbusPdu::isValid.

Following the link to isValid:

A PDU is considered valid if the message code is in the range of 1 to 255 decimal and the PDU's compound size (function code + data) does not exceed 253 bytes.