-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented so far: - multi-pg send/receive with possibility to enter a time limit - Broadcast Announce Message (BAM) send/receive with up to 4 concurrent sessions and up to 15300 bytes of data per session - RTS/CTS (Destination Specific) send/receive with up to 8 concurrent sessions and up to 16777215 bytes of data per session added j1939-22 transport protocol and multi-pg examples the data-link-layer (j1939-21 or j1939-22) can be selected when creating the ECU instance. the data link layer cannot be changed dynamically, because j1939 does not allow mixing of data link layers either.
- Loading branch information
juergen
committed
Jun 27, 2021
1 parent
99ec5f3
commit 546e4e7
Showing
14 changed files
with
1,738 additions
and
677 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import logging | ||
import time | ||
import j1939 | ||
|
||
|
||
logging.getLogger('j1939').setLevel(logging.DEBUG) | ||
logging.getLogger('can').setLevel(logging.DEBUG) | ||
|
||
|
||
def on_message(priority, pgn, sa, timestamp, data): | ||
"""Receive incoming messages from the bus | ||
:param int priority: | ||
Priority of the message | ||
:param int pgn: | ||
Parameter Group Number of the message | ||
:param int sa: | ||
Source Address of the message | ||
:param int timestamp: | ||
Timestamp of the message | ||
:param bytearray data: | ||
Data of the PDU | ||
""" | ||
print("PGN {} length {}".format(pgn, len(data)), timestamp) | ||
|
||
|
||
def ca_timer_callback1(ca): | ||
"""Callback for sending messages | ||
This callback is registered at the ECU timer event mechanism to be | ||
executed every 500ms. | ||
:param cookie: | ||
A cookie registered at 'add_timer'. May be None. | ||
""" | ||
# wait until we have our device_address | ||
if ca.state != j1939.ControllerApplication.State.NORMAL: | ||
# returning true keeps the timer event active | ||
return True | ||
|
||
# create data with 20 bytes | ||
data = [j1939.ControllerApplication.FieldValue.NOT_AVAILABLE_8] * 20 | ||
|
||
# sending broadcast message | ||
# the following two PGNs are packed into one multi-pg, due to time-limit of 10ms and same destination address (global) | ||
ca.send_pgn(0, 0xFD, 0xED, 6, data, time_limit=0.01) | ||
ca.send_pgn(0, 0xFE, 0x32, 6, data, time_limit=0.01) | ||
|
||
# sending normal peer-to-peer message, destintion address is 0x04 | ||
# the following PGNs are transferred separately, because time limit == 0 | ||
ca.send_pgn(0, 0xE0, 0x04, 6, data) | ||
ca.send_pgn(0, 0xD0, 0x04, 6, data) | ||
|
||
# returning true keeps the timer event active | ||
return True | ||
|
||
|
||
def main(): | ||
print("Initializing") | ||
|
||
# create the ElectronicControlUnit (one ECU can hold multiple ControllerApplications) with j1939-22 data link layer | ||
ecu = j1939.ElectronicControlUnit(data_link_layer='j1939-22', max_cmdt_packets=200) | ||
|
||
# can fd Baud: 500k/2M | ||
ecu.connect(bustype='pcan', channel='PCAN_USBBUS1', fd=True, | ||
f_clock_mhz=80, nom_brp=10, nom_tseg1=12, nom_tseg2=3, nom_sjw=1, data_brp=4, data_tseg1=7, data_tseg2=2, data_sjw=1) | ||
|
||
# subscribe to all (global) messages on the bus | ||
ecu.subscribe(on_message) | ||
|
||
# compose the name descriptor for the new ca | ||
name = j1939.Name( | ||
arbitrary_address_capable=0, | ||
industry_group=j1939.Name.IndustryGroup.Industrial, | ||
vehicle_system_instance=1, | ||
vehicle_system=1, | ||
function=1, | ||
function_instance=1, | ||
ecu_instance=1, | ||
manufacturer_code=666, | ||
identity_number=1234567 | ||
) | ||
|
||
# create the ControllerApplications | ||
ca = j1939.ControllerApplication(name, 0x1) | ||
ecu.add_ca(controller_application=ca) | ||
# callback every 0.5s | ||
ca.add_timer(0.500, ca_timer_callback1, ca) | ||
ca.start() | ||
|
||
time.sleep(120) | ||
|
||
print("Deinitializing") | ||
ca.stop() | ||
ecu.disconnect() | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import logging | ||
import time | ||
import j1939 | ||
|
||
|
||
logging.getLogger('j1939').setLevel(logging.DEBUG) | ||
logging.getLogger('can').setLevel(logging.DEBUG) | ||
|
||
|
||
def on_message(priority, pgn, sa, timestamp, data): | ||
"""Receive incoming messages from the bus | ||
:param int priority: | ||
Priority of the message | ||
:param int pgn: | ||
Parameter Group Number of the message | ||
:param int sa: | ||
Source Address of the message | ||
:param int timestamp: | ||
Timestamp of the message | ||
:param bytearray data: | ||
Data of the PDU | ||
""" | ||
print("PGN {} length {}".format(pgn, len(data)), timestamp) | ||
|
||
|
||
def ca_timer_callback1(ca): | ||
"""Callback for sending messages | ||
This callback is registered at the ECU timer event mechanism to be | ||
executed every 500ms. | ||
:param cookie: | ||
A cookie registered at 'add_timer'. May be None. | ||
""" | ||
# wait until we have our device_address | ||
if ca.state != j1939.ControllerApplication.State.NORMAL: | ||
# returning true keeps the timer event active | ||
return True | ||
|
||
# create data with 500 bytes | ||
data = [j1939.ControllerApplication.FieldValue.NOT_AVAILABLE_8] * 500 | ||
|
||
# sending transport protocol broadcast message (BAM) | ||
successful = ca.send_pgn(0, 0xFD, 0xED, 6, data) | ||
if not successful: | ||
print( 'error occurred while sending BAM' ) | ||
|
||
# sending transport protocol peer-to-peer message (rts/cts), destintion address is 0x04 | ||
successful = ca.send_pgn(0, 0xE0, 0x04, 6, data) | ||
if not successful: | ||
print( 'error occurred while sending rts/cts message' ) | ||
|
||
# returning true keeps the timer event active | ||
return True | ||
|
||
|
||
def main(): | ||
print("Initializing") | ||
|
||
# create the ElectronicControlUnit (one ECU can hold multiple ControllerApplications) with j1939-22 data link layer | ||
ecu = j1939.ElectronicControlUnit(data_link_layer='j1939-22', max_cmdt_packets=200) | ||
|
||
# can fd Baud: 500k/2M | ||
ecu.connect(bustype='pcan', channel='PCAN_USBBUS1', fd=True, | ||
f_clock_mhz=80, nom_brp=10, nom_tseg1=12, nom_tseg2=3, nom_sjw=1, data_brp=4, data_tseg1=7, data_tseg2=2, data_sjw=1) | ||
|
||
# subscribe to all (global) messages on the bus | ||
ecu.subscribe(on_message) | ||
|
||
# compose the name descriptor for the new ca | ||
name = j1939.Name( | ||
arbitrary_address_capable=0, | ||
industry_group=j1939.Name.IndustryGroup.Industrial, | ||
vehicle_system_instance=1, | ||
vehicle_system=1, | ||
function=1, | ||
function_instance=1, | ||
ecu_instance=1, | ||
manufacturer_code=666, | ||
identity_number=1234567 | ||
) | ||
|
||
# create the ControllerApplications | ||
ca = j1939.ControllerApplication(name, 0x1) | ||
ecu.add_ca(controller_application=ca) | ||
# callback every 0.5s | ||
ca.add_timer(0.500, ca_timer_callback1, ca) | ||
ca.start() | ||
|
||
time.sleep(120) | ||
|
||
print("Deinitializing") | ||
ca.stop() | ||
ecu.disconnect() | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.