j1939 package

Submodules

j1939.Dm14Query module

class j1939.Dm14Query.QueryState(*values)[source]

Bases: Enum

IDLE = 1
WAIT_FOR_SEED = 2
WAIT_FOR_DM16 = 3
WAIT_FOR_OPER_COMPLETE = 4
classmethod __contains__(value)

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members. 3) value is a pseudo-member (flags)

classmethod __getitem__(name)

Return the member matching name.

classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

class j1939.Dm14Query.Command(*values)[source]

Bases: Enum

ERASE = 0
READ = 1
WRITE = 2
STATUS_REQUEST = 3
OPERATION_COMPLETED = 4
OPERATION_FAILED = 5
BOOT_LOAD = 6
EDCP_GENERATION = 7
classmethod __contains__(value)

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members. 3) value is a pseudo-member (flags)

classmethod __getitem__(name)

Return the member matching name.

classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

class j1939.Dm14Query.Dm15Status(*values)[source]

Bases: Enum

PROCEED = 0
BUSY = 1
OPERATION_COMPLETE = 4
OPERATION_FAILED = 5
classmethod __contains__(value)

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members. 3) value is a pseudo-member (flags)

classmethod __getitem__(name)

Return the member matching name.

classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

class j1939.Dm14Query.Dm14Query(ca: ControllerApplication, user_level=7)[source]

Bases: object

performs memory access queries using DM14-DM18 messaging. Presently only read and write queries are supported

Parameters:
  • ca (obj) – j1939 controller application

  • user_level (int) – the user level for the request

__init__(ca: ControllerApplication, user_level=7) None[source]

performs memory access queries using DM14-DM18 messaging. Presently only read and write queries are supported

Parameters:
  • ca (obj) – j1939 controller application

  • user_level (int) – the user level for the request

data_queue: Queue
exception_queue: Queue
address: int | None
command: Command | None
unsubscribe_all() None[source]

Unsubscribes all message handlers

reset_query() None[source]

Resets query to remove transaction specific data

read(dest_address: int, direct: int, address: int, object_count: int, object_byte_size: int = 1, signed: bool = False, return_raw_bytes: bool = False, max_timeout: int = 1) list[source]

Send a read query to dest_address, requesting data at address :param int dest_address: destination address of the message :param int direct: direct address of the message :param int address: address of the message :param int object_count: number of objects to be read :param int object_byte_size: size of each object in bytes :param bool signed: whether the data is signed :param bool return_raw_bytes: whether to return raw bytes or values :param int max_timeout: max timeout for transaction

write(dest_address: int, direct: int, address: int, values: list, object_byte_size: int = 1, max_timeout: int = 1) None[source]

Send a write query to dest_address, requesting to write values at address :param int dest_address: destination address of the message :param int direct: direct address of the message :param int address: address of the message :param list values: values to be written :param int object_byte_size: size of each object in bytes :param int max_timeout: max timeout for transaction

set_seed_key_algorithm(algorithm: Callable[[int], int]) None[source]

set seed-key algorithm to be used for key generation :param algorithm: seed-key algorithm

j1939.Dm14Server module

class j1939.Dm14Server.ResponseState(*values)[source]

Bases: Enum

IDLE = 1
WAIT_FOR_DM14 = 2
WAIT_FOR_KEY = 3
SEND_PROCEED = 4
SEND_OPERATION_COMPLETE = 5
WAIT_OPERATION_COMPLETE = 6
SEND_ERROR = 7
WAIT_FOR_DM16 = 8
classmethod __contains__(value)

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members. 3) value is a pseudo-member (flags)

classmethod __getitem__(name)

Return the member matching name.

classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

class j1939.Dm14Server.DM14Server(ca: ControllerApplication)[source]

Bases: object

performs memory access responses using DM14-DM18 messaging. :param obj ca: j1939 controller application

__init__(ca: ControllerApplication) None[source]

performs memory access responses using DM14-DM18 messaging. :param obj ca: j1939 controller application

sa: int | None
data_queue: Queue
address: bytearray | None
data: bytearray | list
parse_dm14(priority: int, pgn: int, sa: int, timestamp: int, data: bytearray) None[source]

parse DM14 message received :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

bytes_to_int(data: bytearray) int[source]

Convert bytearray to integer :param bytearray data: bytearray to be converted to integer

set_busy(busy: bool) None[source]

Sets busy variable to indicate if busy or not :param busy: busy value

generate_seed() int[source]

Generte a random seed value for key generation

set_seed_key_algorithm(algorithm: Callable[[int], int]) None[source]

Set seed key algorithm to be used for key generation :param algorithm: seed-key algorithm

set_seed_generator(algorithm: Callable[[], int]) None[source]

Sets seed generation algorithm to be used for generating a seed value :param algorithm: seed generation algorithm

set_verify_key(algorithm: Callable[[...], bool]) None[source]

Set key verification algorithm to be used for key verification :param algorithm: key verification algorithm

verify_key(seed: int, key: int) bool[source]

Checks to see if key is valid :param int seed: seed :param int key: key

unsubscribe_all() None[source]

Unsubscribes all message handlers

reset_server() None[source]

Resets server to remove transaction specific data

respond(proceed: bool, data=None, error: int = 16777215, edcp: int = 255, max_timeout: int = 3) list | None[source]

Respond to DM14 query with the requested data or confimation of operation is good to proceed :param bool proceed: whether the operation is good to proceed :param list data: data to be sent to device :param int error: error code to be sent to device :param int edcp: value for edcp extension :param int max_timeout: max time for transaction

j1939.controller_application module

class j1939.controller_application.ControllerApplication(name, device_address_preferred=None, bypass_address_claim=False)[source]

