Files
remote-ui/tools/CAN/CAN_interface.cpp
T
sani7 e25e2baa12 can interface
remove next mid
2026-02-01 18:40:57 +01:00

36 lines
1011 B
C++

#include "CAN_interface.hpp"
CAN_Interface::CAN_Interface(QString dev, QObject *parent)
: QObject(parent), m_mid(0)
{
if (!QCanBus::instance()->plugins().contains(QStringLiteral("socketcan"))) {
throw std::runtime_error("CAN Interface: socketcan plugin not found");
}
QString errorString;
m_canDevice = QCanBus::instance()->createDevice(QStringLiteral("socketcan"), dev, &errorString);
if (!m_canDevice)
{
// Error handling goes here
throw std::runtime_error("CAN Interface: " + errorString.toStdString());
}
m_canDevice->connectDevice();
connect(m_canDevice, &QCanBusDevice::framesReceived, this, [this] {
for (const QCanBusFrame &frame : m_canDevice->readAllFrames())
{
emit frame_received(frame);
}
});
}
CAN_Interface::~CAN_Interface()
{
m_canDevice->disconnectDevice();
delete m_canDevice;
}
void CAN_Interface::send_frame(const QCanBusFrame &frame)
{
m_canDevice->writeFrame(frame);
}