ebroecker / canmatrix

Converting Can (Controller Area Network) Database Formats .arxml .dbc .dbf .kcd ...
BSD 2-Clause "Simplified" License
926 stars 400 forks source link

canmatrix.py --> decimal.InvalidOperation: [<class 'decimal.DivisionUndefined'>] #773

Closed daestx closed 5 months ago

daestx commented 8 months ago

I have an issue with doing a diff on two DBC files. I get following debug info:

File "canmatrix\formats__init__.py", line 79, in loadp_flat File "canmatrix\formats__init.py", line 71, in loadp File "canmatrix\formats\init__.py", line 90, in load File "canmatrix\formats\dbc.py", line 960, in load File "canmatrix\canmatrix.py", line 450, in phys2raw decimal.InvalidOperation: [<class 'decimal.DivisionUndefined'>]

coming from this line raw_value = (self.float_factory(value) - self.float_factory(self.offset)) / self.float_factory(self.factor)

obviously, division by 0 is not handled correctly.

I changed the code as workaround

if self.factor == 0:
    self.factor = 1

raw_value = (self.float_factory(value) - self.float_factory(self.offset)) / self.float_factory(self.factor)

please check if there is a better solution

daestx commented 8 months ago

issue #766 looks similar

ebroecker commented 8 months ago

Some Idea:

canmatrix for now only uses COMPU-INTERNAL-TO-PHYS compu-methods,

Maybe your ARXML uses COMPU-PHYS-TO-INTERNAL.

daestx commented 8 months ago

I used dbc file as input not an ARXML

ebroecker commented 8 months ago

Oh sorry you are right...

Do you have a chance to check the dbc itself? A factor of 0 does not make sense at all. I just want to know if it's a problem with canmatrix or with the dbc file...

daestx commented 8 months ago

So, I checked the DBC file now and identified, that the DBC is buggy. According the documentation in https://www.influxtechnology.com/post/understanding-can-dbc

image

So, I presume this definition of a DBC file I received is wrong, right?

image

So the question to me is if such an issue shall be handled in the library. I don't know as well if the scaling factor shall not be 0. So far, I didn't find any CANdbc file spec. But what I see from Vectors CANdb++ tool, the factor for the given signal was interpreted as 1.

ebroecker commented 8 months ago

Hi @daestx

thanks for your analysis. Yes the dbc defines the scale (=factor) to zero which is nonsense. Seems so, that candb++ has some plausibility-check here to get around with a scale of 0. So maybe canmatrix should do the same...

Maybe I'll add some warning if scale is 0 and set it to 1

ebroecker commented 5 months ago

should be merged