Bases: object

ControllerApplication (CA) identified by a Name and an Address.

Parameters:
  • name – A j1939 j1939.Name instance

  • device_address_preferred – The device_address this CA should claim on the bus.

  • bypass_address_claim – Flag to bypass address claim procedure

class State[source]

Bases: object

NONE = 0
WAIT_VETO = 1
NORMAL = 2
CANNOT_CLAIM = 3
class ClaimTimeout[source]

Bases: object

VETO = 0.25
REQUEST_FOR_CLAIM = 1.25
class FieldValue[source]

Bases: object

NOT_AVAILABLE_8 = 255
NOT_AVAILABLE_16 = 65280
NOT_AVAILABLE_16_ARR = [255, 0]
NOT_VALID_8 = 254
NOT_VALID_16 = 65024
NOT_VALID_16_ARR = [254, 0]
MAX_8 = 250
MAX_16 = 64255
MAX_16_ARR = [250, 255]
__init__(name, device_address_preferred=None, bypass_address_claim=False)[source]
Parameters:
  • name – A j1939 j1939.Name instance

  • device_address_preferred – The device_address this CA should claim on the bus.

  • bypass_address_claim – Flag to bypass address claim procedure

associate_ecu(ecu)[source]

Binds this CA to the ECU given :param ecu:

The ECU this CA should be bound to. A j1939 j1939.ElectronicControlUnit instance

remove_ecu()[source]
subscribe(callback)[source]

Add the given callback to the message notification stream. :param callback:

Function to call when message is received.

unsubscribe(callback)[source]

Stop listening for message. :param callback:

Function to call when message is received.

subscribe_request(callback)[source]

Add the given callback to the request notification stream. :param callback: Function to call when a request is received.

unsubscribe_request(callback)[source]

Remove the given callback to the request notification stream. :param callback: Function to call when a request is received.

subscribe_acknowledge(callback)[source]

Add the given callback from the acknowledge notification stream :param callback: Function to call when an acknowledge is received.

unsubscribe_acknowledge(callback)[source]

Remove the given callback from the request notification stream. :param callback: Function to call when an acknowledge is received.

add_timer(delta_time, callback, cookie=None)[source]

Adds a callback to the list of timer events :param delta_time:

The time in seconds after which the event is to be triggered.

Parameters:

callback – The callback function to call

remove_timer(callback)[source]

Removes ALL entries from the timer event list for the given callback :param callback:

The callback to be removed from the timer event list

register_dependent(dependent)[source]

Register a helper whose stop() should be called on ECU shutdown.

Convenience forwarder to ElectronicControlUnit.register_dependent() for helpers that only hold a reference to a CA.

Parameters:

dependent – Any object exposing a no-arg stop() method.

unregister_dependent(dependent)[source]

Remove a previously-registered dependent.

Convenience forwarder to ElectronicControlUnit.unregister_dependent().

Parameters:

dependent – The object previously passed to register_dependent().

start(claim_delay=0.5)[source]

Starts the CA :param claim_delay:

The time in seconds to wait before starting the address claim procedure.

stop()[source]

Stops the CA

send_message(priority, parameter_group_number, data)[source]
send_pgn(data_page, pdu_format, pdu_specific, priority, data, time_limit=0, frame_format=3)[source]

send a pgn :param int data_page: data page :param int pdu_format: pdu format :param int pdu_specific: pdu specific :param int priority: message priority :param list data: payload, each list index represents one payload byte :param time_limit: option j1939-22 multi-pg: specify a time limit in s (e.g. 0.1 == 100ms), after this time, the multi-pg will be sent. several pgs can thus be combined in one multi-pg. 0 or no time-limit means immediate sending.

send_request(data_page, pgn, destination)[source]

send a request message :param int data_page: data page :param int pgn: pgn to be requested :param list data: destination address

on_request(src_address, dest_address, pgn)[source]

Callback for PGN requests :param int src_address:

The address the request comes from

Parameters:
  • dest_address (int) – The address the request was sent to; normally ours, but can also be GLOBAL

  • pgn (int) – Parameter Group Number requested

message_acceptable(dest_address)[source]

Indicates if this CA would accept a message This function indicates the acceptance of this CA for the given dest_address.

property state
property device_address
property started: bool

Getter for the started property

j1939.diagnostic_messages module

class j1939.diagnostic_messages.DTC(dtc=None, spn=None, fmi=None, oc=0, cm=4)[source]

Bases: object

Parser/encoder for J1939 DTC (Diagnostic Trouble Code).

Supports the four SAE J1939-73 SPN conversion methods:
  • CM 1: SPN MSBs in byte 1, mid in byte 2, LSBs+FMI in byte 3, CM bit = 1

  • CM 2: SPN mid in byte 1, MSBs in byte 2, LSBs+FMI in byte 3, CM bit = 1

  • CM 3: SPN LSBs/mid/MSBs in bytes 1/2/3 (modern layout), CM bit = 1

  • CM 4: same byte layout as CM 3, CM bit = 0 (current standard)

The on-wire CM bit only distinguishes {1,2,3} (bit=1) from {4} (bit=0). CM 1 vs CM 2 vs CM 3 are not separable from the bytes alone; when decoding raw bytes with CM bit = 1, the caller must indicate which one was used (defaults to CM 3 — the most common legacy layout).

__init__(dtc=None, spn=None, fmi=None, oc=0, cm=4)[source]
property spn
Returns:

SPN Suspect Parameter Number

Return type:

int

property fmi
Returns:

FMI Failure Mode Identifier

Return type:

int

property oc
Returns:

