Function definition
Create a fresh application
If you do not already have a openOBD basic application in Replit.com, please create it first by following the hosting setup
Now that we have seen some of the possibilities of what you can achieve with openOBD we want to introduce a new building block: the openOBD Function.
An openOBD Function is a Python class that inherits the functionality of the OpenOBDFunction class.
You might have seen this construct in earlier examples.
The basic elements of an openOBD function include:
- It is a Python class definition
- It inherits from OpenOBDFunction
- It defines a
run()function that is the entry-point of your functions (gets executed) - It defines a set of standard properties:
When we want to create a function that reads out the Vehicle
Identification Number (VIN) through an EOBD command 0902 we can setup the function class as follows, the id and signature can be filled out later when we have a signature from Jifeline Networks:
from openobd import *
class EOBDReadVIN(OpenOBDFunction):
id = ""
signature = ""
version = "v0.1"
name = "Read vin"
description = "Read VIN using EOBD"
def run(self):
# Set bus configuration
self.set_bus_configuration([
(BusConfiguration(bus_name="bus_6_14",
can_bus=CanBus(pin_plus=6, pin_min=14, can_protocol=CanProtocol.CAN_PROTOCOL_ISOTP,
can_bit_rate=CanBitRate.CAN_BIT_RATE_500,
transceiver=TransceiverSpeed.TRANSCEIVER_SPEED_HIGH)))
])
# Define the engine control module, which is present on bus pins 6, 14, with CAN IDs 7E0-7E8, and turn on frame padding
ecm_channel = IsotpChannel(bus_name="bus_6_14", request_id=0x7E0, response_id=0x7E8,
padding=Padding.PADDING_ENABLED)
# Start a stream to communicate with the engine
with Socket(IsotpSocket(self.openobd_session, ecm_channel)) as ecm:
# Request the VIN from the engine, attempting it up to 2 times, each attempt waiting a maximum of 5 seconds for a response
# print("Sending 22F190...")
logging.info("Sending EOBD request 0902...")
try:
response = ecm.request("0902", tries=2, timeout=5)
logging.info(f"Response: {response}")
except NoResponseException as e:
logging.warning("No response to our request")
response = ""
# Decode and print the VIN, ignoring the first 3 bytes of the response
vin = bytes.fromhex(response[6:]).decode("utf-8")
logging.info(f"VIN: {vin}")
self.openobd_session.set_context_variable('VIN', vin, ContextVariableType.GLOBAL)
Create a new EOBD script
- Create a new script file that has the contents of the above script in your 'myfunctions' folder.
- Give the file an appropriate name (ending with the extension
.py)