* @brief Opens the serial port and sends the analyzer initialization frame. */
| 200 | * @brief Opens the serial port and sends the analyzer initialization frame. |
| 201 | */ |
| 202 | bool IO::Drivers::SeeedCanBackend::open() |
| 203 | { |
| 204 | const auto requested = configurationParameter(QCanBusDevice::BitRateKey).toUInt(); |
| 205 | if (seeedBitrateCode(requested == 0 ? 500000 : requested) == 0) { |
| 206 | setError(tr("The bitrate %1 bps is not supported by the USB-CAN Analyzer.").arg(requested), |
| 207 | QCanBusDevice::ConfigurationError); |
| 208 | setState(QCanBusDevice::UnconnectedState); |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | m_port = new QSerialPort(m_portName, this); |
| 213 | m_port->setBaudRate(kSeeedBaudRate); |
| 214 | |
| 215 | if (!m_port->open(QIODevice::ReadWrite)) { |
| 216 | setError(tr("Could not open serial port %1: %2").arg(m_portName, m_port->errorString()), |
| 217 | QCanBusDevice::ConnectionError); |
| 218 | m_port->deleteLater(); |
| 219 | m_port = nullptr; |
| 220 | setState(QCanBusDevice::UnconnectedState); |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | connect(m_port, &QSerialPort::readyRead, this, &SeeedCanBackend::onReadyRead); |
| 225 | |
| 226 | if (!sendInitFrame(requested == 0 ? 500000 : requested)) { |
| 227 | setError(tr("Failed to initialize the USB-CAN Analyzer."), QCanBusDevice::ConnectionError); |
| 228 | m_port->close(); |
| 229 | m_port->deleteLater(); |
| 230 | m_port = nullptr; |
| 231 | setState(QCanBusDevice::UnconnectedState); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | setState(QCanBusDevice::ConnectedState); |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @brief Closes the serial port. |
nothing calls this directly
no test coverage detected