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