# -*- coding: utf-8 -*-
"""
    AO "NPF Micran" 28.06.2021
    This is an example for G7M generator with SCPI commands
    Python 3.7 example	 
    Frequency sweep with fixed power
    G7M SCPI programming sample

"""

import pyvisa

# Create resource manager
#rm = pyvisa.ResourceManager(r"C:\Lib\MiVISA64.dll")
rm = pyvisa.ResourceManager(r"libMiVisa.so")

# Set device address
inputAddress = input("Enter device address:") 
deviceAddress = "TCPIP::"+ inputAddress +"::8888::SOCKET::GNM"

# Open connection
g7m = rm.open_resource(resource_name = deviceAddress,
                       resource_pyclass = pyvisa.resources.MessageBasedResource, 
                       open_timeout = 5e3)

g7m._encoding = "Windows-1251"
g7m.timeout = 120e3

# Get IDN
print(g7m.query("*IDN?"))
# Set frequency sweep mode
g7m.write("SOURce:FREQuency:MODE SWEEp")
# Set frequency range values
g7m.write( "SOURce:FREQuency:STARt 10MHz;STOP 20GHz")
# Set points count
g7m.write( "SOURce:SWEEp:POINts 501")
# Set power level value
g7m.write( "SOURce:POWer:LEVel -10")
# Set external trigger IN 
#g7m.write("TRIGger:SEQuence:SOURce EXTernal")
# Set trigger IN action (next point)
g7m.write("SOURce:LIST:MODE MANual")
#	Set trigger OUT action (PLL ready)
g7m.write("TRIGger:AUXiliary:INTerval POINt")
#	Enable trigger OUT
g7m.write("TRIGger:AUXiliary ON");
# Set reference generator to internal source
g7m.write("ROSCillator:SOURce INTernal")
# Set triggered mode
g7m.write( "INITiate:CONTinuous OFF")
# Turn ON RF output
g7m.write( "OUTPut:STATe ON")
# Check errors
print(g7m.query("SYSTem:ERRor?"))

# Set trigger
g7m.write( "INITiate:IMMediate");

# Wait for measurement complete
print("OPC=" + g7m.query(r"*OPC?"))
print("Done!")

# Turn OFF RF output
g7m.write( "OUTPut:STATe OFF")
# Check Errors
print(g7m.query("SYSTem:ERRor?"))

g7m.close()
rm.close()