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

Method open

app/src/IO/Drivers/CANBus.cpp:249–309  ·  view source on GitHub ↗

* @brief Opens the CAN bus device with the given mode. */

Source from the content-addressed store, hash-verified

247 * @brief Opens the CAN bus device with the given mode.
248 */
249bool IO::Drivers::CANBus::open(const QIODevice::OpenMode mode)
250{
251 Q_UNUSED(mode)
252
253 close();
254
255 if (!canSupportAvailable()) {
256 showCanSupportError();
257 return false;
258 }
259
260 if (!validateOpenPreconditions())
261 return false;
262
263 QString plugin = m_pluginList.at(m_pluginIndex);
264 QString interface = m_interfaceList.at(m_interfaceIndex);
265
266 QString error;
267 if (const auto* backend = IO::Drivers::CanBackends::find(plugin))
268 m_device = backend->create(interface);
269 else
270 m_device = QCanBus::instance()->createDevice(plugin, interface, &error);
271
272 if (!m_device) {
273 Misc::Utilities::showMessageBox(
274 tr("CAN Device Creation Failed"),
275 error.isEmpty() ? tr("Unable to create CAN bus device. Check your hardware and drivers.")
276 : error,
277 QMessageBox::Critical);
278 return false;
279 }
280
281 m_device->setConfigurationParameter(QCanBusDevice::BitRateKey, m_bitrate);
282 if (m_canFD)
283 m_device->setConfigurationParameter(QCanBusDevice::CanFdKey, true);
284
285 if (m_loopback)
286 m_device->setConfigurationParameter(QCanBusDevice::LoopbackKey, true);
287
288 if (m_listenOnly)
289 m_device->setConfigurationParameter(IO::Drivers::kListenOnlyConfigKey, true);
290
291 wireCanBusSignals();
292
293 if (!m_device->connectDevice()) {
294 error = m_device->errorString();
295 m_device->deleteLater();
296 m_device = nullptr;
297 const QString base =
298 error.isEmpty()
299 ? tr("Unable to connect to CAN bus device. Check your hardware connection and settings.")
300 : error;
301 Misc::Utilities::showMessageBox(tr("CAN Connection Failed"),
302 base + connectionErrorHint(plugin, interface),
303 QMessageBox::Critical);
304 return false;
305 }
306

Callers

nothing calls this directly

Calls 4

isEmptyMethod · 0.80
connectDeviceMethod · 0.80
errorStringMethod · 0.80
createMethod · 0.45

Tested by

no test coverage detected