Gestore bus CAN
L'esempio invia e riceve frame del bus CAN.
L'esempio invia e riceve frame del bus CAN. I frame in arrivo vengono ordinati in base al loro tipo. È disponibile una finestra di dialogo di connessione per regolare i parametri di connessione del bus CAN.
Classi Qt Serial Bus utilizzate in questo esempio:

Creazione di un QCanBusDevice
Per eseguire qualsiasi comunicazione CAN è necessaria un'istanza di ` QCanBusDevice `.
ConnectDialog consente di specificare tutti i parametri necessari. Successivamente, il dispositivo viene creato utilizzando i nomi del plugin e dell’interfaccia forniti:
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);Le connessioni stabilite consentono di elaborare i frame in entrata, controllare i frame inviati e gestire gli errori.
Una volta creato il dispositivo, utilizzare ` QCanBusDevice::connectDevice()` per avviare la comunicazione.
Elaborazione dei frame in entrata
QCanBusDevice emette un segnale framesReceived() quando sono disponibili nuovi frame. Il metodo readFrame() può essere utilizzato per leggere un singolo QCanBusFrame quando sono presenti available frames. Una volta ricevuto il frame, è possibile estrarne singoli parametri quali frameId, timeStamp o 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}));
}Invio di frame
Per inviare dati personalizzati sul bus CAN, l’utente deve specificare almeno un frameId e un payload. Facoltativamente, è possibile configurare altri parametri 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);Una volta preparato il frame, è possibile utilizzare il metodo QCanBusDevice::writeFrame() per inviarlo:
m_canDevice->writeFrame(frame);Esecuzione dell’esempio
È possibile eseguire l'esempio da:
- Qt Creator
Aprire la modalità Welcome e selezionare l’esempio da Examples. Per ulteriori informazioni, consultare Qt Creator: Tutorial: Compilazione ed esecuzione.
- Qt Extension for Visual Studio Code
Eseguire il comando ` Qt: Open Qt examples ` da ` Command Palette` e selezionare l’esempio dall’elenco. Per ulteriori informazioni, consultare Qt Extension for Visual Studio Code: Tutorial: Compilazione ed esecuzione.
© 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.