Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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?



  • #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


  • Lifetime Qt Champion

    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



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


Log in to reply