CSS-Electronics / can_decoder

API module for decoding raw CAN bus data to physical values
MIT License
74 stars 14 forks source link

Allow for in-memory binary stream for "can_decoder.load_dbc()" #1

Closed mtwichan closed 4 years ago

mtwichan commented 4 years ago

Currently only decoding a .dbc from its pathname is supported and I have a use case where I'd like to decode the contents of a .dbc file as an in-memory binary stream (see: link) function below:

can_decoder.load_dbc()

As of now, the function takes in a pathname containing the .dbc file and then reads the file in as an in-memory binary stream.

https://github.com/CSS-Electronics/can_decoder/blob/5bb4d7a9c4d96d794830e135bb90a5b2ceab8b83/can_decoder/DBCLoader.py#L26-L27

An easy fix to support using both the pathname and in-memory binary stream would be simply to remove the line below : https://github.com/CSS-Electronics/can_decoder/blob/5bb4d7a9c4d96d794830e135bb90a5b2ceab8b83/can_decoder/DBCLoader.py#L26

and have the user read data from a path as part of the client code like so:

with open(mdf_path, "rb") as handle:
    dbc_file = can_decoder.load_dbc(handle)
    df_decoder = can_decoder.DataFrameDecoder(dbc_file)

then users who want to pass in the in-memory binary stream can then do:

decoded_str = base64.b64decode(...)
dbc_file  = can_decoder.load_dbc(io.BytesIO(decoded_str))

CC: @dflyon

MatinF commented 4 years ago

Thanks for the suggestion. We believe this should be a fairly simple change that we can update on a development branch.

To ensure we look into the right implementation, can you clarify what the use case scenario is for this? I.e. what way would you be using this functionality in the script?

mtwichan commented 4 years ago

@MatinF great question. I have a use case where I need to upload the files into a full-stack app, and I don't want to save the files temporarily on the server. Instead, the upload functionality converts the files into an ingestable in-memory binary stream stored in the browser and is then used by the above functions to aggregate/munge the data above further :+1:

MatinF commented 4 years ago

Hi, the latest update of the can_decoder tool should enable this - let me know if you get it working

MatinF commented 4 years ago

You can install the dev version via:

pip install -i https://test.pypi.org/simple/ can-decoder

mtwichan commented 4 years ago

@MatinF works for me :+1: