###############################################################################
#                                                                              
# Rohde & Schwarz GmbH & Co. KG                                                
# 1MA295 Characterization of LoRa Devices                                  
# Production Testing with CMW100                                   
#                                 
#                                                                              
###############################################################################

import visa
import math

# Variables #

c_frq  = 902000000  # Center Frequency in Hz                                                                
att_out =   0.00    # External Attenuation (Input) in dB                                            
lev     = -30.00    # Level (RMS) in dBm                                       
dga     =   0.00    # Digital Gain in dB                                       
    
# Arbitrary Waveform File #

arbfile = "C:\ProgramData\Rohde-Schwarz\CMW\Data\waveform\KW683_LoRa_125kHz_1frame.wv"


# User Information #

print("----------------------------------------------------------------------")
print("\nRohde & Schwarz GmbH & Co. KG")
print("1MA295 Characterization of LoRa Devices ")
print("Production Testing with CMW100")
print("----------------------------------------------------------------------")

# R&S CMW: General Setup #

print("----------------------------------------------------------------------")
print("R&S CMW100: General Setup.")
print("----------------------------------------------------------------------")
    
CMW.write("*RST")                                                               # Reset Device.                      
CMW.write("*CLS")                                                               # Clear Status.                      
CMW.ask("*OPT?")                                                                # Option Query.                      
CMW.ask("*IDN?")                                                                # Query ID String.                   

# R&S CMW: General Purpose Generator #

print("----------------------------------------------------------------------")
print("R&S CMW100: General Purpose Generator.")
print("----------------------------------------------------------------------")

# Select Connectors and Signal Paths:
CMW.write("ROUT:GPRF:GEN:SCEN:SAL R118, TX11")

# Activate/Deactivates RF Connectors:
CMW.write("CONF:GPRF:GEN:CMWS:USAG:TX:ALL R118, ON, ON, ON, OFF, OFF, OFF, OFF, OFF")

CMW.write("SOUR:GPRF:GEN:RFS:EATT " + str(att_out))                             # Define External Attenuation.       
CMW.write("SOUR:GPRF:GEN:RFS:FREQ " + str(c_frq))                               # Define Center Frequency.      
CMW.write("SOUR:GPRF:GEN:RFS:LEV " + str(lev))                                  # Define Base RMS Level.             
CMW.write("SOUR:GPRF:GEN:RFS:DGA " + str(dga))                                  # Define Digital Gain.               

CMW.write("SOUR:GPRF:GEN:BBM ARB")                                              # Set Baseband Mode to ARB.          
CMW.write("SOUR:GPRF:GEN:ARB:REP CONT")                                         # Set Reputation to Continuous.      
CMW.write("SOUR:GPRF:GEN:ARB:FILE '" + str(arbfile) + "'")                      # Select ARB File.                   
CMW.write("SOUR:GPRF:GEN:STAT ON")                                              # Turn ON the Generator.             

# User Information #

print("----------------------------------------------------------------------")
print("Executing Script finished!")
print("----------------------------------------------------------------------")