Skip to content

Commit b5401ae

Browse files
committed
Added an example about how to read a DID
1 parent 824d644 commit b5401ae

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

doc/source/udsoncan/examples.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,49 @@ Security algorithm implementation
109109

110110
-----
111111

112+
113+
114+
.. _reading_a_did:
115+
116+
Reading a DID with ReadDataByIdentifier
117+
---------------------------------------
118+
119+
This example shows how to configure the client with a DID configuration and request the server with ReadDataByIdentifier
120+
121+
.. code-block:: python
122+
123+
import udsoncan
124+
from udsoncan.Connection import IsoTPConnection
125+
from udsoncan.client import Client
126+
import udsoncan.configs
127+
128+
class MyCustomCodecThatShiftBy4(udsoncan.DidCodec):
129+
def encode(self, val):
130+
val = (val << 4) & 0xFFFFFFFF # Do some stuff
131+
return struct.pack('<L', val) # Little endian, 32 bit value
132+
133+
def decode(self, payload):
134+
val = struct.unpack('<L', payload)[0] # decode the 32 bits value
135+
return val >> 4 # Do some stuff (reversed)
136+
137+
def __len__(self):
138+
return 4 # encoded paylaod is 4 byte long.
139+
140+
141+
config = dict(udsoncan.configs.default_client_config)
142+
config['data_identifiers'] = {
143+
0x1234 : MyCustomCodecThatShiftBy4, # Uses own custom defined codec. Giving the class is ok
144+
0x1235 : MyCustomCodecThatShiftBy4(), # Same as 0x1234, giving an instance is good also
145+
0xF190 : udsoncan.AsciiCodec(15) # Codec that read ASCII string. We must tell the length of the string
146+
}
147+
148+
conn = IsoTPConnection('vcan0', rxid=0x123, txid=0x456)
149+
with Client(conn, request_timeout=2, config=config) as client:
150+
vin = client.read_data_by_identifier(0xF190)
151+
print(vin) # 'ABCDE0123456789' (15 chars)
152+
153+
154+
112155
.. _iocontrol_composite_did:
113156

114157
InputOutputControlByIdentifier Composite DID

udsoncan/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ def read_data_by_identifier(self, didlist):
287287
288288
:dependent configuration: ``exception_on_<type>_response`` ``data_identifiers`` ``tolerate_zero_padding``
289289
290+
See :ref:`an example<reading_a_did>` about how to read a DID
291+
290292
:param didlist: The list of DID to be read
291293
:type didlist: list[int]
292294

0 commit comments

Comments
 (0)