DTC occurrence counter

Return type:

int

property cm
Returns:

SPN conversion method (1, 2, 3, or 4 per SAE J1939-73)

Return type:

int

property dtc
Returns:

DTC Diagnostic Trouble Code

Return type:

int

class j1939.diagnostic_messages.DtcLamp[source]

Bases: object

Diagnostic trouble code lamp status

OFF = 0
ON = 1
ON_SLOW_FLASH = 2
ON_FAST_FLASH = 3
NA = 4
get_status(lamp, flash)[source]
get_data(status_dic)[source]
class j1939.diagnostic_messages.Dm1(ca: ControllerApplication, rx_cm_bit_set: int = 3)[source]

Bases: object

Active Diagnostic Trouble Codes (DM1)

Parser for DM1

DM1 provides diagnostic lamp status and diagnostic trouble codes (DTCs). Together, the lamp and DTC information convey the diagnostic condition of the transmitting electronic component to other components on the network. Occurrence counts may be provided.

Parameters:
  • ca (obj) – j1939 controller application

  • rx_cm_bit_set (int) – SPN conversion method (1, 2, or 3) to assume when a received DTC has its CM bit set. The on-wire CM bit cannot distinguish CMs 1, 2 and 3 — only between {1,2,3} (bit=1) and 4 (bit=0). Defaults to 3 (the most common legacy layout). CM 4 is auto-detected.

__init__(ca: ControllerApplication, rx_cm_bit_set: int = 3)[source]
Parameters:
  • ca (obj) – j1939 controller application

  • rx_cm_bit_set (int) – SPN conversion method (1, 2, or 3) to assume when a received DTC has its CM bit set. The on-wire CM bit cannot distinguish CMs 1, 2 and 3 — only between {1,2,3} (bit=1) and 4 (bit=0). Defaults to 3 (the most common legacy layout). CM 4 is auto-detected.

subscribe(callback)[source]

Add the given callback to the Dm1 message notification stream.

Parameters:

callback – Function to call when Dm1 message is received.

unsubscribe(callback)[source]

Stop listening for Dm1 message.

Parameters:

callback – Function to call when Dm1 message is received.

start_send(callback, cycletime=1)[source]

Start cyclic sending of Dm1 message

Parameters:
  • callback – Function to call before Dm1 message is sent

  • cycletime (int) – Optional send cycletime cycletime is 1s if not specified

  • priority (int) – priority of Dm1 message

stop_send()[source]

Stop cyclic sending of Dm1 message

property dtc_dic_list
Returns:

list of dictionaries of all DTCs included in DM1

Return type:

list of dic: ‘spn’, ‘fmi’, ‘oc’

property lamp_status
Returns:

global lamp status for the DM1

Return type:

dic: ‘pl’, ‘awl’, ‘rsl’, ‘mil’

property data
Returns:

j1939 pdu payload

Return type:

list of int

class j1939.diagnostic_messages.Dm11(ca: ControllerApplication)[source]

Bases: object

Diagnostic Data Clear/Reset for Active DTCs (DM11)

Parameters:

ca (obj) – j1939 controller application

__init__(ca: ControllerApplication)[source]
Parameters:

ca (obj) – j1939 controller application

request_clear_all(destination)[source]
subscribe_request_clear_all(callback)[source]
subscribe_acknowledge_clear_all(callback)[source]
class j1939.diagnostic_messages.Dm22(ca: ControllerApplication)[source]

Bases: object

Individual Clear/Reset of Active and Previously Active DTC (DM22)

Parameters:

ca (obj) – j1939 controller application

class DTC_CLR_CTRL[source]

Bases: object

Individual DTC Clear/Reset Control Byte

PA_REQ = 1
PA_ACK = 2
PA_NACK = 3
ACT_REQ = 17
ACT_ACK = 18
ACT_NACK = 19
class DTC_CLR_CTRL_SPECIFIC[source]

Bases: object

Control Byte Specific Indicator for Individual DTC Clear

GENERAL_NACK = 0
ACCESS_DENIED = 1
DTC_UNKNOWN = 2
DTC_PA_NOT_ACTIVE = 3
DTC_ACT_NOT_ACTIVE = 4
__init__(ca: ControllerApplication)[source]
Parameters:

ca (obj) – j1939 controller application

request_clear_act_dtc(dest_address, spn, fmi)[source]

Request to Clear/Reset Active DTC

Parameters:
  • dest_address – destination address of the node

  • spn – spn of the dtc to be cleared

  • spn – fmi of the dtc to be cleared

request_clear_pa_dtc(dest_address, spn, fmi)[source]

Request to Clear/Reset Previously Active DTC

Parameters:
  • dest_address – destination address of the node

  • spn – spn of the dtc to be cleared

  • spn – fmi of the dtc to be cleared

j1939.electronic_control_unit module

class j1939.electronic_control_unit.ElectronicControlUnit(data_link_layer='j1939-21', max_cmdt_packets=1, minimum_tp_rts_cts_dt_interval=None, minimum_tp_bam_dt_interval=None, send_message=None)[source]

Bases: object

ElectronicControlUnit (ECU) holding one or more ControllerApplications (CAs).

Parameters:

data_link_layer – specify data-link-layer, ‘j1939-21’ or ‘j1939-22’

__init__(data_link_layer='j1939-21', max_cmdt_packets=1, minimum_tp_rts_cts_dt_interval=None, minimum_tp_bam_dt_interval=None, send_message=None)[source]
Parameters:

data_link_layer – specify data-link-layer, ‘j1939-21’ or ‘j1939-22’

stop()[source]

Stops the ECU background handling

This Function explicitly stops the background handling of the ECU.

