'How to customize QModbusDataUnit to send hex values in modbus?
I would need to customize the QModbusDataUnit
by sending custom hex via modbus, below my code, I'm trying to customize the post but I do not understand how to do it.
I should send the following values: 0x01,0x08,0x00,0x00,0x00,0x00,0xE0,0x0B
void connessione::clickButton(){
QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, 1, QVector<quint16>({0x01,0x08,0x00,0x00,0x00,0x00,0xE0,0x0B}));
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
}
}
but my response is:
D/libmodbusMobile.so(15006): (null):0 ((null)): qt.modbus: (TCP client) Sent TCP PDU: 0x0300080008 with tId: 0
where set 0x03 ? this is not right, it's not equal to my QVector send, how to solve ?
Solution 1:[1]
Try to test a QModbusDataUnit as follows:
QModbusDataUnit myUnit;
QModbusDataUnit::RegisterType myType;
myType = static_cast<QModbusDataUnit::RegisterType>(0x04);
myUnit = QModbusDataUnit(myType,0x0c,0x03);
cout << hex;
cout << myUnit.registerType() << endl;
cout << myUnit.startAddress() << endl;
Notes:
To use “cout” in a console QT application;
#include <iostream>
using namespace std;
To create a QT Modbus console application include the following in the .pro file;
QT += serialbus widgets
qtConfig(modbus-serialport): QT += serialport
TARGET = modbusserver
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |