tao_z
2022-06-06 20cc603922983f154d10b7d56c6b49e5e02997bd
dbc.py
@@ -22,8 +22,10 @@
UNSIGNED = 0
SIGNED = 1
def normalizeValueTable(table):
    return {int(k): v for k, v in table.items()}
@attr.s(cmp=False)
class Frame:
@@ -100,7 +102,8 @@
        for sig in self.signals:
            i += 1
            for bit in range(sig.getStartbit(),  sig.getStartbit() + int(sig.size)):
            for bit in range(sig.getStartbit(),
                             sig.getStartbit() + int(sig.size)):
                if sig.is_little_endian:
                    bitfieldLe[bit] = i
                else:
@@ -114,7 +117,6 @@
            for j in range(0,8):
                if bitfield[i*8+j] == 0:
                    bitfield[i*8+j] = bitfieldBe[i*8+j]
        return bitfield
@@ -156,11 +158,13 @@
    #    offset = attr.ib(converter = float_factory, default = 0.0)
    min  = attr.ib(converter=float_factory)
    @min.default
    def setDefaultMin(self):
        return  self.calcMin()
    max =  attr.ib(converter = float_factory)
    @max.default
    def setDefaultMax(self):
        return  self.calcMax()
@@ -175,7 +179,9 @@
    enumeration = attr.ib(type=str, default = None)
    comments = attr.ib(type=dict, default = attr.Factory(dict))
    attributes = attr.ib(type=dict, default = attr.Factory(dict))
    values = attr.ib(type=dict, converter=normalizeValueTable, default = attr.Factory(dict))
    values = attr.ib(type=dict,
                     converter=normalizeValueTable,
                     default=attr.Factory(dict))
    calc_min_for_none = attr.ib(type=bool, default = True)
    calc_max_for_none = attr.ib(type=bool, default = True)
    muxValMax = attr.ib(default = 0)
@@ -184,7 +190,6 @@
    def __attrs_post_init__(self):
        self.multiplex = self.multiplexSetter(self.multiplex)
    @property
    def spn(self):
@@ -253,7 +258,8 @@
            startBitInternal = startBitInternal + self.size - 1
        # bit numbering not consistent with byte order. reverse
        if bitNumbering is not None and bitNumbering != self.is_little_endian:
            startBitInternal = startBitInternal - (startBitInternal % 8) + 7 - (startBitInternal % 8)
            startBitInternal = startBitInternal - (
                startBitInternal % 8) + 7 - (startBitInternal % 8)
        return int(startBitInternal)
    def calculateRawRange(self):
@@ -304,15 +310,12 @@
                    value = value_key
                    break
            else:
                raise ValueError(
                        "{} is invalid value choice for {}".format(value, self)
                )
                raise ValueError("{} is invalid value choice for {}".format(
                    value, self))
        if not (self.min <= value <= self.max):
            print(
                "Value {} is not valid for {}. Min={} and Max={}".format(
                    value, self, self.min, self.max)
                )
            print("Value {} is not valid for {}. Min={} and Max={}".format(
                value, self, self.min, self.max))
        raw_value = (value - self.offset) / self.factor
        if not self.is_float:
@@ -338,7 +341,9 @@
    def __str__(self):
        return self.name
class DBC:
    def __init__(self, dbcfile=None):
        self.filePath = dbcfile
        self.frames = self.__load()
@@ -349,7 +354,7 @@
        frameList = []
        i = 0
        frame = None
        with open(self.filePath, mode='r', encoding='gb2312') as f:
        with open(self.filePath, mode='r', encoding=dbcImportEncoding) as f:
            for line in f.readlines():
                i = i + 1
                l = line.strip()
