このページでは

CANバスマネージャー

このサンプルは、CANバスフレームの送受信を行います。

このサンプルでは、CANバスフレームの送受信を行います。受信したフレームは、そのタイプに応じて並べ替えられます。CANバス接続パラメータを調整するための接続ダイアログが用意されています。

この例で使用される Qt Serial Bus このサンプルで使用されるクラス:

CANフレームの送信制御(フレームタイプ、ID、ペイロード)機能を備えたCANマネージャー、およびタイムスタンプとペイロードを表示する受信CANフレームテーブル

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バスを介してカスタムデータを送信するには、ユーザーは少なくともframeId とpayload を指定する必要があります。オプションで、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);

例の動作

以下の手順でサンプルを実行できます:

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

© 2026 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.