__author__ = 'emmanuel'
# 30.07.2014
# http://pyvisa.readthedocs.org/en/1.5/

import visa
import sys
import time



choice = ""

print("[l]ist devices")
print("[g]et info from device")
print("[s]end command to device")
print("[a]sk device")
print("[e]xit")

while True:

    choice = input("command?: ")
    if choice == 'l':
        rm = visa.ResourceManager()
        listDevices = rm.list_resources()   # list all devices on the interface
        x = 0
        for device in listDevices:
            print(device)
            x = x + 1

    elif choice == 'g':
        rm = visa.ResourceManager()
        listDevices = rm.list_resources()   # list all devices on the interface
        x = 0
        for device in listDevices:
            print([x],device)
            x = x + 1
        try:
            whichInterface = input("interface? : ")
            interface = listDevices[int(whichInterface)][:-7]   #get interface name
            # print(listDevices[int(whichInterface)])
            #print(interface)
            device = rm.get_instrument(listDevices[int(whichInterface)])
            print(device.ask("*IDN?"))                          # device available
        except:
            print("That is not a valid interface! ")

    elif choice == 's':
        rm = visa.ResourceManager()
        listDevices = rm.list_resources()   # list all devices on the interface
        x = 0
        for device in listDevices:
            print([x],device)
            x = x + 1
        try:
            whichInterface = input("interface? : ")
            interface = listDevices[int(whichInterface)][:-7]   #get interface name
            device = rm.get_instrument(listDevices[int(whichInterface)])
            GPIB_Command = input("gpib command? : ")
            device.write(GPIB_Command)                      # device available
        except:
            print("That is not a valid interface! ")




    elif choice == 'a':
        rm = visa.ResourceManager()
        listDevices = rm.list_resources()   # list all devices on the interface
        x = 0
        for device in listDevices:
            print([x],device)
            x = x + 1
        try:
            whichInterface = input("interface? : ")
            interface = listDevices[int(whichInterface)][:-7]   #get interface name
            device = rm.get_instrument(listDevices[int(whichInterface)])
            GPIB_Command = input("gpib command? : ")
            print(device.ask(GPIB_Command))                      # device available
        except:
            print("That is not a valid interface! ")


    elif choice == 'e':
        print("good by...")
        time.sleep(2)
        sys.exit()

    else:
        print("That is not a valid input command! ")
        print("[l]ist devices")
        print("[g]et info from device")
        print("[s]end command to device")
        print("[a]sk device")
        print("[e]xit")



