参考 PCAN示例资源文件介绍:PCAN示例资源文件介绍 - GitCode
此处就是界面上添加了一个 按钮,用于创建连接,界面代码自己简单做一个
pro中添加 serialbus
QT += serialbus
.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QCanBusDevice> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; //std::unique_ptr<QCanBusDevice> m_canDevice; QCanBusDevice *m_canDevice=nullptr; QList<QCanBusDeviceInfo> m_interfaces; short tacomathrottle; short tacomasteer; short tacomaspeed; bool tacomabrake; private slots: void processReceivedFrames(); void processErrors(QCanBusDevice::CanBusError) const; void canconnectclicked(); }; #endif // MAINWINDOW_H
.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QCanBus> #include <QDebug> #include <QCanBusFrame> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->canBT,&QPushButton::clicked,this,&MainWindow::canconnectclicked); } MainWindow::~MainWindow() { delete ui; } void MainWindow::processErrors(QCanBusDevice::CanBusError error) const { switch (error) { case QCanBusDevice::ReadError: case QCanBusDevice::WriteError: case QCanBusDevice::ConnectionError: case QCanBusDevice::ConfigurationError: case QCanBusDevice::UnknownError: qDebug()<<m_canDevice->errorString(); break; default: break; } } void MainWindow::processReceivedFrames() { if (!m_canDevice) return; while (m_canDevice->framesAvailable()) { const QCanBusFrame frame = m_canDevice->readFrame(); //qDebug()<<frame.error(); // qDebug()<<frame.frameId();//frame id,it's decimal //qDebug()<<frame.frameType();//1 means dataframe //qDebug()<<m_canDevice->state(); QString view; if (frame.frameType() == QCanBusFrame::ErrorFrame) { view = m_canDevice->interpretErrorFrame(frame); } else if(frame.frameType()==QCanBusFrame::DataFrame) { view=frame.toString(); } qDebug()<<view; } } void MainWindow::canconnectclicked() { //QCanBus::instance()->plugins(); const QString plugin="peakcan"; const int bitrate=500000; bool connectflag=false; QString errorString; //const QString plugin="virtualcan"; // qDebug()<< QCanBus::instance()->plugins(); m_interfaces = QCanBus::instance()->availableDevices(plugin); if(!m_interfaces.empty()) { qDebug()<<m_interfaces.begin()->name(); m_canDevice=QCanBus::instance()->createDevice(plugin, m_interfaces.begin()->name(), &errorString); } else { qDebug()<<"do not find available interface for pcan"; } if (!m_canDevice) { qDebug() <<"createDevice error" <<errorString; } else { /***peakcan do not support configuration key RawFilterKey***/ // QCanBusDevice::Filter filter; // QList<QCanBusDevice::Filter> filterList; // // filter all CAN bus packages with id 0x444 (base) or 0xXXXXX444 (extended) // filter.frameId = 0x444u; // qDebug()<<filter.frameId; // filter.frameIdMask = 0x0u; // filter.format = QCanBusDevice::Filter::MatchBaseFormat; // filter.type = QCanBusFrame::DataFrame; // filterList.append(filter); // m_canDevice->setConfigurationParameter(QCanBusDevice::RawFilterKey, QVariant::fromValue(filterList)); m_canDevice->setConfigurationParameter(QCanBusDevice::BitRateKey,QVariant(bitrate)); connectflag= m_canDevice->connectDevice(); qDebug()<<"connecflag"<<connectflag; } if(connectflag) { connect(m_canDevice, &QCanBusDevice::errorOccurred, this, &MainWindow::processErrors); connect(m_canDevice, &QCanBusDevice::framesReceived, this, &MainWindow::processReceivedFrames); ui->canBT->setEnabled(false); } }
标签:PCAN,const,demo,QCanBusDevice,canDevice,简单,qDebug,include,MainWindow From: https://www.cnblogs.com/RYSBlog/p/18562117