Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [CAN BUS](SocketCAN cannot read CAN frames)
QtWS25 Last Chance

[CAN BUS](SocketCAN cannot read CAN frames)

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 427 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EngElectronic
    wrote on last edited by
    #1

    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-frames

    Hi, 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();
    

    }

    JKSHJ 1 Reply Last reply
    0
    • K Offline
      K Offline
      kuzulis
      Qt Champions 2020
      wrote on last edited by kuzulis
      #2

      Your code is stupid and wrong. Read the documentation and take a look the examples at first, before posting something on a forum.

      1 Reply Last reply
      -4
      • E EngElectronic

        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-frames

        Hi, 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();
        

        }

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @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.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        5

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved