Skip to content

Commit 35893aa

Browse files
authored
convert: selective rx/tx ecu extraction issue #421 (#422)
1 parent a182e95 commit 35893aa

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

docs/cli.rst

+9
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ This is some *lite* ECU-Extract.
242242

243243
$ canconvert --ecus=FRONT_ECU,REAR_ECU source.dbc target.dbc
244244

245+
**extract matrix with frames which FRONT_ECU receives and with frames REAR_ECUS transmits:**
246+
247+
::
248+
249+
$ canconvert --ecus=FRONT_ECU:rx,REAR_ECU:tx source.dbc target.dbc
250+
251+
252+
253+
245254
**extract frame[s] out of matrix:**
246255

247256
::

src/canmatrix/cli/convert.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def get_formats():
7070
@click.option('--skipLongDlc', 'skipLongDlc', help="skip all Frames with dlc bigger than given threshold")
7171
@click.option('--cutLongFrames', 'cutLongFrames', help="cut all signals out of Frames with dlc bigger than given threshold")
7272
@click.option('--deleteFrameAttributes', 'deleteFrameAttributes', help="delete attributes from all frames\nExample --deleteFrameAttributes GenMsgSomeVar,CycleTime")
73-
@click.option('--ecus', help="Copy only given ECUs (comma separated list) to target matrix")
73+
@click.option('--ecus', help="Copy only given ECUs (comma separated list) to target matrix; suffix 'rx' or 'tx' for selection: Example: --ecus FirstEcu:rx,SecondEcu:tx,ThirdEcu")
7474
@click.option('--frames', help="Copy only given Frames (comma separated list) to target matrix")
7575
@click.option('--signals', help="Copy only given Signals (comma separated list) to target matrix just as 'free' signals without containing frame")
7676
@click.option('--merge', help="merge additional can databases.\nSyntax: --merge filename[:ecu=SOMEECU][:frame=FRAME1][:frame=FRAME2],filename2")

src/canmatrix/convert.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,23 @@ def convert(infile, out_file_name, **options): # type: (str, str, **str) -> Non
4646
for name in dbs:
4747
db = None
4848

49-
if 'ecus' in options and options['ecus'] is not None:
49+
if options.get('ecus', False):
5050
ecu_list = options['ecus'].split(',')
5151
db = canmatrix.CanMatrix()
52+
direction = None
5253
for ecu in ecu_list:
53-
canmatrix.copy.copy_ecu_with_frames(ecu, dbs[name], db)
54-
if 'frames' in options and options['frames'] is not None:
54+
if ":" in ecu:
55+
ecu, direction = ecu.split(":")
56+
canmatrix.copy.copy_ecu_with_frames(ecu, dbs[name], db, rx=(direction != "tx"), tx=(direction != "rx"))
57+
if options.get('frames', False):
5558
frame_list = options['frames'].split(',')
56-
db = canmatrix.CanMatrix()
59+
db = canmatrix.CanMatrix() if db is None else db
5760
for frame_name in frame_list:
5861
frame_to_copy = dbs[name].frame_by_name(frame_name)
5962
canmatrix.copy.copy_frame(frame_to_copy.arbitration_id, dbs[name], db)
6063
if options.get('signals', False):
6164
signal_list = options['signals'].split(',')
62-
db = canmatrix.CanMatrix()
65+
db = canmatrix.CanMatrix() if db is None else db
6366
for signal_name in signal_list:
6467
canmatrix.copy.copy_signal(signal_name, dbs[name], db)
6568

src/canmatrix/copy.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def copy_ecu(ecu_or_glob, source_db, target_db):
6565
target_db.ecu_defines[attribute].update()
6666

6767

68-
def copy_ecu_with_frames(ecu_or_glob, source_db, target_db):
68+
def copy_ecu_with_frames(ecu_or_glob, source_db, target_db, rx=True, tx=True):
6969
# type: (typing.Union[canmatrix.Ecu, str], canmatrix.CanMatrix, canmatrix.CanMatrix) -> None
7070
"""
7171
Copy ECU(s) identified by Name or as Object from source CAN matrix to target CAN matrix.
@@ -87,16 +87,18 @@ def copy_ecu_with_frames(ecu_or_glob, source_db, target_db):
8787
target_db.add_ecu(copy.deepcopy(ecu))
8888

8989
# copy tx-frames
90-
for frame in source_db.frames:
91-
if ecu.name in frame.transmitters:
92-
copy_frame(frame.arbitration_id, source_db, target_db)
90+
if tx is True:
91+
for frame in source_db.frames:
92+
if ecu.name in frame.transmitters:
93+
copy_frame(frame.arbitration_id, source_db, target_db)
9394

9495
# copy rx-frames
95-
for frame in source_db.frames:
96-
for signal in frame.signals:
97-
if ecu.name in signal.receivers:
98-
copy_frame(frame.arbitration_id, source_db, target_db)
99-
break
96+
if rx is True:
97+
for frame in source_db.frames:
98+
for signal in frame.signals:
99+
if ecu.name in signal.receivers:
100+
copy_frame(frame.arbitration_id, source_db, target_db)
101+
break
100102

101103
# copy all ECU defines
102104
for attribute in ecu.attributes:

src/canmatrix/tests/test_cli_convert.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def test_copy_signals(tmpdir, run):
194194
assert b"VECTOR__INDEPENDENT_SIG_MSG" in content
195195

196196

197-
def create_dbc():
197+
def create_dbc(additionalReceiver = []):
198198
outFile = str(here / "tmpb.dbc")
199199
myFrame = canmatrix.Frame("testFrame3", arbitration_id=canmatrix.arbitration_id_converter(0x124), size=8, transmitters=["testBU"])
200200
mySignal = canmatrix.Signal("someTestSignal",
@@ -218,7 +218,7 @@ def create_dbc():
218218
offset=1.0,
219219
min=0,
220220
max=500,
221-
receivers=["recBU2"])
221+
receivers=["recBU2"] + additionalReceiver)
222222
myFrame2.add_signal(mySignal2)
223223
mySignal3 = canmatrix.Signal("zeroSignal",
224224
start_bit=20,
@@ -250,6 +250,22 @@ def test_copy_ecus(tmpdir, run):
250250
assert "testBU2" not in content
251251
assert "testBU" in content
252252

253+
def test_copy_ecus_rx(tmpdir, run):
254+
inputFile = create_dbc()
255+
result = run("--ecus", "recBU:rx", inputFile, "tmp2.dbc")
256+
with open("tmp2.dbc","r") as fd:
257+
content = fd.read()
258+
assert "recBU2" not in content
259+
assert "recBU" in content
260+
261+
def test_copy_ecus_tx(tmpdir, run):
262+
inputFile = create_dbc(additionalReceiver = ["testBU"])
263+
result = run("--ecus", "testBU:tx", inputFile, "tmp2.dbc")
264+
with open("tmp2.dbc","r") as fd:
265+
content = fd.read()
266+
assert "testFrame2" not in content
267+
assert "testFrame3" in content
268+
253269
def test_copy_frames(tmpdir, run):
254270
inputFile = create_dbc()
255271
result = run("--frames", "testFrame3", inputFile, "tmp2.dbc")

0 commit comments

Comments
 (0)