def test_queued_loopback(can_in_interface, can_out_interface): 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: # 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(66, 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_session_properties_transition(can_out_interface): """Verify Session properties relationship to session start/stop.""" database_name = 'NIXNET_example' cluster_name = 'CAN_Cluster' frame_name = 'CANEventFrame1' with nixnet.FrameOutQueuedSession( can_out_interface, database_name, cluster_name, frame_name) as output_session: with pytest.raises(errors.XnetError): print(output_session.time_start) with pytest.raises(errors.XnetError): print(output_session.time_communicating) assert output_session.state == constants.SessionInfoState.STOPPED output_session.start() print(output_session.time_start) print(output_session.time_communicating) assert output_session.state == constants.SessionInfoState.STARTED output_session.stop() with pytest.raises(errors.XnetError): print(output_session.time_start) with pytest.raises(errors.XnetError): print(output_session.time_communicating) assert output_session.state == constants.SessionInfoState.STOPPED output_session.check_fault()
def test_session_properties(nixnet_out_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_example' cluster_name = 'CAN_Cluster' frame_name = 'CANEventFrame1' with nixnet.FrameOutQueuedSession(nixnet_out_interface, database_name, cluster_name, frame_name) as output_session: assert output_session.database_name == database_name assert output_session.cluster_name == cluster_name assert output_session.mode == constants.CreateSessionMode.FRAME_OUT_QUEUED assert output_session.application_protocol == constants.AppProtocol.NONE assert output_session.protocol == constants.Protocol.CAN assert output_session.auto_start output_session.auto_start = False assert not output_session.auto_start print(output_session.num_pend) print(output_session.num_unused) print(output_session.payld_len_max) print(output_session.queue_size) output_session.queue_size = 2040 assert output_session.queue_size == 2040 print(output_session.resamp_rate) output_session.resamp_rate = 30 assert output_session.resamp_rate == 30
def test_intf_lin_properties(lin_out_interface): database_name = 'NIXNET_exampleLDF' cluster_name = 'Cluster' frame_name = 'MasterFrame1' with nixnet.FrameOutQueuedSession(lin_out_interface, database_name, cluster_name, frame_name) as output_session: # todo: Add testing for other LIN properties in session interface. output_session.intf.lin_checksum_to_in_strm = True assert output_session.intf.lin_checksum_to_in_strm
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_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 test_session_frames_properties(can_out_interface): """Verify Frames properties. These are pretty transient and can't easily be verified against known good values. For now, we'll just verify the calls don't call catastrophically and someone can always run py.test with ``-s``_. """ database_name = 'NIXNET_example' cluster_name = 'CAN_Cluster' frame_name = 'CANEventFrame1' with nixnet.FrameOutQueuedSession(can_out_interface, database_name, cluster_name, frame_name) as output_session: print(output_session.frames.payld_len_max)
def test_intf_properties_settable_only_when_stopped(can_out_interface): """Verify Interface properties settable only when stopped. Ensure that Interface properties are only allowed to be set when the session is stopped. """ database_name = 'NIXNET_example' cluster_name = 'CAN_Cluster' frame_name = 'CANEventFrame1' with nixnet.FrameOutQueuedSession( can_out_interface, database_name, cluster_name, frame_name) as output_session: # Start the session manually output_session.start() # These should error because interfrace properties can only be modified # when the session is stopped. Note that we've only picked a select few # interface properties to test this behavior. with pytest.raises(errors.XnetError) as term_excinfo: output_session.intf.can_term = constants.CanTerm.ON assert term_excinfo.value.error_type == constants.Err.OBJECT_STARTED with pytest.raises(errors.XnetError) as baud_rate_excinfo: output_session.intf.baud_rate = 100000 assert baud_rate_excinfo.value.error_type == constants.Err.OBJECT_STARTED with pytest.raises(errors.XnetError) as transmit_pause_excinfo: output_session.intf.can_transmit_pause = True assert transmit_pause_excinfo.value.error_type == constants.Err.OBJECT_STARTED output_session.stop() # Should not error because the session has stopped. output_session.intf.can_term = constants.CanTerm.ON output_session.intf.baud_rate = 100000 output_session.intf.can_transmit_pause = True assert output_session.intf.can_term == constants.CanTerm.ON assert output_session.intf.baud_rate == 100000 assert output_session.intf.can_transmit_pause
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 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(): #ibs = IBS200_GEN1('LIN2', 'hella_gen1_ibs') ibs = IBS_GLOBAL_GEN2('LIN2', 'hella_gen2_ibs') # User Defined Configuration C_NOMINAL = 30 #Ah U0_MIN = 11200 #mV U0_MAX = 12650 #mV IQBATT = 250 #mA(100mA Default) ICHRG_MIN = 50 #mA (50mA Default) BATT_TECH = 'AGM' # Set Master payloads ibs.set_nominal_capacity(capacity_ah=C_NOMINAL) ibs.set_u0_minmax(u0_min = U0_MIN, u0_max = U0_MAX) #ibs.set_ibatt_quiescent(iqbatt = IQBATT, ichargemin = ICHRG_MIN) #ibs.set_batt_tech(batt_tech = 'AGM') # Setup LIN Sessions with nixnet.FrameInSinglePointSession(ibs.interface, ibs.database, ibs.cluster, ibs.slave_resp_input_frames) as input_session: with nixnet.FrameOutQueuedSession(ibs.interface, ibs.database, ibs.cluster, ibs.master_req_output_frames) as output_session: output_session.intf.lin_term = constants.LinTerm.ON output_session.intf.lin_master = True output_session.change_lin_schedule(ibs.lin_diag_schedule) def write_LIN(session, id, payload): """ Write LIN Frame to XNET Session.""" print('Sending MasterReq Frame : ID:{} Payload:{}'.format(id,[hex(val) for val in payload])) frame = types.LinFrame(id, type=constants.FrameType.LIN_DATA, payload=bytearray(payload)) output_session.frames.write([frame]) def read_LIN(session): """ Read LIN Frame from XNET Session.""" slave_frame, = input_session.frames.read(frame_type=types.LinFrame) print('Received SlaveResp Frame :{}'.format(slave_frame)) data =[int(struct.unpack('B',val)[0]) for val in slave_frame.payload] print('SlaveResp Payload = {}'.format([hex(val) for val in data])) return data input_session.flush() # Wake up LIN Bus print('0) Waking up LIN Bus...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['Empty_Frame']) slave_frame, = input_session.frames.read(frame_type=types.LinFrame) time.sleep(0.1) # Configure IBS print('Check Table State...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['BattTable_State']) time.sleep(0.35) read_LIN(input_session) print('Switch Table OFF...') ibs.set_switch_table_OnOff(state='Off') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['BattTable_OnOff']) time.sleep(0.35) read_LIN(input_session) print('Change C_NOMINAL...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['BattCap_Write']) time.sleep(0.35) read_LIN(input_session) print('Change U0 MIN/MAX...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['U0_MinMax_Write']) time.sleep(0.35) read_LIN(input_session) # print('Change Batt IQ/Icharge_min...') # write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['IQbatt_Write']) # time.sleep(0.25) # read_LIN(input_session) print('Allow IBS to save all parameters (10s wait)...') print '[' for i in range(0,100): print '#', time.sleep(0.1) print(']\n') print('Check Table State...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['BattTable_State']) time.sleep(0.35) read_LIN(input_session) print('Check C_Nominal...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['BattCap_Read']) time.sleep(0.35) read_LIN(input_session) print('Check U0 MIN/MAX...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['U0_MinMax_Read']) time.sleep(0.35) read_LIN(input_session) # print('Check Batt IQ/Charge_min...') # write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['IQbatt_Read']) # time.sleep(0.25) # read_LIN(input_session) print('Done!')
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 {}', user_value, payload_list) id = 0 extended = False payload = bytearray(payload_list) frame = types.CanFrame(id, extended, 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_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(): #ibs = IBS200_GEN1('LIN2', 'hella_gen1_ibs') ibs = IBS_GLOBAL_GEN2('LIN2', 'hella_gen2_ibs') # User Defined Configuration C_NOMINAL = 30 #Ah U0_MIN = 11250 #mV U0_MAX = 12800 #mV IQBATT = 200 #mA(100mA Default) BATT_TECH = 'AGM' # Set Master payloads ibs.set_nominal_capacity(capacity_ah=C_NOMINAL) ibs.set_u0_minmax(u0_min = U0_MIN, u0_max = U0_MAX) #ibs.set_ibatt_quiescent(iqbatt = IQBATT) ibs.set_batt_tech(batt_tech = 'AGM') # Setup LIN Sessions with nixnet.FrameInSinglePointSession(ibs.interface, ibs.database, ibs.cluster, ibs.slave_resp_input_frames) as input_session: with nixnet.FrameOutQueuedSession(ibs.interface, ibs.database, ibs.cluster, ibs.master_req_output_frames) as output_session: output_session.intf.lin_term = constants.LinTerm.ON output_session.intf.lin_master = True output_session.change_lin_schedule(lin_schedule) def write_LIN(session, id, payload): """ Write LIN Frame to XNET Session.""" print('Sending MasterReq Frame : ID:{} Payload:{}'.format(id,[hex(val) for val in payload])) frame = types.LinFrame(id, type=constants.FrameType.LIN_DATA, payload=bytearray(payload)) output_session.frames.write([frame]) def read_LIN(session): """ Read LIN Frame from XNET Session.""" slave_frame, = input_session.frames.read(frame_type=types.LinFrame) print('Received SlaveResp Frame :{}'.format(slave_frame)) data =[int(struct.unpack('B',val)[0]) for val in slave_frame.payload] print('SlaveResp Payload = {}'.format([hex(val) for val in data])) return data input_session.flush() # Wake up LIN Bus print('Waking up LIN Bus...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['Empty_Frame']) slave_frame, = input_session.frames.read(frame_type=types.LinFrame) time.sleep(0.1) print('Check Table State...') # frame_count = input_session.frames.count() # print('frames in buffer = {}'.format(frame_count)) write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['BattTable_State']) time.sleep(0.25) read_LIN(input_session) print('Check Table State...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['BattTable_State']) time.sleep(0.25) read_LIN(input_session) print('Check C_Nominal...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['BattCap_Read']) time.sleep(0.25) read_LIN(input_session) print('Check Batt Type...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['BattType_Read']) time.sleep(0.25) read_LIN(input_session) print('Check U0 MIN/MAX...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['U0_MinMax_Read']) time.sleep(0.25) read_LIN(input_session) print('Check Batt Tech...') write_LIN(output_session, ibs.MasterReqId, ibs.master_payloads['Batt_Tech_Read']) time.sleep(0.25) read_LIN(input_session) print('Done!')