asammdf icon indicating copy to clipboard operation
asammdf copied to clipboard

csv_bytearray2hex methods is trying to converts bytes into bytes

Open pawpie-otw opened this issue 1 year ago • 1 comments

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()

pawpie-otw avatar Mar 05 '24 10:03 pawpie-otw

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.

danielhrisca avatar Mar 06 '24 11:03 danielhrisca