@@ -358,11 +363,11 @@
                # logging.info(l)
                if l.startswith('BO_ '):
                    # frames
                    regexp = re.compile("^BO\_ ([^\ ]+) ([^\ ]+) *: ([^\ ]+) ([^\ ]+)")
                    regexp = re.compile(
                        "^BO\_ ([^\ ]+) ([^\ ]+) *: ([^\ ]+) ([^\ ]+)")
                    temp = regexp.match(l)
                    # name, id, dlc, transmitters
                    frame = Frame(
                        temp.group(2),
                    frame = Frame(temp.group(2),
                        id=int(temp.group(1)),
                        size=int(temp.group(3)),
                        transmitters=temp.group(4).split())
@@ -375,12 +380,14 @@
                    temp = regexp.match(l)
                    if temp:
                        extras = {}
                        receiver = list(map(str.strip, temp.group(11).split(',')))
                        tempSig = Signal(
                            temp.group(1),
                        receiver = list(
                            map(str.strip,
                                temp.group(11).split(',')))
                        tempSig = Signal(temp.group(1),
                            startBit=int(temp.group(2)),
                            size=int(temp.group(3)),
                            is_little_endian=(int(temp.group(4)) == 1),
                                         is_little_endian=(int(
                                             temp.group(4)) == 1),
                            is_signed=(temp.group(5) == '-'),
                            factor=temp.group(6),
                            offset=temp.group(7),
@@ -388,17 +395,19 @@
                            max=temp.group(9),
                            unit=temp.group(10),
                            receiver=receiver,
                            **extras
                        )
                                         **extras)
                        if not tempSig.is_little_endian:
                            # startbit of motorola coded signals are MSB in dbc
                            tempSig.setStartbit(int(temp.group(2)), bitNumbering=1)
                            tempSig.setStartbit(int(temp.group(2)),
                                                bitNumbering=1)
                        frame.signals.append(tempSig)
                    else:
                        pattern = "^SG\_ +(\w+) +(\w+) *: *(\d+)\|(\d+)@(\d+)([\+|\-]) +\(([0-9.+\-eE]+),([0-9.+\-eE]+)\) +\[([0-9.+\-eE]+)\|([0-9.+\-eE]+)\] +\"(.*)\" +(.*)"
                        regexp = re.compile(pattern)
                        temp = regexp.match(l)
                        receiver = list(map(str.strip, temp.group(12).split(',')))
                        receiver = list(
                            map(str.strip,
                                temp.group(12).split(',')))
                        multiplex = temp.group(2)
                        is_complex_multiplexed = False
@@ -417,11 +426,11 @@
                        extras = {}
                        tempSig = Signal(
                            temp.group(1),
                        tempSig = Signal(temp.group(1),
                            startBit=int(temp.group(3)),
                            size=int(temp.group(4)),
                            is_little_endian=(int(temp.group(5)) == 1),
                                         is_little_endian=(int(
                                             temp.group(5)) == 1),
                            is_signed=(temp.group(6) == '-'),
                            factor=temp.group(7),
                            offset=temp.group(8),
@@ -430,8 +439,7 @@
                            unit=temp(11),
                            receiver=receiver,
                            multiplex=multiplex,
                            **extras
                        )
                                         **extras)
                        if is_complex_multiplexed:
                            tempSig.is_multiplexer = True
@@ -439,7 +447,8 @@
                        if not tempSig.is_little_endian:
                            # startbit of motorola coded signals are MSB in dbc
                            tempSig.setStartbit(int(temp.group(3)), bitNumbering=1)
                            tempSig.setStartbit(int(temp.group(3)),
                                                bitNumbering=1)
                        frame.addSignal(tempSig)
                        if is_complex_multiplexed:
@@ -468,8 +477,10 @@
                        if testF.id == int(tmpId):
                            for signal in testF.signals:
                                if signal.name == signalName:
                                    for i in range(math.floor(len(tempList) / 2)):
                                        signal.values[tempList[i * 2].strip()] = tempList[i * 2 + 1].strip()
                                    for i in range(
                                            math.floor(len(tempList) / 2)):
                                        signal.values[tempList[i * 2].strip(
                                        )] = tempList[i * 2 + 1].strip()
                                    break
                            break
                elif l.startswith("VAL_TABLE_ "):
@@ -491,7 +502,8 @@
                    regexp = re.compile("^BA\_ +\"[A-Za-z0-9[\-_ .]+\" +(.+)")
                    tempba = regexp.match(l)
                    if tempba.group(1).strip().startswith("BO_ "):
                        regexp = re.compile(r"^BA_ +\"(.*)\" +BO_ +(\w+) +(.+);")
                        regexp = re.compile(
                            r"^BA_ +\"(.*)\" +BO_ +(\w+) +(.+);")
                        temp = regexp.match(l)
                        tempId = temp.group(2)
                        for testF in frameList:
@@ -501,10 +513,12 @@
                            tempCys = temp.group(3)
                            frame.cycle = int(tempCys)
                elif l.startswith("SIG_GROUP_ "):
                    regexp = re.compile("^SIG\_GROUP\_ +(\w+) +(\w+) +(\w+) +\:(.*);")
                    regexp = re.compile(
                        "^SIG\_GROUP\_ +(\w+) +(\w+) +(\w+) +\:(.*);")
                    temp = regexp.match(l)
                elif l.startswith("SIG_VALTYPE_ "):
                    regexp = re.compile("^SIG\_VALTYPE\_ +(\w+) +(\w+)\s*\:(.*);")
                    regexp = re.compile(
                        "^SIG\_VALTYPE\_ +(\w+) +(\w+)\s*\:(.*);")
                    temp = regexp.match(l)
                elif l.startswith("BA_DEF_DEF_ "):
                    pattern = "^BA\_DEF\_DEF\_ +\"([A-Za-z0-9\-_\.]+)\" +(.+)\;"
@@ -527,7 +541,7 @@
        for frame in self.frames:
            # if frame.size == 0:
            frame.calcDLC()
            print(frame.id, frame.size)
            # print(frame.id, frame.size)
            if frame.size > 8:
                frame.is_fd = True
    
@@ -615,7 +629,11 @@
                break
        return ret
if __name__ == '__main__':
    testDbc = DBC("data/SX7H.dbc")
    testDbc = DBC("DBC/DFLZM.dbc")
    abc=testDbc.analyzer(msgid=1056, data='40 01 C8 00 FF 00 00 00')
    print(abc)
    print(abc[5]["name"])
    print(abc[6]["name"])
    print(abc[4]["name"])