* @brief Set Modbus protocol (RTU or TCP) */
| 238 | * @brief Set Modbus protocol (RTU or TCP) |
| 239 | */ |
| 240 | API::CommandResponse API::Handlers::ModbusHandler::setProtocolIndex(const QString& id, |
| 241 | const QJsonObject& params) |
| 242 | { |
| 243 | if (!params.contains(QStringLiteral("protocolIndex"))) { |
| 244 | return CommandResponse::makeError( |
| 245 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: protocolIndex")); |
| 246 | } |
| 247 | |
| 248 | const int protocolIndex = params.value(QStringLiteral("protocolIndex")).toInt(); |
| 249 | auto* modbus = IO::ConnectionManager::instance().modbus(); |
| 250 | const auto& protocolList = modbus->protocolList(); |
| 251 | |
| 252 | if (protocolIndex < 0 || protocolIndex >= protocolList.count()) { |
| 253 | return CommandResponse::makeError(id, |
| 254 | ErrorCode::InvalidParam, |
| 255 | QStringLiteral("Invalid protocolIndex: %1. Valid range: 0-%2") |
| 256 | .arg(protocolIndex) |
| 257 | .arg(protocolList.count() - 1)); |
| 258 | } |
| 259 | |
| 260 | modbus->setProtocolIndex(static_cast<quint8>(protocolIndex)); |
| 261 | |
| 262 | QJsonObject result; |
| 263 | result[QStringLiteral("protocolIndex")] = protocolIndex; |
| 264 | result[QStringLiteral("protocolName")] = protocolList.at(protocolIndex); |
| 265 | return CommandResponse::makeSuccess(id, result); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @brief Set Modbus slave address |
nothing calls this directly
no test coverage detected