Open HourGlss opened 1 year ago
Just found this... canio
has a more thorough description of Match
:
https://docs.circuitpython.org/en/latest/shared-bindings/canio/index.html#canio.Match
I also have printing spam problem. Best would be a flag to silence it. Is any one planning to work on this?
UPDATE: every easy to download the bundle git, follow the instructions, comment out the offending print(), generate a new bundle and use that version of MCP2515
I actually think the problem lies in the examples and how they recommend using the "listen()" and "receive()" methods.
The 'listen()' method is where the listener is created and also the place where that unused mask message is being printed. If the listen() method is called inside the 'while true:' loop it'll recreate the listener and also display that message on every loop.
Also, according to the documentation it might not be wise to recreate the listener every time due to the cost of creating it...
Creating a listener is an expensive operation and can interfere with reception of messages by other listeners.
I think a better approach (if you're masks and filters don't change over time) is to call the listen() method once outside of the 'while True:' loop. Then just call the receive() method inside the loop to get the incoming messages that haven't been processed. This also has the side benefit of making your while loop much faster (see comment above). So far, this approach has worked very well for me.
# using an array with only one match
matches = [
adafruit_mcp2515.Match(0x060, mask=0x7F0)
]
# Setup the CAN Bus and Listener.
mcp = adafruit_mcp2515.MCP2515(spi, cs, baudrate=1000000)
listener = mcp.listen(matches, timeout=.002)
# You should only see the warning message only once using this approach
while True:
# use a for loop to process all messages received since the last time this was called.
message_count = listener.in_waiting()
print(message_count, "messages available")
for _i in range(message_count):
msg = listener.receive()
# Handle the incoming messages here.
print("Message from ", hex(msg.id))
if isinstance(msg, Message):
print("message data:", msg.data)
if isinstance(msg, RemoteTransmissionRequest):
print("RTR length:", msg.length)
Hope that helps!
Please see test case:
This produces a LOT of spam on the serial log.
intended functionality would be: the mcp2515 can be used with one mask without something printing.
Adding ANOTHER mask removes the print.
this seems to be the problem... but I think it may point to a larger issue. https://github.com/adafruit/Adafruit_CircuitPython_MCP2515/blob/a50e24362154677d262e643c8edec9237f810a77/adafruit_mcp2515/__init__.py#L917
Lastly, adding some documentation to Mask would be pretty cool. https://docs.circuitpython.org/projects/mcp2515/en/latest/api.html#adafruit_mcp2515.canio.Match