CANバスマネージャー

CANバスフレームの送受信を行います。

この例では、CANバスフレームを送受信します。受信フレームは、タイプに従って順序付けられます。CANバスの接続パラメーターを調整するための接続ダイアログが表示されます。

キー Qt Serial Busこの例で使用されているクラス

QCanBusDeviceの作成

CAN通信を実行するには、QCanBusDevice のインスタンスが必要です。

ConnectDialog 、必要なパラメーターをすべて指定できます。その後、提供されたプラグイン名とインターフェイス名を使用してデバイスが作成されます:

    QString errorString;
    m_canDevice.reset(QCanBus::instance()->createDevice(p.pluginName, p.deviceInterfaceName,
                                                        &errorString));
    connect(m_canDevice.get(), &QCanBusDevice::errorOccurred,
            this, &MainWindow::processErrors);
    connect(m_canDevice.get(), &QCanBusDevice::framesReceived,
            this, &MainWindow::processReceivedFrames);
    connect(m_canDevice.get(), &QCanBusDevice::framesWritten,
            this, &MainWindow::processFramesWritten);

確立された接続により、受信フレームの処理、送信フレームの制御、エラー処理が可能になります。

デバイスが作成されたら、QCanBusDevice::connectDevice() を使用して通信を開始します。

受信フレームの処理

QCanBusDevice は、新しいフレームが利用可能になると () シグナルを発信します。 がある間は、 () メソッドを使用して、単一の を読み取ることができます。フレームを受信すると、 、 、 などの個々のパラメータを抽出することができます:framesReceived available frames readFrame QCanBusFrame frameId timeStamp payload

    while (m_canDevice->framesAvailable()) {
        m_numberFramesReceived++;
        const QCanBusFrame frame = m_canDevice->readFrame();

        QString data;
        if (frame.frameType() == QCanBusFrame::ErrorFrame)
            data = m_canDevice->interpretErrorFrame(frame);
        else
            data = QString::fromLatin1(frame.payload().toHex(' ').toUpper());

        const QString time = QString::fromLatin1("%1.%2  ")
                .arg(frame.timeStamp().seconds(), 10, 10, ' '_L1)
                .arg(frame.timeStamp().microSeconds() / 100, 4, 10, '0'_L1);

        const QString flags = frameFlags(frame);

        const QString id = QString::number(frame.frameId(), 16).toUpper();
        const QString dlc = QString::number(frame.payload().size());

        m_model->appendFrame(QStringList({QString::number(m_numberFramesReceived),
                                          time, flags, id, dlc, data}));
    }

フレームの送信

カスタムデータをCANバスで送信するには、ユーザーは少なくともframeIdpayload を提供する必要があります。オプションで、その他のQCanBusFrame パラメータを設定することもできます:

    const uint frameId = m_ui->frameIdEdit->text().toUInt(nullptr, 16);
    QString data = m_ui->payloadEdit->text();
    m_ui->payloadEdit->setText(formatHexData(data));
    const QByteArray payload = QByteArray::fromHex(data.remove(u' ').toLatin1());

    QCanBusFrame frame = QCanBusFrame(frameId, payload);
    frame.setExtendedFrameFormat(m_ui->extendedFormatBox->isChecked());
    frame.setFlexibleDataRateFormat(m_ui->flexibleDataRateBox->isChecked());
    frame.setBitrateSwitch(m_ui->bitrateSwitchBox->isChecked());

    if (m_ui->errorFrame->isChecked())
        frame.setFrameType(QCanBusFrame::ErrorFrame);
    else if (m_ui->remoteFrame->isChecked())
        frame.setFrameType(QCanBusFrame::RemoteRequestFrame);

フレームが準備されると、QCanBusDevice::writeFrame() メソッドを使用して送信できます:

    m_canDevice->writeFrame(frame);

例の実行

から例を実行するには Qt Creatorからサンプルを実行するには、Welcome モードを開き、Examples からサンプルを選択します。詳しくは、Building and Running an Exampleをご覧ください。

サンプルプロジェクト @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.