* @brief Set RTU serial port by index */
| 373 | * @brief Set RTU serial port by index |
| 374 | */ |
| 375 | API::CommandResponse API::Handlers::ModbusHandler::setSerialPortIndex(const QString& id, |
| 376 | const QJsonObject& params) |
| 377 | { |
| 378 | if (!params.contains(QStringLiteral("portIndex"))) { |
| 379 | return CommandResponse::makeError( |
| 380 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: portIndex")); |
| 381 | } |
| 382 | |
| 383 | const int portIndex = params.value(QStringLiteral("portIndex")).toInt(); |
| 384 | auto* modbus = IO::ConnectionManager::instance().modbus(); |
| 385 | const auto& portList = modbus->serialPortList(); |
| 386 | |
| 387 | if (portIndex < 0 || portIndex >= portList.count()) { |
| 388 | return CommandResponse::makeError(id, |
| 389 | ErrorCode::InvalidParam, |
| 390 | QStringLiteral("Invalid portIndex: %1. Valid range: 0-%2") |
| 391 | .arg(portIndex) |
| 392 | .arg(portList.count() - 1)); |
| 393 | } |
| 394 | |
| 395 | modbus->setSerialPortIndex(static_cast<quint8>(portIndex)); |
| 396 | |
| 397 | QJsonObject result; |
| 398 | result[QStringLiteral("portIndex")] = portIndex; |
| 399 | result[QStringLiteral("portName")] = portList.at(portIndex); |
| 400 | return CommandResponse::makeSuccess(id, result); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * @brief Set RTU baud rate |
nothing calls this directly
no test coverage detected