|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import canmatrix.formats |
| 4 | +import sys |
| 5 | +import optparse |
| 6 | + |
| 7 | +# command line options... |
| 8 | +usage = """ |
| 9 | +%prog [options] matrix |
| 10 | +
|
| 11 | +matrixX can be any of *.dbc|*.dbf|*.kcd|*.arxml |
| 12 | +""" |
| 13 | + |
| 14 | +parser = optparse.OptionParser(usage=usage) |
| 15 | +parser.add_option( |
| 16 | + "-f", "--frames", |
| 17 | + dest="frames", |
| 18 | + help="encode list of frames", |
| 19 | + default="*") |
| 20 | + |
| 21 | +(cmdlineOptions, args) = parser.parse_args() |
| 22 | + |
| 23 | +if len(args) < 1: |
| 24 | + parser.print_help() |
| 25 | + sys.exit(1) |
| 26 | + |
| 27 | +# load matrix |
| 28 | +db = canmatrix.formats.loadp(args[0], flat_import=True) |
| 29 | + |
| 30 | +#get all frames which match the commandline |
| 31 | +frames = db.glob_frames(cmdlineOptions.frames) |
| 32 | + |
| 33 | +#helper to read physical value from user |
| 34 | +def read_signal_value_from_user(signal): |
| 35 | + a = input("Enter Value for " + signal.name + " ") |
| 36 | + |
| 37 | + if signal.is_float: |
| 38 | + return float(a) |
| 39 | + else: |
| 40 | + return int(a) |
| 41 | + |
| 42 | +# go through all frames |
| 43 | +for frame in frames: |
| 44 | + print (frame.name) |
| 45 | + |
| 46 | + if frame.is_complex_multiplexed: |
| 47 | + # ignore complex multiplexed signals |
| 48 | + continue |
| 49 | + if frame.is_multiplexed: |
| 50 | + # if multiplexed frame search for multiplexer |
| 51 | + multiplexer_signal = None |
| 52 | + for signal in frame.signals: |
| 53 | + if signal.is_multiplexer: |
| 54 | + multiplexer_signal = signal |
| 55 | + |
| 56 | + signalDict = dict() |
| 57 | + |
| 58 | + # read multiplexer value |
| 59 | + a = input("Enter Value for Multiplexer " + multiplexer_signal.name + " ") |
| 60 | + signalDict[multiplexer_signal.name] = int(a) |
| 61 | + |
| 62 | + # read signals for the given multiplexer value |
| 63 | + for signal in [s for s in frame.signals if (s.mux_val == int(a) or (s.mux_val is None and not s.is_multiplexer) )]: |
| 64 | + signalDict[signal.name] = read_signal_value_from_user(signal) |
| 65 | + |
| 66 | + else: |
| 67 | + # not multiplexed frame |
| 68 | + signalDict = dict() |
| 69 | + # go through all signals |
| 70 | + for signal in frame.signals: |
| 71 | + signalDict[signal.name] = read_signal_value_from_user(signal) |
| 72 | + |
| 73 | + frame_data = frame.encode(signalDict) |
| 74 | + if frame.arbitration_id.extended: |
| 75 | + print("{:05X}#".format(frame.arbitration_id.id) + "".join(["%02X" % i for i in frame_data])) |
| 76 | + else: |
| 77 | + print("{:03X}#".format(frame.arbitration_id.id) + "".join(["%02X" % i for i in frame_data])) |
0 commit comments