Ask example for Listeners

Issue #50 resolved
LongQi Zhang created an issue

The API is pretty straightforward. But I still don't know how the listener works after reading the documents, could you create a example of how to use this class to create my own.

Comments (8)

  1. Mike Molt

    I could use some examples as well for listeners. For instance, using the BufferedReader subclass. I'm not exactly sure how to properly access the messages put into the buffer.

  2. Brian Thorne repo owner

    Did you see the can.Notifier class?

    a_listener = can.BufferedReader()
    notifier = can.Notifier(self.bus, [a_listener])
    m = a_listener.get_message(TIMEOUT)
    # m will be None or a Message
    

    But yes there should be some tests and documentation for the Notifiers and Listeners.

  3. Mike Molt

    Brian, thank you!

    So let me make sure I have this straight.

    1. The notifier catches the event that something new has been placed on the bus, and then calls the designated listener to react to the event. In the case we're talking about, the listener puts the can message in a buffer. Is that correct so far?

    2. The BufferedReader subclass has a method get_message utilized to get the latest can message out of the buffer to do something with. This is pretty straight forward.

    ...here's what I'm not clear on, and this is probably my programming 'noobieness' showing. I think I understand why using a method to get a can message as it is received is more efficient than continually 'checking' the bus with some sort of loop...so that is good I think. What I struggle with is figuring out a way to create an event in the rest of my program to actually get_message so I can do something with it.

  4. Mike Molt

    Okay, I'm answering myself here with the hopes that someone else may benefit. A producer consumer type arrangement for getting and displaying the messages in the queue created by the BufferedReader subclass is the way to go here...particularly for GUI apps. For those learning like me, the author Mark Lutz covers this concept really well in the book 'Programming Python'.

  5. Brian Thorne repo owner

    Only other point I'll make is there are often multiple consumers attached to a bus. The Notifier/Listener api means that all consumers will receive the message.

  6. Mehdi Tlili

    Is is that hard to write a code snippet doing the thing? I mean, probably what most people want is a script than keeps running, printing new messages whenever they arrive. I don't see any registerCallBack similar function in python-can

    this is what I have now

    class CanListener(can.Listener):
        def on_message_received(self, msg):
            print msg
    
    can_filters = [{"can_id": "0x1c1"}]
    bus = can.interface.Bus("can0", bustype="socketcan_ctypes", can_filters=can_filters)
    a_listener = CanListener()
    notifier = can.Notifier(bus, [a_listener])
    

    But I don't know how to activate this listener. My script now just goes through the declaration and finishes.

  7. Log in to comment