Before stopping the ECU’s own protocol/timer threads, every registered dependent (see register_dependent()) has its stop() method invoked in LIFO order. Exceptions raised by a dependent’s stop() are logged and swallowed so a single misbehaving dependent cannot prevent the rest of the shutdown from completing.

register_dependent(dependent)[source]

Register a helper whose stop() should be called by stop().

Any helper object that owns threads, timers, or other resources tied to this ECU should call this during construction. ecu.stop() will invoke dependent.stop() in LIFO order before tearing down its own threads.

Duplicate registrations of the same object (by identity) are silently ignored.

Parameters:

dependent – Any object exposing a no-arg stop() method.

Raises:
  • RuntimeError – If called while the ECU is shutting down.

  • TypeError – If dependent does not expose a callable stop attribute.

unregister_dependent(dependent)[source]

Remove a previously-registered dependent.

Parameters:

dependent – The object previously passed to register_dependent().

add_timer(delta_time, callback, cookie=None)[source]

Adds a callback to the list of timer events

Parameters:
  • delta_time – The time in seconds after which the event is to be triggered.

  • callback – The callback function to call

remove_timer(callback)[source]

Removes ALL entries from the timer event list for the given callback

Parameters:

callback – The callback to be removed from the timer event list

connect(*args, **kwargs)[source]

Connect to CAN bus using python-can.

Arguments are passed directly to can.BusABC. Typically these may include:

Parameters:
  • channel – Backend specific channel for the CAN interface.

  • interface (str) – Name of the interface (formerly bustype, renamed in python-can v4.2). See python-can manual for full list of supported interfaces.

  • bitrate (int) – Bitrate in bit/s.

Raises:

can.CanError – When connection fails.

disconnect()[source]

Disconnect from the CAN bus.

Must be overridden in a subclass if a custom interface is used.

subscribe(callback, device_address=None)[source]

Add the given callback to the message notification stream.

Parameters:
  • callback – Function to call when message is received.

  • device_address (int) – Device address of the application. This is a simple way for peer-to-peer reception without adding a controller-application. Only one device address can be entered. Multiple device addresses are only possible with controller applications. Note: TP.CMDT will only be received if the destination address is bound to a controller application.

unsubscribe(callback)[source]

Stop listening for message.

Parameters:

callback – Function to call when message is received.

add_ca(**kwargs)[source]

Add a ControllerApplication to the ECU.

Parameters:
  • controller_application – A j1939.ControllerApplication object.

  • name – A j1939.Name object.

  • device_address – An integer representing the device address to announce to the bus.

Returns:

The CA object that was added.

Return type:

r3964.ControllerApplication

remove_ca(device_address)[source]

Remove a ControllerApplication from the ECU.

Parameters:

device_address (int) – A integer representing the device address

Returns:

True if the ControllerApplication was successfully removed, otherwise False is returned.

add_bus(bus)[source]

Add a bus to the ECU.

Parameters:

bus – A can.BusABC object.

add_notifier(notifier)[source]

Add a notifier to the ECU.

Parameters:

notifier – A can.Notifier object.

remove_bus()[source]

Remove the bus from the ECU.

remove_notifier()[source]

Remove the notifier from the ECU.

send_pgn(data_page, pdu_format, pdu_specific, priority, src_address, data, time_limit=0, frame_format=3)[source]

send a pgn :param int data_page: data page :param int pdu_format: pdu format :param int pdu_specific: pdu specific :param int priority: message priority :param int src_address: address of the transmitter :param list data: payload, each list index represents one payload byte :param time_limit: option j1939-22 multi-pg: specify a time limit in s (e.g. 0.1 == 100ms), after this time, the multi-pg will be sent. several pgs can thus be combined in one multi-pg. 0 or no time-limit means immediate sending.

send_message(can_id, extended_id, data, fd_format=False)[source]

Send a raw CAN message to the bus.

This method may be overridden in a subclass if you need to integrate this library with a custom backend. It is safe to call this from multiple threads.

Parameters:
  • can_id (int) – CAN-ID of the message (always 29-bit)

  • data – Data to be transmitted (anything that can be converted to bytes)

  • fd_format – fd format means bitrate switching and payload of max 64Bytes is active

Raises:

can.CanError – When the message fails to be transmitted

notify(can_id, data, timestamp)[source]

Feed incoming CAN message into this ecu.

If a custom interface is used, this function must be called for each 29-bit standard message read from the CAN bus.

Parameters:
  • can_id (int) – CAN-ID of the message (always 29-bit)

  • data (bytearray) – Data part of the message (0 - 8 bytes)

  • timestamp (float) – The timestamp field in a CAN message is a floating point number representing when the message was received since the epoch in seconds. Where possible this will be timestamped in hardware.

add_bus_filters(filters: can.typechecking.CanFilters | None)[source]

Add bus filters to the underlying CAN bus.

Parameters:

filters – An iterable of dictionaries each containing a “can_id”, a “can_mask”, and an optional “extended” key

class j1939.electronic_control_unit.MessageListener(*args: Any, **kwargs: Any)[source]

Bases: Listener

Listens for messages on CAN bus and feeds them to an ECU instance.

Parameters:

ecu (j1939.ElectronicControlUnit) – The ECU to notify on new messages.

__init__(ecu: ElectronicControlUnit)[source]
on_message_received(msg: can.Message)[source]
stop()[source]

j1939.error_info module

class j1939.error_info.J1939Error(*values)[source]

Bases: Enum

Enum of general errors based off of SAE Mobilus guidelines

