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. I am trying to read the CAN data from socketcan in my linux pc. I could not read any frame data, below is my mainwindow.cpp. Any suggestions ???
Forum Updated to NodeBB v4.3 + New Features

I am trying to read the CAN data from socketcan in my linux pc. I could not read any frame data, below is my mainwindow.cpp. Any suggestions ???

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 374 Views 2 Watching
  • 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.
  • R Offline
    R Offline
    rmk1842
    wrote on last edited by
    #1

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QCanBus>
    #include <QCanBusFrame>
    #include <QtDebug>
    #include<iostream>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    if (QCanBus::instance()->plugins().contains(QStringLiteral("socketcan")))
    {
    qDebug()<<"Avaliable";
    }
    QCanBusDevice *device = QCanBus::instance()->createDevice(
    QStringLiteral("socketcan"), QStringLiteral("can0"));
    device->connectDevice();
    qDebug()<<"sucess";
    while(device->framesAvailable())
    {
    const QCanBusFrame frame = device->readFrame();
    QString test = frame.toString();
    std::string text = test.toUtf8().constData();
    qDebug()<<test;
    std::cout<<text<<std::endl;
    }
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    Pablo J. RoginaP aha_1980A 2 Replies Last reply
    0
    • R rmk1842

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QCanBus>
      #include <QCanBusFrame>
      #include <QtDebug>
      #include<iostream>

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);
      if (QCanBus::instance()->plugins().contains(QStringLiteral("socketcan")))
      {
      qDebug()<<"Avaliable";
      }
      QCanBusDevice *device = QCanBus::instance()->createDevice(
      QStringLiteral("socketcan"), QStringLiteral("can0"));
      device->connectDevice();
      qDebug()<<"sucess";
      while(device->framesAvailable())
      {
      const QCanBusFrame frame = device->readFrame();
      QString test = frame.toString();
      std::string text = test.toUtf8().constData();
      qDebug()<<test;
      std::cout<<text<<std::endl;
      }
      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @rmk1842 your while loop is blocking Qt's events. Connect a slot to the framesReceived signal instead.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • R rmk1842

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QCanBus>
        #include <QCanBusFrame>
        #include <QtDebug>
        #include<iostream>

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        if (QCanBus::instance()->plugins().contains(QStringLiteral("socketcan")))
        {
        qDebug()<<"Avaliable";
        }
        QCanBusDevice *device = QCanBus::instance()->createDevice(
        QStringLiteral("socketcan"), QStringLiteral("can0"));
        device->connectDevice();
        qDebug()<<"sucess";
        while(device->framesAvailable())
        {
        const QCanBusFrame frame = device->readFrame();
        QString test = frame.toString();
        std::string text = test.toUtf8().constData();
        qDebug()<<test;
        std::cout<<text<<std::endl;
        }
        }

        MainWindow::~MainWindow()
        {
        delete ui;
        }

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #2

        @rmk1842 have you already tried this Qt CAN Bus example?

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        2
        • R rmk1842

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QCanBus>
          #include <QCanBusFrame>
          #include <QtDebug>
          #include<iostream>

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);
          if (QCanBus::instance()->plugins().contains(QStringLiteral("socketcan")))
          {
          qDebug()<<"Avaliable";
          }
          QCanBusDevice *device = QCanBus::instance()->createDevice(
          QStringLiteral("socketcan"), QStringLiteral("can0"));
          device->connectDevice();
          qDebug()<<"sucess";
          while(device->framesAvailable())
          {
          const QCanBusFrame frame = device->readFrame();
          QString test = frame.toString();
          std::string text = test.toUtf8().constData();
          qDebug()<<test;
          std::cout<<text<<std::endl;
          }
          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @rmk1842 your while loop is blocking Qt's events. Connect a slot to the framesReceived signal instead.

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • R Offline
            R Offline
            rmk1842
            wrote on last edited by
            #4

            Thanks using framesRecived() the issue is solved

            1 Reply Last reply
            1

            • Login

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