Skip to content

Commit 2c6a01d

Browse files
authored
device address and pgn request corrections (juergenH87#5)
* Correct device address Add state check to the function send request Correct a missing part at the remove timer function * Addressclaim is allowed without an address. PGN Addressclaim are allowed for nodes without a device address * Adapt examples and Tests Adapt the examples to wait for the device address Adapt the tests to work with the current version of the library * Start the controller application before subscribe. * Subscribe accept now a function * Revert some unneeded changes
1 parent 40c1e7d commit 2c6a01d

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

j1939/controller_application.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def subscribe(self, callback):
6868
:param callback:
6969
Function to call when message is received.
7070
"""
71-
self._ecu.subscribe(callback, self._device_address_preferred)
71+
self._ecu.subscribe(callback, self.message_acceptable)
7272

7373
def unsubscribe(self, callback):
7474
"""Stop listening for message.
@@ -121,7 +121,7 @@ def remove_timer(self, callback):
121121
:param callback:
122122
The callback to be removed from the timer event list
123123
"""
124-
remove_timer(callback)
124+
self._ecu.remove_timer(callback)
125125

126126
def start(self):
127127
"""Starts the CA
@@ -263,16 +263,23 @@ def send_pgn(self, data_page, pdu_format, pdu_specific, priority, data):
263263
if self.state != ControllerApplication.State.NORMAL:
264264
raise RuntimeError("Could not send message unless address claiming has finished")
265265

266-
self._ecu.send_pgn(data_page, pdu_format, pdu_specific, priority, self._device_address_preferred, data)
266+
self._ecu.send_pgn(data_page, pdu_format, pdu_specific, priority, self._device_address, data)
267267

268268
def send_request(self, data_page, pgn, destination):
269269
"""send a request message
270270
:param int data_page: data page
271271
:param int pgn: pgn to be requested
272272
:param list data: destination address
273273
"""
274+
if self.state != ControllerApplication.State.NORMAL:
275+
if pgn != j1939.ParameterGroupNumber.PGN.ADDRESSCLAIM:
276+
raise RuntimeError("Could not send request message unless address claiming has finished")
277+
source_address = j1939.ParameterGroupNumber.Address.NULL
278+
else:
279+
source_address = self._device_address
280+
274281
data = [(pgn & 0xFF), ((pgn >> 8) & 0xFF), ((pgn >> 16) & 0xFF)]
275-
self._ecu.send_pgn(data_page, (j1939.ParameterGroupNumber.PGN.REQUEST >> 8) & 0xFF, destination & 0xFF, 6, self._device_address_preferred, data)
282+
self._ecu.send_pgn(data_page, (j1939.ParameterGroupNumber.PGN.REQUEST >> 8) & 0xFF, destination & 0xFF, 6, source_address, data)
276283

277284
def _send_address_claimed(self, address):
278285
# TODO: Normally the (initial) address claimed message must not be an auto repeat message.

j1939/electronic_control_unit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def notify_subscribers(self, priority, pgn, sa, dest, timestamp, data):
561561
# notify only the CA for which the message is intended
562562
# each CA receives all broadcast messages
563563
for dic in self._subscribers:
564-
if (dic['dev_adr'] == None) or (dest == j1939.ParameterGroupNumber.Address.GLOBAL) or (dest == dic['dev_adr']):
564+
if (dic['dev_adr'] == None) or (dest == j1939.ParameterGroupNumber.Address.GLOBAL) or (callable(dic['dev_adr']) and dic['dev_adr'](dest)) or (dest == dic['dev_adr']):
565565
dic['cb'](priority, pgn, sa, timestamp, data)
566566

567567
def add_ca(self, **kwargs):

test/test_ca.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _send_message(self, can_id, data):
5050
expected_data = self.can_messages.pop(0)
5151
self.assertEqual(expected_data[0], TestCA.MsgType.CANTX, "No transmission was expected")
5252
self.assertEqual(can_id, expected_data[1])
53-
self.assertSequenceEqual(data, expected_data[2])
53+
self.assertSequenceEqual(data[::-1], expected_data[2])
5454
self._inject_messages_into_ecu()
5555

5656
def _on_message(self, pgn, data):
@@ -64,7 +64,7 @@ def _on_message(self, pgn, data):
6464
expected_data = self.pdus.pop(0)
6565
self.assertEqual(expected_data[0], TestCA.MsgType.PDU)
6666
self.assertEqual(pgn, expected_data[1])
67-
self.assertSequenceEqual(data, expected_data[2])
67+
self.assertSequenceEqual(data[::-1], expected_data[2])
6868

6969
def setUp(self):
7070
"""Called before each test methode.

test/test_ecu.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ def _on_message(self, priority, pgn, sa, timestamp, data):
8585
expected_data = self.pdus.pop(0)
8686
self.assertEqual(expected_data[0], TestECU.MsgType.PDU)
8787
self.assertEqual(pgn, expected_data[1])
88-
self.assertSequenceEqual(data, expected_data[2])
88+
if isinstance(data, list):
89+
self.assertSequenceEqual(data, expected_data[2])
90+
else:
91+
self.assertIsNone(data)
8992

9093
def setUp(self):
9194
"""Called before each test methode.
@@ -128,7 +131,7 @@ def tearDown(self):
128131
#def test_connect(self):
129132
# self.ecu.connect(bustype="virtual", channel=1)
130133
# self.ecu.disconnect()
131-
134+
132135
def test_broadcast_receive_short(self):
133136
"""Test the receivement of a normal broadcast message
134137
@@ -151,7 +154,7 @@ def test_broadcast_receive_short(self):
151154
# wait for final processing
152155
time.sleep(0.100)
153156
self.ecu.unsubscribe(self._on_message)
154-
157+
155158
def test_broadcast_receive_long(self):
156159
"""Test the receivement of a long broadcast message
157160
@@ -240,15 +243,15 @@ def test_peer_to_peer_send_short(self):
240243
Its length is 8 Bytes. The contained values are bogous of cause.
241244
"""
242245
self.can_messages = [
243-
(TestECU.MsgType.CANTX, 0x18009B90, [1, 2, 3, 4, 5, 6, 7, 8], 0.0), # PGN 61440
246+
(TestECU.MsgType.CANTX, 0x18F09B90, [1, 2, 3, 4, 5, 6, 7, 8], 0.0), # PGN 61440
244247
]
245248

246249
pdu = (TestECU.MsgType.PDU, 61440, [1, 2, 3, 4, 5, 6, 7, 8])
247250

248251
self.ecu.subscribe(self._on_message)
249252

250253
# sending from 144 to 155 with prio 6
251-
self.ecu.send_pgn(0, pdu[1], 155, 6, 144, pdu[2])
254+
self.ecu.send_pgn(0, pdu[1]>>8, 155, 6, 144, pdu[2])
252255

253256
# wait until all messages are processed asynchronously
254257
while len(self.can_messages)>0:
@@ -265,22 +268,26 @@ def test_peer_to_peer_send_long(self):
265268
Its length is 20 Bytes.
266269
"""
267270
self.can_messages = [
268-
(TestECU.MsgType.CANTX, 0x18EC9B90, [16, 20, 0, 3, 255, 155, 0, 0], 0.0), # TP.CM RTS 1
269-
(TestECU.MsgType.CANRX, 0x1CEC909B, [17, 1, 1, 255, 255, 155, 0, 0], 0.0), # TP.CM CTS 1
271+
(TestECU.MsgType.CANTX, 0x18EC9B90, [16, 20, 0, 3, 255, 0, 223, 0], 0.0), # TP.CM RTS 1
272+
(TestECU.MsgType.CANRX, 0x1CEC909B, [17, 1, 1, 255, 255, 0, 223, 0], 0.0), # TP.CM CTS 1
270273
(TestECU.MsgType.CANTX, 0x1CEB9B90, [1, 1, 2, 3, 4, 5, 6, 7], 0.0), # TP.DT 1
271-
(TestECU.MsgType.CANRX, 0x1CEC909B, [17, 1, 2, 255, 255, 155, 0, 0], 0.0), # TP.CM CTS 2
274+
(TestECU.MsgType.CANRX, 0x1CEC909B, [17, 1, 2, 255, 255, 0, 223, 0], 0.0), # TP.CM CTS 2
272275
(TestECU.MsgType.CANTX, 0x1CEB9B90, [2, 1, 2, 3, 4, 5, 6, 7], 0.0), # TP.DT 2
273-
(TestECU.MsgType.CANRX, 0x1CEC909B, [17, 1, 3, 255, 255, 155, 0, 0], 0.0), # TP.CM CTS 3
276+
(TestECU.MsgType.CANRX, 0x1CEC909B, [17, 1, 3, 255, 255, 0, 223, 0], 0.0), # TP.CM CTS 3
274277
(TestECU.MsgType.CANTX, 0x1CEB9B90, [3, 1, 2, 3, 4, 5, 6, 255], 0.0), # TP.DT 3
275-
(TestECU.MsgType.CANRX, 0x1CEC909B, [19, 20, 0, 3, 255, 155, 0, 0], 0.0), # TP.CM EOMACK
278+
(TestECU.MsgType.CANRX, 0x1CEC909B, [19, 20, 0, 3, 255, 0, 223, 0], 0.0), # TP.CM EOMACK
279+
]
280+
281+
self.pdus = [
282+
(TestECU.MsgType.PDU, 57088, None),
276283
]
277284

278285
pdu = (TestECU.MsgType.PDU, 57088, [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6])
279286

280287
self.ecu.subscribe(self._on_message)
281-
288+
282289
# sending from 144 to 155 with prio 6
283-
self.ecu.send_pgn(0, pdu[1], 155, 6, 144, pdu[2])
290+
self.ecu.send_pgn(0, pdu[1]>>8, 155, 6, 144, pdu[2])
284291

285292
# wait until all messages are processed asynchronously
286293
while len(self.can_messages)>0:
@@ -296,7 +303,7 @@ def test_broadcast_send_long(self):
296303
Its length is 20 Bytes. The contained values are bogous of cause.
297304
"""
298305
self.can_messages = [
299-
(TestECU.MsgType.CANTX, 0x18ECFF90, [32, 20, 0, 3, 255, 255, 176, 0], 0.0), # TP.BAM
306+
(TestECU.MsgType.CANTX, 0x18ECFF90, [32, 20, 0, 3, 255, 176, 254, 0], 0.0), # TP.BAM
300307
(TestECU.MsgType.CANTX, 0x1CEBFF90, [1, 1, 2, 3, 4, 5, 6, 7], 0.0), # TP.DT 1
301308
(TestECU.MsgType.CANTX, 0x1CEBFF90, [2, 1, 2, 3, 4, 5, 6, 7], 0.0), # TP.DT 2
302309
(TestECU.MsgType.CANTX, 0x1CEBFF90, [3, 1, 2, 3, 4, 5, 6, 255], 0.0), # TP.DT 3
@@ -307,7 +314,7 @@ def test_broadcast_send_long(self):
307314
self.ecu.subscribe(self._on_message)
308315

309316
# sending from 144 to GLOABL with prio 6
310-
self.ecu.send_pgn(0, pdu[1], j1939.ParameterGroupNumber.Address.GLOBAL, 6, 144, pdu[2])
317+
self.ecu.send_pgn(0, pdu[1]>>8, pdu[1], 6, 144, pdu[2])
311318

312319
# wait until all messages are processed asynchronously
313320
while len(self.can_messages)>0:

0 commit comments

Comments
 (0)