NO_ERROR = 0
UNKNOWN_ERROR = 1
BUSY = 2
BUSY_ERASE_REQUEST = 16
BUSY_READ_REQUEST = 17
BUSY_WRITE_REQUEST = 18
BUSY_STATUS_REQUEST = 19
BUSY_BOOT_LOAD_REQUEST = 22
BUSY_EDCP_GENERATION_REQUEST = 23
BUSY_UNKNOW_REQUEST = 31
EDC_PARAMETER_ERROR = 32
RAM_ERROR = 33
FLASH_ERROR = 34
PROM_ERROR = 35
INTERNAL_ERROR = 36
GENERAL_ADDRESSING_ERROR = 256
ADDRESS_NOT_ON_BOUNDARY = 257
ADDRESS_INVALID_LENGTH = 258
ADDRESS_MEMORY_OVERFLOW = 259
ADDRESS_DATA_ERASE_REQUIRED = 260
ADDRESS_PROGRAM_ERASE_REQUIRED = 261
ADDRESS_TX_ERASE_PROGRAM_REQUIRED = 262
ADDRESS_BOOT_LOAD_OUT_OF_RANGE = 263
ADDRESS_BOOT_LOAD_NOT_ON_BOUNDARY = 264
DATA_OUT_OF_RANGE = 265
DATA_NAME_UNEXPECTED = 266
SECURITY_GENERAL = 4096
SECURITY_INVALID_PASSWORD = 4097
SECURITY_INVALID_LEVEL = 4098
SECURITY_INVALID_KEY = 4099
SECURITY_NOT_DIAGNOSTIC = 4100
SECURITY_INCORRECT_MODE = 4101
SECURITY_ENGINE_RUNNING = 4102
SECURITY_VEHICLE_MOVING = 4103
ABORT_EXTERNAL = 65536
MAX_RETRY = 65537
NO_RESPONSE = 65538
INITILIZATION_TIMEOUT = 65539
COMPLETION_TIMEOUT = 65540
NO_INDICATOR = 16777215
classmethod __contains__(value)

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members. 3) value is a pseudo-member (flags)

classmethod __getitem__(name)

Return the member matching name.

classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

j1939.j1939_21 module

class j1939.j1939_21.J1939_21(send_message, job_thread_wakeup, notify_subscribers, max_cmdt_packets, minimum_tp_rts_cts_dt_interval, minimum_tp_bam_dt_interval, ecu_is_message_acceptable)[source]

Bases: object

class ConnectionMode[source]

Bases: object

RTS = 16
CTS = 17
EOM_ACK = 19
BAM = 32
ABORT = 255
class ConnectionAbortReason[source]

Bases: object

BUSY = 1
RESOURCES = 2
TIMEOUT = 3
CTS_WHILE_DT = 4
class Timeout[source]

Bases: object

Timeouts according SAE J1939/21

Tr = 0.2
Th = 0.5
T1 = 0.75
T2 = 1.25
T3 = 1.25
T4 = 1.05
Tb = 0.05
class SendBufferState[source]

Bases: object

WAITING_CTS = 0
SENDING_IN_CTS = 1
SENDING_BM = 2
TRANSMISSION_FINISHED = 3
__init__(send_message, job_thread_wakeup, notify_subscribers, max_cmdt_packets, minimum_tp_rts_cts_dt_interval, minimum_tp_bam_dt_interval, ecu_is_message_acceptable)[source]
add_ca(ca)[source]
remove_ca(device_address)[source]
send_pgn(data_page, pdu_format, pdu_specific, priority, src_address, data, time_limit, frame_format)[source]
async_job_thread(now)[source]
notify(can_id, data, timestamp)[source]

Feed incoming CAN message into this ecu.

If a custom interface is used, this function must be called for each 29-bit standard message read from the CAN bus.

Parameters:
  • can_id (int) – CAN-ID of the message (always 29-bit)

  • data (bytearray) – Data part of the message (0 - 8 bytes)

  • timestamp (float) – The timestamp field in a CAN message is a floating point number representing when the message was received since the epoch in seconds. Where possible this will be timestamped in hardware.

j1939.j1939_22 module

class j1939.j1939_22.J1939_22(send_message, job_thread_wakeup, notify_subscribers, max_cmdt_packets, minimum_tp_rts_cts_dt_interval, minimum_tp_bam_dt_interval, ecu_is_message_acceptable)[source]

Bases: object

class TpControlType(*values)[source]

Bases: IntEnum

RTS = 0
CTS = 1
EOM_STATUS = 2
EOM_ACK = 3
BAM = 4
ABORT = 15
conjugate()

Returns self, the complex conjugate of any int.

bit_length()

Number of bits necessary to represent self in binary.

>>> bin(37)
'0b100101'
>>> (37).bit_length()
6
bit_count()

Number of ones in the binary representation of the absolute value of self.

Also known as the population count.

>>> bin(13)
'0b1101'
>>> (13).bit_count()
3
to_bytes(length=1, byteorder='big', *, signed=False)

Return an array of bytes representing an integer.

length

Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1.

byteorder

The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.

signed

Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.

classmethod from_bytes(bytes, byteorder='big', *, signed=False)

Return the integer represented by the given array of bytes.

bytes

Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.

byteorder

The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.

signed

Indicates whether two’s complement is used to represent the integer.

as_integer_ratio()

Return a pair of integers, whose ratio is equal to the original int.

The ratio is in lowest terms and has a positive denominator.

>>> (10).as_integer_ratio()
(10, 1)
>>> (-10).as_integer_ratio()
(-10, 1)
>>> (0).as_integer_ratio()
(0, 1)
is_integer()

Returns True. Exists for duck type compatibility with float.is_integer.

real

the real part of a complex number

imag

