Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csv_bytearray2hex methods is trying to converts bytes into bytes #989

Open
pawpie-otw opened this issue Mar 5, 2024 · 1 comment
Open

Comments

@pawpie-otw
Copy link

Python error:
File "...\CSV2MDF4.venv39\lib\site-packages\numpy\lib\function_base.py", line 2455, in _vectorize_call
outputs = ufunc(*inputs)
File "...\CSV2MDF4.venv39\lib\site-packages\asammdf\blocks\utils.py", line 1698, in csv_bytearray2hex
val = val.tobytes()[:size].hex(" ", 1).upper()

<class 'AttributeError'>: 'bytes' object has no attribute 'tobytes'
2024-03-05, 11:26:32

<class 'AttributeError'>
'bytes' object has no attribute 'tobytes'

I converted the below data

TimestampEpoch;BusChannel;ID;IDE;DLC;DataLength;Dir;EDL;BRS;DataBytes
1670926807.419500;1;000;0;1;1;0;1;1;AA
1670926807.419700;1;000;0;2;2;0;1;1;AAAA
1670926807.419900;1;000;0;3;3;0;1;1;AAAAAA
1670926807.420100;1;000;0;4;4;0;1;1;AAAAAAAA
1670926807.420300;1;000;0;5;5;0;1;1;AAAAAAAAAA
1670926807.420500;1;000;0;6;6;0;1;1;AAAAAAAAAAAA
1670926807.420700;1;000;0;7;7;0;1;1;AAAAAAAAAAAAAA
1670926807.420900;1;000;0;8;8;0;1;1;AAAAAAAAAAAAAAAA

to mdf4 by below script"

df = pd.read_csv('input/can.csv', sep=';')
mdf = MDF(version='4.20')
mdf.append(df)
mdf.save('output/output.mf4')

I ran the program (asammdfgui.py/exe)
I loaded the data (.mdf4)
I chose the window type 'tabular'
Then the error appears

Here's my solution:

def csv_bytearray2hex(val, size: int | None = None) -> str:
    """format CAN payload as hex strings

    b'\xa2\xc3\x08' -> A2 C3 08

    """
    if not isinstance(val, bytes):
        val = val.tobytes()
    if size is not None:
        val = val[:size]
    return val.hex(" ", 1).upper()
@danielhrisca
Copy link
Owner

The source of the problem in your case is that the DataBytes is loaded from the dataframe as a category dtype, instead of as bytearray as is normally is for bus logging payload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants