def test_intf_blink(can_in_interface): """Verify LEDs can be blinked. Since we don't have a camera watching the LEDs, we can at least test if we are having an impact by checking - `ENABLE` works anytime except a session is in use - `DISABLE` always works Sleeps are in place in case this in case a visual confirmation is possible by the developer. """ with system.System() as sys: intfs = list(sys.intf_refs_all) in_intfs = [intf for intf in intfs if intf == can_in_interface] assert len(in_intfs) == 1, "Pre-requisite failed" in_intf = in_intfs[0] # Smoke-test the function calls in_intf.blink(constants.BlinkMode.ENABLE) time.sleep(0.01) in_intf.blink(constants.BlinkMode.DISABLE) time.sleep(0.01) with nixnet.FrameInStreamSession(can_in_interface) as input_session: input_session.intf.baud_rate = 125000 input_session.start() with pytest.raises(errors.XnetError) as excinfo: in_intf.blink(constants.BlinkMode.ENABLE) assert excinfo.value.error_type == constants.Err.PORT_LEDS_BUSY in_intf.blink(constants.BlinkMode.DISABLE) in_intf.blink(constants.BlinkMode.ENABLE) in_intf.blink(constants.BlinkMode.DISABLE)
def test_stream_loopback(can_in_interface, can_out_interface): with nixnet.FrameInStreamSession(can_in_interface) as input_session: with nixnet.FrameOutStreamSession(can_out_interface) as output_session: input_session.intf.baud_rate = 125000 output_session.intf.baud_rate = 125000 # Start the input session manually to make sure that the first # frame value sent before the initial read will be received. input_session.start() payload_list = [2, 4, 8, 16] expected_frames = [ types.CanFrame(0, constants.FrameType.CAN_DATA, bytes(bytearray(payload_list))) ] output_session.frames.write(expected_frames) # Wait 1 s and then read the received values. # They should be the same as the ones sent. time.sleep(1) actual_frames = list(input_session.frames.read(1)) assert len(expected_frames) == len(actual_frames) for i, (expected, actual) in enumerate(zip(expected_frames, actual_frames)): assert_can_frame(i, expected, actual)
def test_lin_stream_loopback(lin_in_interface, lin_out_interface): database_name = 'NIXNET_exampleLDF' with nixnet.FrameInStreamSession(lin_in_interface, database_name) as input_session: with nixnet.FrameOutStreamSession(lin_out_interface, database_name) as output_session: 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 automically enable # master mode output_session.change_lin_schedule(0) payload_list = [2, 4, 8, 16] expected_frames = [ types.LinFrame(0, constants.FrameType.LIN_DATA, bytes(bytearray(payload_list))) ] output_session.frames.write(expected_frames) # Wait 1 s and then read the received values. # They should be the same as the ones sent. time.sleep(1) actual_frames = list(input_session.frames.read(1)) assert len(expected_frames) == len(actual_frames) for i, (expected, actual) in enumerate(zip(expected_frames, actual_frames)): assert_can_frame(i, expected, actual)
def test_session_container(can_in_interface, can_out_interface): with nixnet.FrameInStreamSession(can_in_interface) as input_session: with nixnet.FrameInStreamSession(can_out_interface) as output_session: assert input_session == input_session assert not (input_session == output_session) assert not (input_session == 1) assert not (input_session != input_session) assert input_session != output_session assert input_session != 1 set([input_session]) # Testing `__hash__` print(repr(input_session)) with pytest.warns(errors.XnetResourceWarning): input_session.close()
def test_session_lin_properties(lin_in_interface): """Verify Session properties. Ideally, mutable properties would be set to multiple values and we'd test for the intended side-effect. That'll be a massive undertaking. For now, ensure they are settable and getttable. """ database_name = 'NIXNET_exampleLDF' with nixnet.FrameInStreamSession(lin_in_interface, database_name) as input_session: print(input_session.lin_comm)
import nixnet from nixnet import constants CAN_port = 'CAN5' count = 5 print('Starting the Program') try: with nixnet.FrameInStreamSession(CAN_port) as input_session: input_session.intf.can_term = constants.CanTerm.ON input_session.intf.baud_rate = 125000 frames = input_session.frames.read(count) try: next_item = next(frames) except StopIteration: # exhausted, handle this case print('Emprty....') for frame in frames: print('Received frame:') if frame is not None: print(frame) else: print('Empty Frame detected') except Exception as error: print('Error :', error)
def main(): interface1 = 'CAN1' interface2 = 'CAN2' with nixnet.FrameInStreamSession(interface1) as input_session: with nixnet.FrameOutStreamSession(interface2) 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 elif terminated_cable.lower() == "n": input_session.intf.can_term = constants.CanTerm.ON output_session.intf.can_term = constants.CanTerm.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.baud_rate = 125000 output_session.intf.baud_rate = 125000 # Start the input session manually to make sure that the first # frame value sent before the initial read will be received. input_session.start() user_value = six.moves.input('Enter payload [int, int]: ') try: payload_list = [int(x.strip()) for x in user_value.split(",")] except ValueError: payload_list = [2, 4, 8, 16] print('Unrecognized input ({}). Setting data buffer to {}', user_value, payload_list) id = 0 extended = False payload = bytearray(payload_list) frame = types.CanFrame(id, extended, constants.FrameType.CAN_DATA, payload) print('The same values should be received. Press q to quit') i = 0 while True: for index, byte in enumerate(payload): payload[index] = byte + i frame.payload = payload output_session.frames.write_can([frame]) print('Sent frame with ID %s payload: %s' % (id, payload)) # Wait 1 s and then read the received values. # They should be the same as the ones sent. time.sleep(1) count = 1 frames = input_session.frames.read_can(count) for frame in frames: print('Received frame: ') pp.pprint(frame) i += 1 if max(payload) + i > 0xFF: i = 0 inp = six.moves.input('Hit enter to continue (q to quit): ') if inp.lower() == 'q': break print('Data acquisition stopped.')
def main(): interface1 = 'LIN1' interface2 = 'LIN2' database = 'NIXNET_exampleLDF' 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.lin_term = constants.LinTerm.ON output_session.intf.lin_term = constants.LinTerm.OFF elif terminated_cable.lower() == "n": 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.lin_term = constants.LinTerm.ON output_session.intf.lin_term = constants.LinTerm.ON 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: payload_list = [int(x.strip()) for x in user_value.split(",")] except ValueError: payload_list = [2, 4, 8, 16] print('Unrecognized input ({}). Setting data buffer to {}'. format(user_value, payload_list)) id = 0 payload = bytearray(payload_list) frame = types.LinFrame(id, constants.FrameType.LIN_DATA, payload) print('The same values should be received. Press q to quit') i = 0 while True: for index, byte in enumerate(payload): payload[index] = byte + i frame.payload = payload output_session.frames.write([frame]) print('Sent frame with ID {} payload: {}'.format(id, payload)) # Wait 1 s and then read the received values. # They should be the same as the ones sent. time.sleep(1) count = 1 frames = input_session.frames.read(count) for frame in frames: print('Received frame: {}'.format(frame)) print(' payload={}'.format( list(six.iterbytes(frame.payload)))) i += 1 if max(payload) + i > 0xFF: i = 0 inp = six.moves.input('Hit enter to continue (q to quit): ') if inp.lower() == 'q': break print('Data acquisition stopped.')