Adapting CAN examples to LINΒΆ
- To adapt the examples from CAN to LIN, reference signals in a database that use LIN:
write
:- Accepts any frame type.
read
:- Chooses the frame object to create based on the
frame_type
field in the raw data. This can be overridden by passing a customnixnet.types.FrameFactory
in theframe_type
parameter. change_lin_sched
:- Writes a request for the LIN interface to change the running schedule.
This displays the diff of can_frame_stream_io.py
and
lin_frame_stream_io.py
to demonstrate the changes required to
update CAN example code for LIN.
--- /home/docs/checkouts/readthedocs.org/user_builds/nixnet/checkouts/latest/nixnet_examples/can_frame_stream_io.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/nixnet/checkouts/latest/nixnet_examples/lin_frame_stream_io.py
@@ -12,29 +12,32 @@
def main():
- interface1 = 'CAN1'
- interface2 = 'CAN2'
+ interface1 = 'LIN1'
+ interface2 = 'LIN2'
+ database = 'NIXNET_exampleLDF'
- with nixnet.FrameInStreamSession(interface1) as input_session:
- with nixnet.FrameOutStreamSession(interface2) as output_session:
+ with nixnet.FrameInStreamSession(interface1, database) as input_session:
+ with nixnet.FrameOutStreamSession(interface2, database) as output_session:
terminated_cable = six.moves.input('Are you using a terminated cable (Y or N)? ')
if terminated_cable.lower() == "y":
- input_session.intf.can_term = constants.CanTerm.ON
- output_session.intf.can_term = constants.CanTerm.OFF
+ input_session.intf.lin_term = constants.LinTerm.ON
+ output_session.intf.lin_term = constants.LinTerm.OFF
elif terminated_cable.lower() == "n":
- input_session.intf.can_term = constants.CanTerm.ON
- output_session.intf.can_term = constants.CanTerm.ON
+ input_session.intf.lin_term = constants.LinTerm.ON
+ output_session.intf.lin_term = constants.LinTerm.ON
else:
print("Unrecognised input ({}), assuming 'n'".format(terminated_cable))
- input_session.intf.can_term = constants.CanTerm.ON
- output_session.intf.can_term = constants.CanTerm.ON
+ input_session.intf.lin_term = constants.LinTerm.ON
+ output_session.intf.lin_term = constants.LinTerm.ON
- input_session.intf.baud_rate = 125000
- output_session.intf.baud_rate = 125000
+ output_session.intf.lin_master = True
# Start the input session manually to make sure that the first
# frame value sent before the initial read will be received.
input_session.start()
+
+ # Set the schedule. This will also automatically enable master mode.
+ output_session.change_lin_schedule(0)
user_value = six.moves.input('Enter payload [int, int]: ')
try:
@@ -43,9 +46,9 @@
payload_list = [2, 4, 8, 16]
print('Unrecognized input ({}). Setting data buffer to {}'.format(user_value, payload_list))
- id = types.CanIdentifier(0)
+ id = 0
payload = bytearray(payload_list)
- frame = types.CanFrame(id, constants.FrameType.CAN_DATA, payload)
+ frame = types.LinFrame(id, constants.FrameType.LIN_DATA, payload)
print('The same values should be received. Press q to quit')
i = 0