Filtering QCanBusDevice messages
-
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: ... -
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. -
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>] -
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>]@friedemannkleint Nice !
It might be worth adding something about it in the python part of the doc.
-
T tanselgonul has marked this topic as solved on