MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / setSerialPortIndex

Method setSerialPortIndex

app/src/API/Handlers/ModbusHandler.cpp:375–401  ·  view source on GitHub ↗

* @brief Set RTU serial port by index */

Source from the content-addressed store, hash-verified

373 * @brief Set RTU serial port by index
374 */
375API::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

Callers

nothing calls this directly

Calls 5

modbusMethod · 0.80
serialPortListMethod · 0.80
containsMethod · 0.45
valueMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected