ModbusTcpClient not working
-
Cannot get modbus to work with QT (works with other library, so no physical connection problems etc). Any idea why this does not work?
QModbusTcpClient modbusClient;
modbusClient.setConnectionParameter(QModbusDevice::NetworkAddressParameter, QVariant(QString("192.168.0.3")));
modbusClient.setConnectionParameter(QModbusDevice::NetworkPortParameter, QVariant(502));
modbusClient.connectDevice();if (!modbusClient.state() == QModbusDevice::ConnectedState) {
qDebug() << "Modbus connection failed";
}QModbusDataUnit readRequest(QModbusDataUnit::Coils, 0, 1);
modbusClient.sendReadRequest(readRequest, 1);Application output says:
qt.modbus: (Client) Device is not connected
qt.modbus: (TCP client) Connected to QHostAddress("192.168.0.3") on port 502 -
Cannot get modbus to work with QT (works with other library, so no physical connection problems etc). Any idea why this does not work?
QModbusTcpClient modbusClient;
modbusClient.setConnectionParameter(QModbusDevice::NetworkAddressParameter, QVariant(QString("192.168.0.3")));
modbusClient.setConnectionParameter(QModbusDevice::NetworkPortParameter, QVariant(502));
modbusClient.connectDevice();if (!modbusClient.state() == QModbusDevice::ConnectedState) {
qDebug() << "Modbus connection failed";
}QModbusDataUnit readRequest(QModbusDataUnit::Coils, 0, 1);
modbusClient.sendReadRequest(readRequest, 1);Application output says:
qt.modbus: (Client) Device is not connected
qt.modbus: (TCP client) Connected to QHostAddress("192.168.0.3") on port 502@Gelvis , you haven't specified if you are the slave or the master, I assume slave, is a master running at the IP 192.168.0.3 ?
I haven't use the QModbusTcpClient however I have written lots of modbus implementations both Master and Slave on various platforms, I guess because its called QMobusTcpClient it must be a slave....check the master is running and listening on port 502.
-
The physical device is running on 192.168.0.3.
I have got it working with another modbus library
(https://github.com/fz-lyu/modbuspp)
modbus mb = modbus("192.168.0.3", 502);
mb.modbus_set_slave_id(1);
mb.modbus_connect();bool read_coil;
mb.modbus_read_coils(0, 1, &read_coil); -
The physical device is running on 192.168.0.3.
I have got it working with another modbus library
(https://github.com/fz-lyu/modbuspp)
modbus mb = modbus("192.168.0.3", 502);
mb.modbus_set_slave_id(1);
mb.modbus_connect();bool read_coil;
mb.modbus_read_coils(0, 1, &read_coil);@Gelvis , I have no idea what QModbusClient is doing, but in order for a slave to talk to a master it must send a request, modbus is a very simple protocol, there is no connection except when a request is sent to the master then the master responds. In your first post I cannot see where you set the slave address of the client.
In your new post, you connect then issue a request to get the coils, this is correct and a connection is only made when the request is issued.
Move:
if (!modbusClient.state() == QModbusDevice::ConnectedState) { qDebug() << "Modbus connection failed"; }
to after:
modbusClient.sendReadRequest(readRequest, 1);
-
I do not understand QModbusTcpClient myself
I guessed the 1 in "modbusClient.sendReadRequest(readRequest, 1);" is the slave address
Tried modbusClient.setSlaveAddress(1), but that method does not exist
Moving the check made no difference
-
@Gelvis , also there are various inconsistencies in the protocol, for example in the protocol, all addresses start from 0, but 0 actually translates in the real world to 1. This is a serious gotcha.
-
Cannot get modbus to work with QT (works with other library, so no physical connection problems etc). Any idea why this does not work?
QModbusTcpClient modbusClient;
modbusClient.setConnectionParameter(QModbusDevice::NetworkAddressParameter, QVariant(QString("192.168.0.3")));
modbusClient.setConnectionParameter(QModbusDevice::NetworkPortParameter, QVariant(502));
modbusClient.connectDevice();if (!modbusClient.state() == QModbusDevice::ConnectedState) {
qDebug() << "Modbus connection failed";
}QModbusDataUnit readRequest(QModbusDataUnit::Coils, 0, 1);
modbusClient.sendReadRequest(readRequest, 1);Application output says:
qt.modbus: (Client) Device is not connected
qt.modbus: (TCP client) Connected to QHostAddress("192.168.0.3") on port 502@Gelvis I would suggest looking at the return value of
modbusClient.connectDevice();
it hopefully returns true .this one
modbusClient.sendReadRequest(readRequest, 1);
returns aQModbusReply
instance, you completely ignore that one, that is super wrong.Because you have to listen to the finished() signal of that reply! Only once that signal is emitted can you actually read the received data.
-
I did test for the modbusClient.state() == QModbusDevice::ConnectedState which gives true (connectDevice also returns true)
I also have more code reading the reply, but my point was that it will never get to that after sendReadRequest gives this error: qt.modbus: (Client) Device is not connected
(fails on first line, never gets to qDebug messages or errorString)
edit: gets to the last one obviously "Read request failed, check connection status"
// Wait for the response if (auto *reply = modbusClient.sendReadRequest(readRequest, 1)) { if (!reply->isFinished()) connect(reply, &QModbusReply::finished, this, [this, reply]() { if (reply->error() == QModbusDevice::NoError) { const QModbusDataUnit unit = reply->result(); qDebug() << "Read register value:" << unit.value(0); } else { qDebug() << "Error:" << reply->errorString(); } reply->deleteLater(); }); else reply->deleteLater(); } else { qDebug() << "Read request failed, check connection status"; }
-
@Gelvis , please explain? I suggest downloading the Modicon Modbus Protocol Specification. It’s very easy to follow and freely available.
-
I might have to. I was hoping I did not have to implement the whole thing myself for this simple task
I got this working in 5 minutes on Linux with a header file I found on github, however I have to make this for Windows for the assignment