Examples¶
You can download all niswitch examples here
niswitch_connect_channels.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #!/usr/bin/python import argparse import niswitch import sys def example(resource_name, channel1, channel2, topology, simulate): # if we are simulating resource name must be blank resource_name = '' if simulate else resource_name with niswitch.Session(resource_name=resource_name, topology=topology, simulate=simulate) as session: session.connect(channel1=channel1, channel2=channel2) print('Channel ', channel1, ' and ', channel2, ' are now connected.') session.disconnect(channel1=channel1, channel2=channel2) print('Channel ', channel1, ' and ', channel2, ' are now disconnected.') def _main(argsv): parser = argparse.ArgumentParser(description='Performs a connection with NI-SWITCH Channels.', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI switch.') parser.add_argument('-ch1', '--channel1', default='c0', help='Channel One.') parser.add_argument('-ch2', '--channel2', default='r0', help='Channel Two.') parser.add_argument('-t', '--topology', default='Configured Topology', help='Topology.') parser.add_argument('-s', '--simulate', default=False, action='store_true', help='Simulate device.') args = parser.parse_args(argsv) example(args.resource_name, args.channel1, args.channel2, args.topology, args.simulate) def test_example(): example('', 'c0', 'r0', '2737/2-Wire 4x64 Matrix', True) def test_main(): cmd_line = ['--topology', '2737/2-Wire 4x64 Matrix', '--simulate'] _main(cmd_line) def main(): _main(sys.argv[1:]) if __name__ == '__main__': main() |
niswitch_get_device_info.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #!/usr/bin/python import argparse import niswitch import sys def example(resource_name, topology, simulate, device, channel, relay): # if we are simulating resource name must be blank resource_name = '' if simulate else resource_name with niswitch.Session(resource_name=resource_name, topology=topology, simulate=simulate) as session: if device: print('Device Info:') row_format = '{:<18}' * (2) print(row_format.format('Device Name: ', session.io_resource_descriptor)) print(row_format.format('Device Model: ', session.instrument_model)) print(row_format.format('Driver Revision: ', session.specific_driver_revision)) print(row_format.format('Channel count: ', session.channel_count)) print(row_format.format('Relay count: ', session.number_of_relays)) if channel: print('Channel Info:') row_format = '{:6}' + ' ' * 12 + '{:<15}{:<22}{:6}' print(row_format.format('Number', 'Name', 'Is Configuration', 'Is Source')) for i in range(1, session.channel_count + 1): channel_name = session.get_channel_name(index=i) channel = session.channels[channel_name] print(row_format.format(i, channel_name, str(channel.is_configuration_channel), str(channel.is_source_channel))) if relay: print('Relay Info:') row_format = '{:6}' + ' ' * 12 + '{:<15}{:<22}{:6}' print(row_format.format('Number', 'Name', 'Position', 'Count')) for i in range(1, session.number_of_relays + 1): relay_name = session.get_relay_name(index=i) print(row_format.format(i, relay_name, session.get_relay_position(relay_name=relay_name), session.get_relay_count(relay_name=relay_name))) def _main(argsv): parser = argparse.ArgumentParser(description='Prints information for the specified NI-SWITCH.', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI switch.') parser.add_argument('-d', '--device', default=False, action='store_true', help='Prints information for the device') parser.add_argument('-c', '--channel', default=False, action='store_true', help='Prints information for all channels on the device') parser.add_argument('-r', '--relay', default=False, action='store_true', help='Prints information for all relays on the device') parser.add_argument('-t', '--topology', default='Configured Topology', help='Topology.') parser.add_argument('-s', '--simulate', default=False, action='store_true', help='Simulate device.') args = parser.parse_args(argsv) if not (args.device or args.channel or args.relay): print('You must specify at least one of -d, -c, or -r!') parser.print_help() sys.exit(1) example(args.resource_name, args.topology, args.simulate, args.device, args.channel, args.relay) def test_example(): example('', '2737/2-Wire 4x64 Matrix', True, True, True, True) def test_main(): cmd_line = ['--topology', '2737/2-Wire 4x64 Matrix', '--simulate', '--device', '--channel', '--relay', ] _main(cmd_line) def main(): _main(sys.argv[1:]) if __name__ == '__main__': main() |
niswitch_relay_control.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #!/usr/bin/python import argparse import niswitch import sys def example(resource_name, topology, simulate, relay, action): # if we are simulating resource name must be blank resource_name = '' if simulate else resource_name with niswitch.Session(resource_name=resource_name, topology=topology, simulate=simulate) as session: session.relay_control(relay_name=relay, relay_action=niswitch.RelayAction[action]) print('Relay ', relay, ' has had the action ', action, ' performed.') def _main(argsv): parser = argparse.ArgumentParser(description='Performs relay control with NI-SWITCH relays.', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI switch.') parser.add_argument('-r', '--relay', default='k0', help='Relay Name.') parser.add_argument('-a', '--action', default='OPEN', choices=niswitch.RelayAction.__members__.keys(), type=str.upper, help='Relay Action.') parser.add_argument('-t', '--topology', default='Configured Topology', help='Topology.') parser.add_argument('-s', '--simulate', default=False, action='store_true', help='Simulate device.') args = parser.parse_args(argsv) example(args.resource_name, args.topology, args.simulate, args.relay, args.action) def test_example(): example('', '2737/2-Wire 4x64 Matrix', True, 'kr0c0', 'OPEN') def test_main(): cmd_line = ['--topology', '2737/2-Wire 4x64 Matrix', '--simulate', '--relay', 'kr0c0'] _main(cmd_line) def main(): _main(sys.argv[1:]) if __name__ == '__main__': main() |