Thanks for the answers, I found the connection problem. I added the part:
void MainWindow::on_connectType_currentIndexChanged(int index)
{
if (modbusDevice) {
modbusDevice->disconnectDevice();
delete modbusDevice;
modbusDevice = nullptr;
}
auto type = static_cast<ModbusConnection> (index);
if (type == Serial) {
modbusDevice = new QModbusRtuSerialMaster(this);
} else if (type == Tcp) {
modbusDevice = new QModbusTcpClient(this);
if (ui->portEdit->text().isEmpty())
ui->portEdit->setText(QLatin1Literal("127.0.0.1:502"));
}
connect(modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error) {
statusBar()->showMessage(modbusDevice->errorString(), 5000);
});
if (!modbusDevice) {
ui->connectButton->setDisabled(true);
if (type == Serial)
statusBar()->showMessage(tr("Could not create Modbus master."), 5000);
else
statusBar()->showMessage(tr("Could not create Modbus client."), 5000);
} else {
connect(modbusDevice, &QModbusClient::stateChanged,
this, &MainWindow::onStateChanged);
}
}
And now I can make a connection.
Now I have to test it on the device, but i can't access it for a while, to be continued