the imaginary part of a complex number

numerator

the numerator of a rational number in lowest terms

denominator

the denominator of a rational number in lowest terms

classmethod __contains__(value)

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members. 3) value is a pseudo-member (flags)

__dir__()

Returns public methods and other interesting attributes.

classmethod __getitem__(name)

Return the member matching name.

__init__(*args, **kwds)
classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

class Adt(*values)[source]

Bases: IntEnum

NO_ADT = 0
MS_CS = 1
MS_FS = 2
MS_COMBINED_CS_FS = 3
conjugate()

Returns self, the complex conjugate of any int.

bit_length()

Number of bits necessary to represent self in binary.

>>> bin(37)
'0b100101'
>>> (37).bit_length()
6
bit_count()

Number of ones in the binary representation of the absolute value of self.

Also known as the population count.

>>> bin(13)
'0b1101'
>>> (13).bit_count()
3
to_bytes(length=1, byteorder='big', *, signed=False)

Return an array of bytes representing an integer.

length

Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1.

byteorder

The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.

signed

Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.

classmethod from_bytes(bytes, byteorder='big', *, signed=False)

Return the integer represented by the given array of bytes.

bytes

Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.

byteorder

The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.

signed

Indicates whether two’s complement is used to represent the integer.

as_integer_ratio()

Return a pair of integers, whose ratio is equal to the original int.

The ratio is in lowest terms and has a positive denominator.

>>> (10).as_integer_ratio()
(10, 1)
>>> (-10).as_integer_ratio()
(-10, 1)
>>> (0).as_integer_ratio()
(0, 1)
is_integer()

Returns True. Exists for duck type compatibility with float.is_integer.

real

the real part of a complex number

imag

the imaginary part of a complex number

numerator

the numerator of a rational number in lowest terms

denominator

the denominator of a rational number in lowest terms

classmethod __contains__(value)

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members. 3) value is a pseudo-member (flags)

__dir__()

Returns public methods and other interesting attributes.

classmethod __getitem__(name)

Return the member matching name.

__init__(*args, **kwds)
classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

class ConnectionAbortReason[source]

Bases: object

BUSY = 1
RESOURCES = 2
TIMEOUT = 3
CTS_WHILE_DT = 4
class DataLength[source]

Bases: object

TP = 60
MULTI_PG = 60
class Timeout[source]

Bases: object

Timeouts according SAE J1939/22

Tr = 0.2
Th = 0.5
T1 = 0.75
T2 = 1.25
T3 = 1.25
T4 = 1.05
T5 = 3.0
class SendBufferState[source]

Bases: object

WAITING_CTS = 0
SENDING_RTS_CTS = 1
SENDING_BAM = 2
SENDING_EOM_STATUS = 3
WAITING_EOM_ACK = 4
EOM_ACK_RECEIVED = 5
TRANSMISSION_FINISHED = 6
class Acknowledgement[source]

Bases: object

ACK = 0
NACK = 1
AccessDenied = 2
CannotRespond = 3
__init__(send_message, job_thread_wakeup, notify_subscribers, max_cmdt_packets, minimum_tp_rts_cts_dt_interval, minimum_tp_bam_dt_interval, ecu_is_message_acceptable)[source]
add_ca(ca)[source]
remove_ca(device_address)[source]
send_pgn(data_page, pdu_format, pdu_specific, priority, src_address, data, time_limit, frame_format, tos=2, trailer_format=0)[source]
async_job_thread(now)[source]
notify(can_id, data, timestamp)[source]

Feed incoming CAN message into this ecu.

If a custom interface is used, this function must be called for each 29-bit standard message read from the CAN bus.

Parameters:
  • can_id (int) – CAN-ID of the message (always 29-bit)

  • data (bytearray) – Data part of the message (0 - 8 bytes)

  • timestamp (float) – The timestamp field in a CAN message is a floating point number representing when the message was received since the epoch in seconds. Where possible this will be timestamped in hardware.

j1939.memory_access module

class j1939.memory_access.DMState(*values)[source]

Bases: Enum

IDLE = 1
REQUEST_STARTED = 2
WAIT_RESPONSE = 3
WAIT_QUERY = 4
SERVER_CLEANUP = 5
classmethod __contains__(value)

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members. 3) value is a pseudo-member (flags)

classmethod __getitem__(name)

Return the member matching name.

classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

class j1939.memory_access.MemoryAccess(ca: ControllerApplication)[source]

Bases: object

Makes an overarching Memory access class

Spawns a background servicer thread tied to the lifetime of this instance. Call stop() (or use the instance as a context manager) when done. The instance is also registered as a dependent of the parent ECU, so ecu.stop() will cascade and tear this instance down automatically.

Parameters:

ca – Controller Application

__init__(ca: ControllerApplication) None[source]

Makes an overarching Memory access class

Spawns a background servicer thread tied to the lifetime of this instance. Call stop() (or use the instance as a context manager) when done. The instance is also registered as a dependent of the parent ECU, so ecu.stop() will cascade and tear this instance down automatically.

Parameters:

ca – Controller Application

stop(timeout: float = 2.0) None[source]

Stop the background servicer thread and release resources.

Idempotent: subsequent calls are no-ops. Safe to call from any thread, including from inside ecu.stop()’s cascade.

Parameters:

timeout (float) – Maximum time in seconds to wait for the servicer thread to exit.

respond(proceed: bool, data: list | None = None, error: int = 16777215, edcp: int = 255, max_timeout: int = 3) list | None[source]

Responds with requested data and error code, if applicable, to a read request

