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 버스를 통해 사용자 정의 데이터를 전송하려면, 사용자는 최소한 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);예제 실행
다음 경로에서 예제를 실행할 수 있습니다:
- Qt Creator
Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 Qt Creator: 튜토리얼: 빌드 및 실행을 참조하십시오.
- Qt Extension for Visual Studio Code
Command Palette 에서 Qt: Open Qt examples 명령을 실행하고 목록에서 예제를 선택하십시오. 자세한 내용은 Qt Extension for Visual Studio Code 의 ‘튜토리얼: 빌드 및 실행’을 참조하십시오.
© 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.