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 using QCanBus for reading CAN data. I am tring to convert QString to QByteArray and hence convert them to decimal values.? The code is not efficient ?any suggestions?

I am using QCanBus for reading CAN data. I am tring to convert QString to QByteArray and hence convert them to decimal values.? The code is not efficient ?any suggestions?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 319 Views 1 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 aha_1980
    #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")))
        {
            qWarning() << "plugin available";
        }
    
        QString errorString;
        QCanBusDevice *device = QCanBus::instance()->createDevice(
            QStringLiteral("socketcan"), QStringLiteral("can0"), &errorString);
        if (!device)
        {
            qWarning() << errorString;
        } else
        {
            device->connectDevice();
            std::cout << "connected can0" << std::endl;
    
            device->connect(device, &QCanBusDevice::framesReceived, [device]()
            {
            QCanBusFrame frame = device->readFrame();
            QString testV = frame.toString();
            QString text = testV.toUtf8().constData();
            QByteArray tempArray = text.toLatin1();
            QList<QByteArray>qbList = tempArray.split(' ');
            QByteArray dataBytes;
            dataBytes.reserve(qbList.count());
            int dbIndex = qbList.count() - 1;
            bool ok;
            for (int i = 0; i < qbList.count(); i++, dbIndex --) {
              unsigned char c;
              c = qbList.at(i).toUInt(&ok, 16) & 0xff; // guard against sign extension
              dataBytes[dbIndex] = c;
            }
            // check the values in dataBytes
            for (int i = 0; i < dataBytes.count(); i++)
            {
              printf("0x%02x\n", dataBytes[i] & 0xff);
    
            }
    
    
            });
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    Edit aha_1980: added code tags

    aha_1980A 1 Reply 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")))
          {
              qWarning() << "plugin available";
          }
      
          QString errorString;
          QCanBusDevice *device = QCanBus::instance()->createDevice(
              QStringLiteral("socketcan"), QStringLiteral("can0"), &errorString);
          if (!device)
          {
              qWarning() << errorString;
          } else
          {
              device->connectDevice();
              std::cout << "connected can0" << std::endl;
      
              device->connect(device, &QCanBusDevice::framesReceived, [device]()
              {
              QCanBusFrame frame = device->readFrame();
              QString testV = frame.toString();
              QString text = testV.toUtf8().constData();
              QByteArray tempArray = text.toLatin1();
              QList<QByteArray>qbList = tempArray.split(' ');
              QByteArray dataBytes;
              dataBytes.reserve(qbList.count());
              int dbIndex = qbList.count() - 1;
              bool ok;
              for (int i = 0; i < qbList.count(); i++, dbIndex --) {
                unsigned char c;
                c = qbList.at(i).toUInt(&ok, 16) & 0xff; // guard against sign extension
                dataBytes[dbIndex] = c;
              }
              // check the values in dataBytes
              for (int i = 0; i < dataBytes.count(); i++)
              {
                printf("0x%02x\n", dataBytes[i] & 0xff);
      
              }
      
      
              });
          }
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      

      Edit aha_1980: added code tags

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

      Hi @rmk1842,

      If I get it correctly, your code:

      device->connect(device, &QCanBusDevice::framesReceived, device
      {
      QCanBusFrame frame = device->readFrame();
      QString testV = frame.toString();
      QString text = testV.toUtf8().constData();
      QByteArray tempArray = text.toLatin1();
      QList<QByteArray>qbList = tempArray.split(' ');
      QByteArray dataBytes;
      ...

      can be simplified to.

      device->connect(device, &QCanBusDevice::framesReceived, [device]() {
         const QCanBusFrame frame = device->readFrame();
         QByteArray dataBytes = frame.payload();
         std::reverse(dataBytes.begin(), dataBytes.end());
      
         // check the values in dataBytes
         for (int i = 0; i < dataBytes.count(); i++)
         {
            printf("0x%02x\n", dataBytes.at(i) & 0xff);
         }
      });
      

      Edit: I just saw you want the databytes in reverse order. Fixed my example

      Regards

      Qt has to stay free or it will die.

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

        how to print in the qdebug and view them? printf is taking too much buffer time to view them and also i want to convert them to decimal values.

        1 Reply Last reply
        0

        • Login

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