Parameters:
  • proceed (bool) – whether the operation is good to proceed

  • data (list) – data to be sent to device

  • error (int) – error code to be sent to device

  • edcp (int) – value for edcp extension

  • max_timeout (int) – max timeout for transaction

read(dest_address: int, direct: int, address: int, object_count: int, object_byte_size: int = 1, signed: bool = False, return_raw_bytes: bool = False, max_timeout: int = 1) list[source]

Make a dm14 read Query

Parameters:
  • dest_address (int) – destination address of the message

  • direct (int) – direct address of the message

  • address (int) – address of the message

  • object_count (int) – number of objects to be read

  • object_byte_size (int) – size of each object in bytes

  • signed (bool) – whether the data is signed

  • return_raw_bytes (bool) – whether to return raw bytes or values

  • max_timeout (int) – max timeout for transaction

write(dest_address: int, direct: int, address: int, values: list, object_byte_size: int = 1, max_timeout: int = 1) None[source]

Send a write query to dest_address, requesting to write values at address

Parameters:
  • dest_address (int) – destination address of the message

  • direct (int) – direct address of the message

  • address (int) – address of the message

  • values (list) – values to be written

  • object_byte_size (int) – size of each object in bytes

  • max_timeout (int) – max timeout for transaction

set_seed_generator(seed_generator: Callable[[], int]) None[source]

Sets seed generator function to use :param seed_generator: seed generator function

set_seed_key_algorithm(algorithm: Callable[[int], int]) None[source]

Sets seed-key algorithm to be used for key generation

Parameters:

algorithm – seed-key algorithm

set_verify_key(verify_key: Callable[[...], bool]) None[source]

Sets verify key function to be used for verifying the key

Parameters:

verify_key – verify key function

set_notify(notify: Callable[[], None]) None[source]

Sets notify function to be used for notifying the user of memory accesses

Parameters:

notify – notify function

set_proceed(proceed: Callable[[...], bool]) None[source]

Sets proceed function to determine if a memory query is valid or not

Parameters:

proceed – proceed function

reset() None[source]

Resets both server and query to remove transaction specific data

j1939.message_id module

class j1939.message_id.MessageId(**kwargs)[source]

Bases: object

The CAN MessageId of an PDU.

The MessageId consists of three parts:
  • Priority

  • Parameter Group Number

  • Source Address

Parameters:
  • priority – 3-bit Priority

  • parameter_group_number – 18-bit Parameter Group Number

  • source_address – 8-bit Source Address There is a total of 253 addresses available and every address must be unique within the network.

  • can_id – A 29-bit CAN-Id the MessageId should be parsed from.

__init__(**kwargs)[source]
Parameters:
  • priority – 3-bit Priority

  • parameter_group_number – 18-bit Parameter Group Number

  • source_address – 8-bit Source Address There is a total of 253 addresses available and every address must be unique within the network.

  • can_id – A 29-bit CAN-Id the MessageId should be parsed from.

property can_id

Transforms the MessageId object to a 29 bit CAN-Id

class j1939.message_id.FrameFormat[source]

Bases: object

CBFF = 0
CEFF = 1
FBFF = 2
FEFF = 3

j1939.name module

class j1939.name.Name(**kwargs)[source]

Bases: object

The Name of one Controller Application.

The Name consists of 64 bit:

1-bit Arbitrary Address Capable Indicate the capability to solve address conflicts. Set to 1 if the device is Arbitrary Address Capable, set to 0 if it’s Single Address Capable.

3-bit Industry Group One of the predefined J1939 industry groups.

4-bit Vehicle System Instance Instance number of a vehicle system to distinguish two or more device with the same Vehicle System number in the same J1939 network. The first instance is assigned to the instance number 0.

7-bit Vehicle System A subcomponent of a vehicle, that includes one or more J1939 segments and may be connected or disconnected from the vehicle. A Vehicle System may be made of one or more functions. The Vehicle System depends on the Industry Group definition.

1-bit Reserved This field is reserved for future use by SAE.

8-bit Function One of the predefined J1939 functions. The same function value (upper 128 only) may mean different things for different Industry Groups or Vehicle Systems.

5-bit Function Instance Instance number of a function to distinguish two or more devices with the same function number in the same J1939 network. The first instance is assigned to the instance number 0.

3-bit ECU Instance Identify the ECU instance if multiple ECUs are involved in performing a single function. Normally set to 0.

11-bit Manufacturer Code One of the predefined J1939 manufacturer codes.

21-bit Identity Number A unique number which identifies the particular device in a manufacturer specific way.

Parameters:
  • value – 64-bit value the address should be extracted from

  • bytes – Array of 8 bytes containing the name object as binary representation.

  • arbitrary_address_capable – 1-bit Arbitrary Address Capable Indicate the capability to solve address conflicts. Set to 1 if the device is Arbitrary Address Capable, set to 0 if it’s Single Address Capable.

  • industry_group – 3-bit Industry Group One of the predefined J1939 industry groups.

  • vehicle_system_instance – 4-bit Vehicle System Instance Instance number of a vehicle system to distinguish two or more device with the same Vehicle System number in the same J1939 network. The first instance is assigned to the instance number 0.

  • vehicle_system – 7-bit Vehicle System A subcomponent of a vehicle, that includes one or more J1939 segments and may be connected or disconnected from the vehicle. A Vehicle System may be made of one or more functions. The Vehicle System depends on the Industry Group definition.

  • function – 8-bit Function One of the predefined J1939 functions. The same function value (upper 128 only) may mean different things for different Industry Groups or Vehicle Systems.

  • function_instance – 5-bit Function Instance Instance number of a function to distinguish two or more devices with the same function number in the same J1939 network. The first instance is assigned to the instance number 0.

  • ecu_instance – 3-bit ECU Instance Identify the ECU instance if multiple ECUs are involved in performing a single function. Normally set to 0.

  • manufacturer_code – 11-bit Manufacturer Code One of the predefined J1939 manufacturer codes.

  • identity_number – 21-bit Identity Number A unique number which identifies the particular device in a manufacturer specific way.

