Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Problem reading a message from a QCanBusDevice.

    General and Desktop
    qcanbusdevice can bus qml
    3
    11
    818
    Loading More Posts
    • 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
      Babs last edited by aha_1980

      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_1980 1 Reply Last reply Reply Quote 0
      • aha_1980
        aha_1980 Lifetime Qt Champion @Babs last edited by

        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 Reply Quote 1
        • B
          Babs @aha_1980 last edited by

          @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();
          
          jsulm 1 Reply Last reply Reply Quote 0
          • B
            Babs @aha_1980 last edited by

            @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_1980 1 Reply Last reply Reply Quote 0
            • jsulm
              jsulm Lifetime Qt Champion @Babs last edited by

              @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 Reply Quote 0
              • aha_1980
                aha_1980 Lifetime Qt Champion @Babs last edited by

                @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 Reply Quote 0
                • B
                  Babs @jsulm last edited by Babs

                  @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 Reply Quote 0
                  • B
                    Babs @aha_1980 last edited by

                    @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_1980 1 Reply Last reply Reply Quote 0
                    • aha_1980
                      aha_1980 Lifetime Qt Champion @Babs last edited by

                      @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 Reply Quote 0
                      • B
                        Babs @aha_1980 last edited by

                        @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 Reply Quote 0
                        • B
                          Babs @aha_1980 last edited by

                          @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 Reply Quote 0
                          • First post
                            Last post