def test_singlepoint_loopback(can_in_interface, can_out_interface): database_name = 'NIXNET_example' cluster_name = 'CAN_Cluster' frame_name = ['CANEventFrame1', 'CANEventFrame2'] with nixnet.FrameInSinglePointSession(can_in_interface, database_name, cluster_name, frame_name) as input_session: with nixnet.FrameOutSinglePointSession(can_out_interface, database_name, cluster_name, frame_name) as output_session: # Start the input session manually to make sure that the first # frame value sent before the initial read will be received. input_session.start() first_payload_list = [2, 4, 8, 16] second_payload_list = [1, 3] expected_frames = [ types.CanFrame(66, constants.FrameType.CAN_DATA, bytes(bytearray(first_payload_list))), types.CanFrame(67, constants.FrameType.CAN_DATA, bytes(bytearray(second_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()) 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_can_frame_equality(): empty_frame = types.CanFrame(0, constants.FrameType.CAN_DATA, b'') base_frame = types.CanFrame(0, constants.FrameType.CAN_DATA, b'\x01') assert empty_frame == empty_frame assert not (empty_frame == base_frame) assert not (empty_frame == 5) assert not (empty_frame != empty_frame) assert empty_frame != base_frame assert empty_frame != 5
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 main(): with system.System() as my_system: database_alias = 'custom_database' database_filepath = os.path.join(os.path.dirname(__file__), 'databases\custom_database.dbc') default_baud_rate = 500000 my_system.databases.add_alias(database_alias, database_filepath, default_baud_rate) database_name = 'custom_database' cluster_name = 'CAN_Cluster' output_frame = 'CANEventFrame1' interface = 'CAN1' with nixnet.FrameOutQueuedSession(interface, database_name, cluster_name, output_frame) as output_session: terminated_cable = six.moves.input( 'Are you using a terminated cable (Y or N)? ') if terminated_cable.lower() == "y": output_session.intf.can_term = constants.CanTerm.OFF elif terminated_cable.lower() == "n": output_session.intf.can_term = constants.CanTerm.ON else: print("Unrecognised input ({}), assuming 'n'".format( terminated_cable)) output_session.intf.can_term = constants.CanTerm.ON 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 = types.CanIdentifier(0) payload = bytearray(payload_list) frame = types.CanFrame(id, constants.FrameType.CAN_DATA, payload) print("Writing CAN frames using {} alias:".format(database_name)) i = 0 while i < 3: 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( frame.identifier, list(frame.payload))) i += 1 with system.System() as my_system: del my_system.databases[database_name]
def test_flush_baseline(can_in_interface, can_out_interface): """Demonstrate that the non-flush version of the code works. Assumes test_frames.test_queued_loopback works. """ database_name = 'NIXNET_example' cluster_name = 'CAN_Cluster' frame_name = 'CANEventFrame1' with nixnet.FrameInQueuedSession( can_in_interface, database_name, cluster_name, frame_name) as input_session: with nixnet.FrameOutQueuedSession( can_out_interface, database_name, cluster_name, frame_name) as output_session: output_session.auto_start = False input_session.start() dropped_frames = [ types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04')] output_session.frames.write(dropped_frames) expected_frames = [ types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09')] output_session.frames.write(expected_frames) expected_frames = dropped_frames + expected_frames output_session.start() # 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(2)) 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 send(self, msg, timeout=None): """ Send a message using NI-XNET. :param can.Message msg: Message to send :param float timeout: Max time to wait for the device to be ready in seconds, None if time is infinite :raises can.exceptions.CanOperationError: If writing to transmit buffer fails. It does not wait for message to be ACKed currently. """ if timeout is None: timeout = constants.TIMEOUT_INFINITE if msg.is_remote_frame: type_message = constants.FrameType.CAN_REMOTE elif msg.is_error_frame: type_message = constants.FrameType.CAN_BUS_ERROR elif msg.is_fd: if msg.bitrate_switch: type_message = constants.FrameType.CANFDBRS_DATA else: type_message = constants.FrameType.CANFD_DATA else: type_message = constants.FrameType.CAN_DATA can_frame = types.CanFrame( types.CanIdentifier(msg.arbitration_id, msg.is_extended_id), type=type_message, payload=msg.data, ) try: self.__session_send.frames.write([can_frame], timeout) except errors.XnetError as error: raise CanOperationError(f"{error.args[0]} ({error.error_type})", error.error_code) from None
def test_start_explicit(can_in_interface, can_out_interface): """Demonstrate that data is properly sent out on an explicit start. Assumes test_frames.test_queued_loopback works """ database_name = 'NIXNET_example' cluster_name = 'CAN_Cluster' frame_name = 'CANEventFrame1' with nixnet.FrameInQueuedSession( can_in_interface, database_name, cluster_name, frame_name) as input_session: with nixnet.FrameOutQueuedSession( can_out_interface, database_name, cluster_name, frame_name) as output_session: output_session.auto_start = False # Start the input session manually to make sure that the first # frame value sent before the initial read will be received. input_session.start() expected_frames = [ types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04')] output_session.frames.write(expected_frames) output_session.start() # 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 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 test_wait_for_transmit_complete(can_in_interface, can_out_interface): """Verifies wait_for_transmit_complete does not fail catastrophically. We can at least see how long it takes us to wait. Longer term, we should switch the test to start waiting and then call ``input_session.start`` and ensure it unblocks after that call. To see the wait time, run py.test with ``-s``_. Assumes test_frames.test_queued_loopback works. """ database_name = 'NIXNET_example' cluster_name = 'CAN_Cluster' frame_name = 'CANEventFrame1' with nixnet.FrameInQueuedSession( can_in_interface, database_name, cluster_name, frame_name) as input_session: with nixnet.FrameOutQueuedSession( can_out_interface, database_name, cluster_name, frame_name) as output_session: output_session.auto_start = False input_session.start() expected_frames = [ types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'), types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09')] output_session.start() initial = time.time() output_session.frames.write(expected_frames) written = time.time() output_session.wait_for_transmit_complete(10) finished = time.time() print("Write took {} s".format(written - initial)) print("Wait took {} s".format(finished - written))
def main(): database_name = 'NIXNET_example' cluster_name = 'CAN_Cluster' input_frame = 'CANEventFrame1' output_frame = 'CANEventFrame1' interface1 = 'CAN1' interface2 = 'CAN2' with nixnet.FrameInQueuedSession(interface1, database_name, cluster_name, input_frame) as input_session: with nixnet.FrameOutQueuedSession(interface2, database_name, cluster_name, output_frame) 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 # 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 {}'. format(user_value, payload_list)) id = types.CanIdentifier(0) payload = bytearray(payload_list) frame = types.CanFrame(id, constants.FrameType.CAN_DATA, payload) 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.')