class IndustryGroup[source]

Bases: object

Global = 0
OnHighway = 1
AgriculturalAndForestry = 2
Construction = 3
Marine = 4
Industrial = 5
__init__(**kwargs)[source]
Parameters:
  • value – 64-bit value the address should be extracted from

  • bytes – Array of 8 bytes containing the name object as binary representation.

  • arbitrary_address_capable – 1-bit Arbitrary Address Capable Indicate the capability to solve address conflicts. Set to 1 if the device is Arbitrary Address Capable, set to 0 if it’s Single Address Capable.

  • industry_group – 3-bit Industry Group One of the predefined J1939 industry groups.

  • vehicle_system_instance – 4-bit Vehicle System Instance Instance number of a vehicle system to distinguish two or more device with the same Vehicle System number in the same J1939 network. The first instance is assigned to the instance number 0.

  • vehicle_system – 7-bit Vehicle System A subcomponent of a vehicle, that includes one or more J1939 segments and may be connected or disconnected from the vehicle. A Vehicle System may be made of one or more functions. The Vehicle System depends on the Industry Group definition.

  • function – 8-bit Function One of the predefined J1939 functions. The same function value (upper 128 only) may mean different things for different Industry Groups or Vehicle Systems.

  • function_instance – 5-bit Function Instance Instance number of a function to distinguish two or more devices with the same function number in the same J1939 network. The first instance is assigned to the instance number 0.

  • ecu_instance – 3-bit ECU Instance Identify the ECU instance if multiple ECUs are involved in performing a single function. Normally set to 0.

  • manufacturer_code – 11-bit Manufacturer Code One of the predefined J1939 manufacturer codes.

  • identity_number – 21-bit Identity Number A unique number which identifies the particular device in a manufacturer specific way.

property arbitrary_address_capable
property industry_group
property vehicle_system_instance
property vehicle_system
property reserved_bit
property function
property function_instance
property ecu_instance
property manufacturer_code
property identity_number
property value
property bytes

Get the Name object as 8 Byte Data

j1939.parameter_group_number module

class j1939.parameter_group_number.ParameterGroupNumber(data_page=0, pdu_format=0, pdu_specific=0)[source]

Bases: object

Parameter Group Number (PGN).

The PGN are described in SAE J1939/21 and consists of four parts:
  • 1-bit Reserved (sometimes referred to as Extended Data Page)

  • 1-bit Data Page (DP)

  • 8-bit PDU Format (PF)

  • 8-bit PDU Specific (PS)

Predefined PGNs are listed in SAE J1939 and SAE J1939/71

A PF value from 0 to 239 (PDU1) indicates a destination address (DA) in PS (peer-to-peer communication). A PF value from 240 to 255 (PDU2) indicates a Group Extension (GE) inside the PS (broadcast message). The DA 255 is called the Global Destination Address. It requires all nodes to listen to and to respond, if required.

TODO: naming/wording: according the standard, a PGN in PDU1 format always

sets the 8 Bit PS to 0. Do we have to separate this object to reflect this rule. And if we have to, how to name the other PGN object?

Parameters:
  • data_page – 1-bit Data Page

  • pdu_format – 8-bit PDU Format

  • pdu_specific – 8-bit PDU Specific

class PGN[source]

Bases: object

FEFF_MULTI_PG = 9472
FD_TP_CM = 19712
FD_TP_DT = 19968
REQUEST = 59904
ADDRESSCLAIM = 60928
DATATRANSFER = 60160
TP_CM = 60416
DM01 = 65226
DM02 = 65227
DM03 = 65228
DM04 = 65229
DM05 = 65230
DM06 = 65231
DM07 = 58112
DM08 = 65232
DM10 = 65234
DM11 = 65235
DM12 = 65236
DM13 = 57088
DM14 = 55552
DM15 = 55296
DM16 = 55040
DM17 = 54784
DM18 = 54272
DM19 = 54016
DM20 = 49664
DM21 = 49408
DM22 = 49920
DM23 = 64949
DM24 = 64950
DM25 = 64951
DM26 = 64952
DM27 = 64898
DM28 = 64896
DM29 = 40448
DM30 = 41984
DM31 = 41728
DM32 = 41472
DM33 = 41216
DM34 = 40960
DM35 = 40704
DM36 = 64868
DM37 = 64867
DM38 = 64866
DM39 = 64865
DM40 = 64864
DM41 = 64863
DM42 = 64862
DM43 = 64861
DM44 = 64860
DM45 = 64859
DM46 = 64858
DM47 = 64857
DM48 = 64856
DM49 = 64855
DM50 = 64854
DM51 = 64853
DM52 = 64852
DM53 = 64721
DM54 = 64722
DM55 = 64723
DM56 = 64711
DM57 = 64710
class Address[source]

Bases: object

NULL = 254
GLOBAL = 255
__init__(data_page=0, pdu_format=0, pdu_specific=0)[source]
Parameters:
  • data_page – 1-bit Data Page

  • pdu_format – 8-bit PDU Format

  • pdu_specific – 8-bit PDU Specific

property is_pdu1_format

Indicates Peer-to-Peer communication

property is_pdu2_format

Indicates broadcast communication

from_message_id(mid)[source]

Fills in the object from a MessageId given

property value

Returns the value of the PGN

j1939.version module