# -*- coding: utf-8 -*-
"""
    AO "NPF Micran" 07.07.2021
    This is an example for PLG with SCPI commands
    Python 3.7 example	 
    Setting fixed power and fixed frequency mode
"""

import pyvisa
import numpy 

# Create resource manager
rm = pyvisa.ResourceManager(r"C:\Lib\MiVISA64.dll")
# Set device address
SN = input("Enter serial number:") 
device_address = f"USB0::0x2b70::0x0040::{SN}::0::INSTR"

# Open connection 
rsrc = rm.open_resource(device_address, 
                        access_mode = pyvisa.constants.AccessModes.exclusive_lock, 
                        resource_pyclass = pyvisa.resources.MessageBasedResource,
                        open_timeout=20000)

# Set encoding
rsrc._encoding = "Windows-1251"

# Set timeout
rsrc.timeout = 120000

# Send parameters
# Device ID
print(rsrc.query("*IDN?")) 
# Send "Reset" command
rsrc.write("*RST")
# Send "Clear status" command
rsrc.write("*CLS")
# Setting frequency to 1 GHz
rsrc.write("FREQ 1GHz")
# Setting power to 1 dBm
rsrc.write("POW 1")
# Power on
rsrc.write("OUTPut:STATe ON")
# Check errors
print(rsrc.query("SYSTem:ERRor?")) 
# Switch LF generator and setting shape SINE
rsrc.write("LFO:FUNC:SHAP SIN")
# Setting frequency to 9 kHz
rsrc.write("LFO:FUNC:FREQ 9 kHz")
# Setting amplitude 3 V
rsrc.write("LFO:AMP 3")
# Wait for user action
input("Press enter to continue...")
# Send "Reset" command
rsrc.write("*RST")
# Close session
rsrc.close()
# Close resource manager
rm.close()