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. Problem reading a message from a QCanBusDevice.
QtWS25 Last Chance

Problem reading a message from a QCanBusDevice.

Scheduled Pinned Locked Moved Unsolved General and Desktop
qcanbusdevicecan busqml
11 Posts 3 Posters 1.9k 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.
  • B Offline
    B Offline
    Babs
    wrote on last edited by aha_1980
    #1

    Hello i'm working on a qt application using CAN Bus Communication. I communicate with a sensor and have to display in my QML side like the temperature, voltage... Value of those data change very often. I send a message to the systec can device and it respond with data containing informations i need. My problem is that i get nothing even when i connect my reading slot to the QCanBusDevice::frameReceived slot.

    I wrote a function deviceProfile process to send a request to read my device profile.

    void DevicesManagement::deviceProfileProcess()
    {
        char data[8]={0x43,0x00,0x10,0x00,0x00,0x00,0x00,0x00};
        QByteArray frame(data,8);
        QCanBusFrame myframe;
        myframe.setPayload(frame);
        myframe.setFrameId(0x617);
        m_device->writeFrame(myframe);
        emit deviceProfileProcessed();
    }
    

    The function deviceProfile() read the frame send in return to my previous message.

    void DevicesManagement::deviceProfile()
    {
        QCanBusFrame frame=m_device->readFrame();
        QString str=frame.payload().toHex().right(8).toUpper();
        set_deviceProfile(str);
    }
    

    I connect the fonction deviceProfile() to the slot QCanBusFrame::framesReceived and i call deviceProfileProcess on my main() in order to see if the device Profile has been update.
    The problem is that i have nothing in return while i can verify that a message has been received.
    The connect:

        connect(m_device,&QCanBusDevice::framesReceived,this,&DevicesManagement::deviceProfile,Qt::UniqueConnection);
    

    Anyone can help?

    aha_1980A 1 Reply Last reply
    0
    • B Babs

      Hello i'm working on a qt application using CAN Bus Communication. I communicate with a sensor and have to display in my QML side like the temperature, voltage... Value of those data change very often. I send a message to the systec can device and it respond with data containing informations i need. My problem is that i get nothing even when i connect my reading slot to the QCanBusDevice::frameReceived slot.

      I wrote a function deviceProfile process to send a request to read my device profile.

      void DevicesManagement::deviceProfileProcess()
      {
          char data[8]={0x43,0x00,0x10,0x00,0x00,0x00,0x00,0x00};
          QByteArray frame(data,8);
          QCanBusFrame myframe;
          myframe.setPayload(frame);
          myframe.setFrameId(0x617);
          m_device->writeFrame(myframe);
          emit deviceProfileProcessed();
      }
      

      The function deviceProfile() read the frame send in return to my previous message.

      void DevicesManagement::deviceProfile()
      {
          QCanBusFrame frame=m_device->readFrame();
          QString str=frame.payload().toHex().right(8).toUpper();
          set_deviceProfile(str);
      }
      

      I connect the fonction deviceProfile() to the slot QCanBusFrame::framesReceived and i call deviceProfileProcess on my main() in order to see if the device Profile has been update.
      The problem is that i have nothing in return while i can verify that a message has been received.
      The connect:

          connect(m_device,&QCanBusDevice::framesReceived,this,&DevicesManagement::deviceProfile,Qt::UniqueConnection);
      

      Anyone can help?

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

      Hi @Babs,

      First you should change your receive slot to a loop, to make sure all received messages are read. Because it may be two messages arrived, and you only read the first one as reaction to the QCanBusDevice::framesReceived signal.

      void DevicesManagement::deviceProfile()
      {
        while (m_device->framesAvailable()) {
          QCanBusFrame frame=m_device->readFrame();
          qDebug() << frame.toString();
        }
      }
      

      Does that give you the correct message as debug output?

      Regards

      Qt has to stay free or it will die.

      B 2 Replies Last reply
      1
      • aha_1980A aha_1980

        Hi @Babs,

        First you should change your receive slot to a loop, to make sure all received messages are read. Because it may be two messages arrived, and you only read the first one as reaction to the QCanBusDevice::framesReceived signal.

        void DevicesManagement::deviceProfile()
        {
          while (m_device->framesAvailable()) {
            QCanBusFrame frame=m_device->readFrame();
            qDebug() << frame.toString();
          }
        }
        

        Does that give you the correct message as debug output?

        Regards

        B Offline
        B Offline
        Babs
        wrote on last edited by
        #3

        @aha_1980 It works when it comes to debug output in the while loop. I save that data on variable and try to debug it in my main window. The data is empty.

        while(m_device->framesAvailable()){
                QCanBusFrame frame=m_device->readFrame();
                QString str=frame.payload().toHex().right(8).toUpper();
                set_deviceProfile(str);
            }
        

        In my main

            QGuiApplication app (argc, argv);
            QQmlApplicationEngine engine (&app);
            registerQtQmlTricksUiElements (&engine);
            registerQtQmlTricksSmartDataModel (&engine);
            DevicesManagement *manager=new DevicesManagement(&engine);
            manager->initializeDevices("125000");
            manager->deviceProfileProcess();
            manager->addDevicetoList(23);
            engine.rootContext()->setContextProperty("mManager",manager);
            qDebug()<<manager->get_deviceProfile();
        
        jsulmJ 1 Reply Last reply
        0
        • aha_1980A aha_1980

          Hi @Babs,

          First you should change your receive slot to a loop, to make sure all received messages are read. Because it may be two messages arrived, and you only read the first one as reaction to the QCanBusDevice::framesReceived signal.

          void DevicesManagement::deviceProfile()
          {
            while (m_device->framesAvailable()) {
              QCanBusFrame frame=m_device->readFrame();
              qDebug() << frame.toString();
            }
          }
          

          Does that give you the correct message as debug output?

          Regards

          B Offline
          B Offline
          Babs
          wrote on last edited by
          #4

          @aha_1980 Also i have to read a lot of data like the temperature ,voltage and current from the Can device. If i intend to read each time in while loop, isn't it possible i get the wrong data sometimes. There are many other datas that transit on my Network.

          aha_1980A 1 Reply Last reply
          0
          • B Babs

            @aha_1980 It works when it comes to debug output in the while loop. I save that data on variable and try to debug it in my main window. The data is empty.

            while(m_device->framesAvailable()){
                    QCanBusFrame frame=m_device->readFrame();
                    QString str=frame.payload().toHex().right(8).toUpper();
                    set_deviceProfile(str);
                }
            

            In my main

                QGuiApplication app (argc, argv);
                QQmlApplicationEngine engine (&app);
                registerQtQmlTricksUiElements (&engine);
                registerQtQmlTricksSmartDataModel (&engine);
                DevicesManagement *manager=new DevicesManagement(&engine);
                manager->initializeDevices("125000");
                manager->deviceProfileProcess();
                manager->addDevicetoList(23);
                engine.rootContext()->setContextProperty("mManager",manager);
                qDebug()<<manager->get_deviceProfile();
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Babs said in Problem reading a message from a QCanBusDevice.:

            I save that data on variable

            Do you mean in set_deviceProfile(str)? Can you show its content?
            Also, you should append to the string inside while loop.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            B 1 Reply Last reply
            0
            • B Babs

              @aha_1980 Also i have to read a lot of data like the temperature ,voltage and current from the Can device. If i intend to read each time in while loop, isn't it possible i get the wrong data sometimes. There are many other datas that transit on my Network.

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

              @Babs said in Problem reading a message from a QCanBusDevice.:

              There are many other datas that transit on my Network.

              Yes, that is typical on such networks. You need to find out the interesting CAN frames (by checking the frameId and the data) and only react on these frames.

              All other frames should be ignored.

              Qt has to stay free or it will die.

              B 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Babs said in Problem reading a message from a QCanBusDevice.:

                I save that data on variable

                Do you mean in set_deviceProfile(str)? Can you show its content?
                Also, you should append to the string inside while loop.

                B Offline
                B Offline
                Babs
                wrote on last edited by Babs
                #7

                @jsulm For set_deviceProfile i wrote this macro

                    QML_WRITABLE_VAR_PROPERTY(QString, deviceProfile)
                

                it defines a Q_PROPERTY to expose variable to QML. And define getters and setters for this variable;
                Set_deviceProfile(QString deviceProfile) contains:

                m_deviceProfile=deviceProfile;
                emit deviceProfileChanged
                
                1 Reply Last reply
                0
                • aha_1980A aha_1980

                  @Babs said in Problem reading a message from a QCanBusDevice.:

                  There are many other datas that transit on my Network.

                  Yes, that is typical on such networks. You need to find out the interesting CAN frames (by checking the frameId and the data) and only react on these frames.

                  All other frames should be ignored.

                  B Offline
                  B Offline
                  Babs
                  wrote on last edited by
                  #8

                  @aha_1980 I plan using a circular buffer for that issue. For know i still can't get my variable filled with the data.

                  aha_1980A 1 Reply Last reply
                  0
                  • B Babs

                    @aha_1980 I plan using a circular buffer for that issue. For know i still can't get my variable filled with the data.

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

                    @Babs said in Problem reading a message from a QCanBusDevice.:

                    @aha_1980 I plan using a circular buffer for that issue.

                    A circular buffer will not help to filter out messages you are not interested in.

                    For know i still can't get my variable filled with the data.

                    But why?

                    Qt has to stay free or it will die.

                    B 2 Replies Last reply
                    0
                    • aha_1980A aha_1980

                      @Babs said in Problem reading a message from a QCanBusDevice.:

                      @aha_1980 I plan using a circular buffer for that issue.

                      A circular buffer will not help to filter out messages you are not interested in.

                      For know i still can't get my variable filled with the data.

                      But why?

                      B Offline
                      B Offline
                      Babs
                      wrote on last edited by
                      #10

                      @aha_1980 i think i got why it is not working. I main function i call

                      qDebug()<<manager->get_deviceProfile();
                      

                      I have got no return because the function connected to signal frameReceived has not finished processing.

                      1 Reply Last reply
                      0
                      • aha_1980A aha_1980

                        @Babs said in Problem reading a message from a QCanBusDevice.:

                        @aha_1980 I plan using a circular buffer for that issue.

                        A circular buffer will not help to filter out messages you are not interested in.

                        For know i still can't get my variable filled with the data.

                        But why?

                        B Offline
                        B Offline
                        Babs
                        wrote on last edited by
                        #11

                        @aha_1980 said in Problem reading a message from a QCanBusDevice.:

                        A circular buffer will not help to filter out messages you are not interested in.

                        I want to use it to store the messages in order to avoid two device attempting to access to the network at the same time

                        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