* @brief Opens the serial port and starts the slcan channel at the configured bitrate. */
| 160 | * @brief Opens the serial port and starts the slcan channel at the configured bitrate. |
| 161 | */ |
| 162 | bool IO::Drivers::SlcanBackend::open() |
| 163 | { |
| 164 | const auto requested = configurationParameter(QCanBusDevice::BitRateKey).toUInt(); |
| 165 | const int index = slcanBitrateIndex(requested == 0 ? 500000 : requested); |
| 166 | if (index < 0) { |
| 167 | setError(tr("The bitrate %1 bps is not a standard slcan rate.").arg(requested), |
| 168 | QCanBusDevice::ConfigurationError); |
| 169 | setState(QCanBusDevice::UnconnectedState); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | m_port = new QSerialPort(m_portName, this); |
| 174 | m_port->setBaudRate(kSlcanBaudRate); |
| 175 | |
| 176 | if (!m_port->open(QIODevice::ReadWrite)) { |
| 177 | setError(tr("Could not open serial port %1: %2").arg(m_portName, m_port->errorString()), |
| 178 | QCanBusDevice::ConnectionError); |
| 179 | m_port->deleteLater(); |
| 180 | m_port = nullptr; |
| 181 | setState(QCanBusDevice::UnconnectedState); |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | connect(m_port, &QSerialPort::readyRead, this, &SlcanBackend::onReadyRead); |
| 186 | |
| 187 | (void)sendCommand(QByteArrayLiteral("C\r")); |
| 188 | (void)sendCommand("S" + QByteArray::number(index) + "\r"); |
| 189 | if (!sendCommand(QByteArrayLiteral("O\r"))) { |
| 190 | setError(tr("Failed to open the slcan channel."), QCanBusDevice::ConnectionError); |
| 191 | m_port->close(); |
| 192 | m_port->deleteLater(); |
| 193 | m_port = nullptr; |
| 194 | setState(QCanBusDevice::UnconnectedState); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | setState(QCanBusDevice::ConnectedState); |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @brief Closes the slcan channel and the serial port. |
nothing calls this directly
no test coverage detected