Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Filtering QCanBusDevice messages
Qt 6.11 is out! See what's new in the release blog

Filtering QCanBusDevice messages

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 683 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.
  • T Offline
    T Offline
    tanselgonul
    wrote on last edited by
    #1

    I am trying to filter the received can frames according to frameId and frameIdMask but Pyside6 source file doesn't seem to have the class attributes I am looking for. I couldn't find a Python documentation about filtering but C++ implementation has the class Filter with the member variables like this

        struct Filter
        {
            friend constexpr bool operator==(const Filter &a, const Filter &b) noexcept
            {
                return a.frameId == b.frameId && a.frameIdMask == b.frameIdMask
                        && a.type == b.type && a.format == b.format;
            }
    
            friend constexpr bool operator!=(const Filter &a, const Filter &b) noexcept
            {
                return !operator==(a, b);
            }
    
            enum FormatFilter {
                MatchBaseFormat = 0x0001,
                MatchExtendedFormat = 0x0002,
                MatchBaseAndExtendedFormat = 0x0003,
            };
            Q_DECLARE_FLAGS(FormatFilters, FormatFilter)
    
            QCanBusFrame::FrameId frameId = 0;
            QCanBusFrame::FrameId frameIdMask = 0;
            QCanBusFrame::FrameType type = QCanBusFrame::InvalidFrame;
            FormatFilter format = MatchBaseAndExtendedFormat;
        };
    

    And the given usage example is like this

    QCanBusDevice::Filter filter;
    QList<QCanBusDevice::Filter> filterList;
    
    // filter all CAN bus packages with id 0x444 (base) or 0xXXXXX444 (extended)
    filter.frameId = 0x444u;
    filter.frameIdMask = 0x7FFu;
    filter.format = QCanBusDevice::Filter::MatchBaseAndExtendedFormat;
    filter.type = QCanBusFrame::InvalidFrame;
    filterList.append(filter);
    
    // apply filter
    device->setConfigurationParameter(QCanBusDevice::RawFilterKey, QVariant::fromValue(filterList));
    

    But on the python side of things I couldn't figure out how to construct the Filter object since class Filter declaration only includes this

        class Filter(Shiboken.Object):
    
            class FormatFilter(enum.Flag):
    
                MatchBaseFormat          : QCanBusDevice.Filter.FormatFilter = ... # 0x1
                MatchExtendedFormat      : QCanBusDevice.Filter.FormatFilter = ... # 0x2
                MatchBaseAndExtendedFormat: QCanBusDevice.Filter.FormatFilter = ... # 0x3
    
    
            @overload
            def __init__(self) -> None: ...
            @overload
            def __init__(self, Filter: PySide6.QtSerialBus.QCanBusDevice.Filter) -> None: ...
    
            @staticmethod
            def __copy__() -> None: ...
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      I currently don't know if it's the correct solution but intuitively, I would go with implementing the __ne__ and __eq__ methods in your filter class.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • F Offline
        F Offline
        friedemannkleint
        wrote on last edited by
        #3

        Reimplementing operator eq will not work since this is not a base class. But the filter fields can be set like in C++, for example:

        import sys
        from PySide6.QtCore import QCoreApplication, QLibraryInfo, qVersion, Qt
        from PySide6.QtSerialBus import QCanBusDevice, QCanBus
        
        if __name__ == '__main__':
            app = QCoreApplication(sys.argv)
            device, name = QCanBus.instance().createDevice("passthrucan", "test")
            print(device, name)
            filters = []
            flt = QCanBusDevice.Filter()
            print("flt.format=", flt.format, "flt.type=", flt.type)
            print("flt.frameId=", flt.frameId, "flt.frameIdMask=", flt.frameIdMask )
            flt.format = QCanBusDevice.Filter.FormatFilter.MatchBaseFormat
            filters.append(flt)
            flt = QCanBusDevice.Filter()
            flt.format = QCanBusDevice.Filter.FormatFilter.MatchBaseAndExtendedFormat
            filters.append(flt)
            print("Setting: ", flt)
            device.setConfigurationParameter(QCanBusDevice.ConfigurationKey.RawFilterKey, filters)
            ret = device.configurationParameter(QCanBusDevice.ConfigurationKey.RawFilterKey)
            print("ret=", ret)
        

        produces

        <PySide6.QtSerialBus.QCanBusDevice(0x11c31f0) at 0x7050c09d0e40>
        flt.format= FormatFilter.MatchBaseAndExtendedFormat flt.type= FrameType.InvalidFrame
        flt.frameId= 0 flt.frameIdMask= 0
        Setting:  <PySide6.QtSerialBus.QCanBusDevice.Filter object at 0x7050c09d0ec0>
        ret= [<PySide6.QtSerialBus.QCanBusDevice.Filter object at 0x7050c09d2540>, <PySide6.QtSerialBus.QCanBusDevice.Filter object at 0x7050c09d3880>]
        
        SGaistS 1 Reply Last reply
        1
        • F friedemannkleint

          Reimplementing operator eq will not work since this is not a base class. But the filter fields can be set like in C++, for example:

          import sys
          from PySide6.QtCore import QCoreApplication, QLibraryInfo, qVersion, Qt
          from PySide6.QtSerialBus import QCanBusDevice, QCanBus
          
          if __name__ == '__main__':
              app = QCoreApplication(sys.argv)
              device, name = QCanBus.instance().createDevice("passthrucan", "test")
              print(device, name)
              filters = []
              flt = QCanBusDevice.Filter()
              print("flt.format=", flt.format, "flt.type=", flt.type)
              print("flt.frameId=", flt.frameId, "flt.frameIdMask=", flt.frameIdMask )
              flt.format = QCanBusDevice.Filter.FormatFilter.MatchBaseFormat
              filters.append(flt)
              flt = QCanBusDevice.Filter()
              flt.format = QCanBusDevice.Filter.FormatFilter.MatchBaseAndExtendedFormat
              filters.append(flt)
              print("Setting: ", flt)
              device.setConfigurationParameter(QCanBusDevice.ConfigurationKey.RawFilterKey, filters)
              ret = device.configurationParameter(QCanBusDevice.ConfigurationKey.RawFilterKey)
              print("ret=", ret)
          

          produces

          <PySide6.QtSerialBus.QCanBusDevice(0x11c31f0) at 0x7050c09d0e40>
          flt.format= FormatFilter.MatchBaseAndExtendedFormat flt.type= FrameType.InvalidFrame
          flt.frameId= 0 flt.frameIdMask= 0
          Setting:  <PySide6.QtSerialBus.QCanBusDevice.Filter object at 0x7050c09d0ec0>
          ret= [<PySide6.QtSerialBus.QCanBusDevice.Filter object at 0x7050c09d2540>, <PySide6.QtSerialBus.QCanBusDevice.Filter object at 0x7050c09d3880>]
          
          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @friedemannkleint Nice !

          It might be worth adding something about it in the python part of the doc.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • T tanselgonul has marked this topic as solved on

          • Login

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