[CAN BUS](SocketCAN cannot read CAN frames)
-
https://forum.qt.io/topic/101710/qt-canbus-systec-can-can-t-read-can-frames
https://forum.qt.io/topic/69635/can-bus-socketcan-cannot-read-can-framesHi, I have same problem that topics above. I can send but I can't read frames.
I did tests with can and vcan. Bellow my code:#include <QCanBusFrame>
#include <QCanBus>
#include <QCanBusDevice>int main(int argc, char *argv[])
{/* CAN */ if (QCanBus::instance()->plugins().contains(QStringLiteral("socketcan"))) { // plugin available } QCanBusDevice *device = QCanBus::instance()->createDevice( QStringLiteral("socketcan"), QStringLiteral("vcan1")); device->connectDevice(); QCanBusFrame frame; frame.setFrameId(0x1A5A5A); QByteArray payload("00001111"); frame.setPayload(payload); device->writeFrame(frame); while(!device){} while(true) { if (device->framesAvailable()) { const QCanBusFrame frameRx = device->readFrame(); break; } } return app.exec();
}
-
Your code is stupid and wrong. Read the documentation and take a look the examples at first, before posting something on a forum.
-
https://forum.qt.io/topic/101710/qt-canbus-systec-can-can-t-read-can-frames
https://forum.qt.io/topic/69635/can-bus-socketcan-cannot-read-can-framesHi, I have same problem that topics above. I can send but I can't read frames.
I did tests with can and vcan. Bellow my code:#include <QCanBusFrame>
#include <QCanBus>
#include <QCanBusDevice>int main(int argc, char *argv[])
{/* CAN */ if (QCanBus::instance()->plugins().contains(QStringLiteral("socketcan"))) { // plugin available } QCanBusDevice *device = QCanBus::instance()->createDevice( QStringLiteral("socketcan"), QStringLiteral("vcan1")); device->connectDevice(); QCanBusFrame frame; frame.setFrameId(0x1A5A5A); QByteArray payload("00001111"); frame.setPayload(payload); device->writeFrame(frame); while(!device){} while(true) { if (device->framesAvailable()) { const QCanBusFrame frameRx = device->readFrame(); break; } } return app.exec();
}
@EngElectronic said in [CAN BUS](SocketCAN cannot read CAN frames):
while(true)
An infinite while while loop blocks the event processor which prevents you from sending or receiving frames.
There are other issues in your code too. Follow the working example to learn how to use QCanBusDevice correctly: https://doc.qt.io/qt-5/qtserialbus-can-example.html
@kuzulis, there is no need and no benefit in being nasty.