Example #1
0
class VectorDevice(object):
    """

    """
    def __init__(self,
                 canalyzer_config_path,
                 canape_config_path,
                 baud_rate,
                 a2l_path,
                 capl_path=None,
                 channel=2,
                 device_type="CCP",
                 logger=None):
        self.logger = logger or logging.getLogger(__name__)
        self._capl_funcs = {}
        self._capl_path = capl_path
        if self._capl_path is not None:
            self._capl_names = self._get_capl_fun_names()
        self._CANalyzer = DispatchEx('CANalyzer.Application')
        self._CANalyzer.Open(canalyzer_config_path)
        self._CANalyzer.CAPL.Compile()
        global CANalyzer
        CANalyzer = self
        event_handler = win32com.client.WithEvents(self._CANalyzer.Measurement,
                                                   MeasEvents)
        self._CANalyzer.UI.Write.Output('measurement starting...')
        self._CANalyzer.Measurement.Start()
        while (not self._CANalyzer.Measurement.Running):
            time.sleep(1)
        self._CANalyzer.UI.Write.Output('measurement started...')
        self.logger.info("Canalyzer measurement started.")

        # init the CANape
        self._Canape = DispatchEx('CANape.Application')
        self._Canape.Open1(canape_config_path, 1, baud_rate, True)
        self._CanapeDev = self._Canape.Devices.Add(device_type, a2l_path,
                                                   device_type, channel)
        pass

    def close(self):
        self._Canape.Quit()
        self._CANalyzer.Quit()

    def _get_capl_fun_names(self):
        re_exp = "^(void|int|byte|word|dword|long|int64|qword)\s+([a-zA-Z0-9_]+)\s*\((void|int|byte|word|dword|long|int64|qword|float)\s*"
        names = []
        with open(self._capl_path) as capl:
            text = capl.read()
            res = re.findall(re_exp, text, re.M)
            names = [i[1] for i in res]
            self.logger.debug("CAPL functions:{0}".format(names))
        return names

    def get_capl_names(self):
        return self._capl_names

    def update_capl_funs(self):
        for name in self._capl_names:
            obj = self._CANalyzer.CAPL.GetFunction(name)
            self._capl_funcs[name] = obj

    def get_capl_func(self, name):
        if name in self._capl_funcs:
            return self._capl_funcs[name]
        else:
            return None

    def ChangeEcuCalib(self, case, *args):
        name = case.ActionPar1
        value = case.ActionPar2 if not str(
            case.ActionPar2).upper().startswith("0X") else int(
                case.ActionPar2, 16)
        self._CanapeDev.CalibrationObjects.Add(name)
        calib_obj = self._CanapeDev.CalibrationObjects.Item(name)
        calib_obj.Value = value
        calib_obj.Write()
        case.RealVal = case.TestRes = "PASS"
        self.logger.info(case)
        return case.TestRes
class PythonCanalyzer(object):
    """

    """
    def __init__(self, canalyzer_config_path, capl_path=None, logger=None):
        self.logger = logger or logging.getLogger(__name__)
        self._capl_funcs = {}
        self._capl_path = capl_path
        self._CANalyzer = DispatchEx('CANalyzer.Application')
        self._CANalyzer.Open(canalyzer_config_path)
        if self._capl_path is not None:
            PythonCanalyzer._capl_names = self._get_capl_fun_names()
            self._CANalyzer.CAPL.Compile()
        global CANalyzer
        CANalyzer = self
        self._event_handler = win32com.client.WithEvents(
            self._CANalyzer.Measurement, MeasEvents)
        self._CANalyzer.UI.Write.Output('measurement starting...')
        self._CANalyzer.Measurement.Start()
        while (not self._CANalyzer.Measurement.Running):
            time.sleep(1)
        self._CANalyzer.UI.Write.Output('measurement started...')
        self.logger.info("Canalyzer measurement started.")

    def close(self):
        self._CANalyzer.Quit()

    def _get_capl_fun_names(self):
        re_exp = "^(void|int|byte|word|dword|long|int64|qword)\s+([a-zA-Z0-9_]+)\s*\((void|int|byte|word|dword|long|int64|qword|float)\s*"
        names = []
        with open(self._capl_path) as capl:
            text = capl.read()
            res = re.findall(re_exp, text, re.M)
            names = [i[1] for i in res]
            self.logger.debug("CAPL functions:{0}".format(names))
        return names

    def get_capl_names(self):
        return self._capl_names

    def update_capl_funs(self):
        for name in PythonCanalyzer._capl_names:
            obj = self._CANalyzer.CAPL.GetFunction(name)
            self._capl_funcs[name] = obj

    def get_capl_func(self, name):
        if name in self._capl_funcs:
            return self._capl_funcs[name]
        else:
            return None

    def get_can_bus_signal_value(self, ch, msg, signal):
        res = float(str(self._CANalyzer.Bus.GetSignal(ch, msg, signal)))
        return res

    def send_can_bus_signal_value(self, ch, msg, sig, value):
        """
        """
        ret_value = 0
        name = msg + "_" + sig
        if name in self._capl_funcs:
            func = self._capl_funcs[name]
            if value:
                func.Call(value)
            else:
                func.Call()
            ret_value = 1
        else:
            ret_value = 0
            self.logger.error("{} CAPL function:{} not founded.".format(
                sys._getframe().f_code.co_name, name))
            raise NameError
        return ret_value

    def call_capl(self, name, value):
        ret_value = 0
        if name in self._capl_funcs:
            func = self._capl_funcs[name]
            if value:
                func.Call(value)
            else:
                func.Call()
            ret_value = 1
        else:
            ret_value = 0
            self.logger.error("{} CAPL function:{} not founded.".format(
                sys._getframe().f_code.co_name, name))
            raise NameError
        return ret_value