I am working with CAN-Bus Data coming in at over 100hz (in bursts) and need to store all the messages as quickly as possible so that the next message can be read from the CAN Chip Buffer. But which way is the quickest way to store this data in micropython?
For those who haven't worked with CAN before, it comes as a series of 8-byte messages with specific IDs, so I will need to quickly store 8 bytes at a time.
These are my thoughts so far (based on my limited experience with micropython):
1) Built-in Data Types:
- List: Obviously appending to a list every time is slow as the memory has to be allocated but if the list was created beforehand with null
values then filled like a buffer might not be as slow? - Bytearray: The obvious choice to write to, but is it quick enough in micropython?
- Default_dict: Creating a default dictionary with byte arrays for each value, and writing each 8 Byte message into the byte array to be
processed later (Not the best Idea I know...)
- I trialled memory view objects by 'pointing' them at a bytearray and it seemed to work well
- I couldn't use the slice notation which means I would have to copyt everything out of the buffer foirst before working on it
Code: Select all
>>> example_bytearray = bytearray(64) >>> mem_view_obj = memory_view(example_bytearray) >>> mem_view_obj = memoryview(example_bytearray) >>> print(mem_view_obj[8:16]) <memoryview> >>> print(example_bytearray[8:16]) bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') >>>
- I haven't worked with arrays much in Python so are they quick to write to?
- What are their limitations in micropython?
- I am aware that this module was designed to interface with functions or libraries written in C, but if it offers the ability to create c-type structures does that mean it offers a similar speed?
- How difficult would it be to implement?
I appreciate this is a bit of an abstract question but was hoping those with a bit more experience using micropython had come into a similar problem before and may already have worked out the quickest way of doing it!