PDA

View Full Version : Qt Modbus Slave Example



qAlexey
25th July 2017, 19:36
Good day, Qt users !
Im working with Qt Modbus Slave Example, and I have a question.

When we are creating a Map for modbus we use construction like :


reg.insert(QModbusDataUnit::InputRegisters, { QModbusDataUnit::InputRegisters, 0, 10 });
// ^start address ^map size


The question is :
When I use just the code from example:
reg.insert(QModbusDataUnit::InputRegisters, { QModbusDataUnit::InputRegisters, 0, 10 });
10 Input registers are created from 0 address. No issues.
But when I want to start from different address lets say from 5, then I write :

reg.insert(QModbusDataUnit::InputRegisters, { QModbusDataUnit::InputRegisters, 5, 10 });
Then when I check and I can only read registrers 5-10, and 10-15 registers gives "Byte missing error" in my test program,

My qt server responds:
qt.modbus: (TCP server) Response PDU: 0x041400000000000000000000


Well, basically I can create 10 registers starting from 5 by using :

reg.insert(QModbusDataUnit::InputRegisters, { QModbusDataUnit::InputRegisters, 5, 15 });
But then then still 5 registers after 15 will respond me "Byte missing error";

So to conclude :
1. I create 10 registers from 0 to 10;
No issues, Map of 10 with 10 entries, map full;
2. I create 10 registers from 5 to 15
Map of 15 filled with 10 registers from 5 and 5 empty registers in the end.
If I will move the start position ( 5 ) the quantity of empty registers( returning "Byte missing error") will rise accordingly;

If you have any thoughts about it, I will appreciate it.


Part of code from Qt Modbus Slave Example:



void MainWindow::on_connectType_currentIndexChanged(int index)
{
if (modbusDevice) {
modbusDevice->disconnect();
delete modbusDevice;
modbusDevice = nullptr;
}

ModbusConnection type = static_cast<ModbusConnection> (index);
if (type == Serial) {
modbusDevice = new QModbusRtuSerialSlave(this);
} else if (type == Tcp) {
modbusDevice = new QModbusTcpServer(this);
if (ui->portEdit->text().isEmpty())
ui->portEdit->setText(QLatin1Literal("127.0.0.1:502"));
}
ui->listenOnlyBox->setEnabled(type == Serial);

if (!modbusDevice) {
ui->connectButton->setDisabled(true);
if (type == Serial)
statusBar()->showMessage(tr("Could not create Modbus slave."), 5000);
else
statusBar()->showMessage(tr("Could not create Modbus server."), 5000);
} else {
QModbusDataUnitMap reg;
reg.insert(QModbusDataUnit::Coils, { QModbusDataUnit::Coils, 0, 10 });
reg.insert(QModbusDataUnit::DiscreteInputs, { QModbusDataUnit::DiscreteInputs, 0, 10 });
reg.insert(QModbusDataUnit::InputRegisters, { QModbusDataUnit::InputRegisters, 0, 10 });
reg.insert(QModbusDataUnit::HoldingRegisters, { QModbusDataUnit::HoldingRegisters, 0,10 });

modbusDevice->setMap(reg);

connect(modbusDevice, &QModbusServer::dataWritten,
this, &MainWindow::updateWidgets);
connect(modbusDevice, &QModbusServer::stateChanged,
this, &MainWindow::onStateChanged);
connect(modbusDevice, &QModbusServer::errorOccurred,
this, &MainWindow::handleDeviceError);

connect(ui->listenOnlyBox, &QCheckBox::toggled, this, [this](bool toggled) {
if (modbusDevice)
modbusDevice->setValue(QModbusServer::ListenOnlyMode, toggled);
});
emit ui->listenOnlyBox->toggled(ui->listenOnlyBox->isChecked());
connect(ui->setBusyBox, &QCheckBox::toggled, this, [this](bool toggled) {
if (modbusDevice)
modbusDevice->setValue(QModbusServer::DeviceBusy, toggled ? 0xffff : 0x0000);
});
emit ui->setBusyBox->toggled(ui->setBusyBox->isChecked());

setupDeviceData();
}
}