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

Method setProtocolIndex

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

* @brief Set Modbus protocol (RTU or TCP) */

Source from the content-addressed store, hash-verified

238 * @brief Set Modbus protocol (RTU or TCP)
239 */
240API::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

Callers

nothing calls this directly

Calls 5

modbusMethod · 0.80
protocolListMethod · 0.80
containsMethod · 0.45
valueMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected