[CAN BUS](SocketCAN cannot read CAN frames)
-
wrote on 2 Dec 2019, 00:11 last edited by
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();
}
-
Qt Champions 2020wrote on 2 Dec 2019, 04:09 last edited by kuzulis 12 Feb 2019, 04:09
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.
3/3