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.
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:
So a reducing the packet size may help.
sendRawRequestverifies the size:Following the link to
isValid: