#This example shows how to create a signal using the python enviromnent and
#how to load it into the Arbitrary Buffer of the Function Generator.

#On channel 1 is loaded a Sine waveform from remote, you can choose the
#number of points between 2 and 16384.
#On channel 2 it is set a Sinc function from predefined list.

#For both channels you can choose amplitude, offset and frequency.

###########################################################################

#Parameters
Instrument_Addr = "TCPIP0::192.168.0.24::inst0::INSTR"

CH1_Amplitude = 5
CH1_Offset = 0
CH1_Frequency = 2 #frequency unit is MHZ
SampleNum = 16384

CH2_Amplitude = 1
CH2_Offset = 0.5
CH2_Frequency = 1 #frequency unit is MHZ

###########################################################################

import visa
import math
import time


#Create a Sine Waveform
byteSamples = bytearray(2 * SampleNum)#declare the array of bytes
i=0;
while (i<SampleNum) :
    Func = 8191 * (math.sin(2 * math.pi * i / SampleNum) + 1)
    #Func=i;
    byteSamples[2 * i] = (int)(Func / 256)       #upper
    byteSamples[2 * i + 1] = (int)(Func % 256)    #lower
    i=i+1

try:
    #Search the VISA instrument
    rm = visa.ResourceManager()

    #Connect with th AFG (insert the AFG name)
    AFG = rm.open_resource(Instrument_Addr)

    #Set the VISA timeout
    AFG.timeout = 60000 #ms, it is 60s

    #Identify the instrument
    print("You are connected to the instrument:\n",AFG.query("*IDN?"))


    #Reset the instrument
    AFG.write("*RST")
    
    #Send analog waveform
    strCmd = 'TRACE1:DATA '
    AFG.write_binary_values(strCmd, byteSamples, datatype='B', is_big_endian=True)

    #Set the carrier CH1
    AFG.write("SOURce1:FUNCtion:SHAPe ARBB")

    #Set amplitude and offset CH1
    cmd = "SOURce1:VOLTage:AMPLitude " + str(CH1_Amplitude)
    AFG.write(cmd)

    cmd = "SOURce1:VOLTage:OFFSet " + str(CH1_Offset)
    AFG.write(cmd)

    #Set the frequency CH1
    cmd = "SOURce1:FREQuency " + str(CH1_Frequency) + "MHZ"
    AFG.write(cmd)

#QUA FUNZIONA
    #AFG.write("SOURce1:FUNCtion:SHAPe ARBB")

    #Set the carrier CH2
    AFG.write("SOURce2:FUNCtion:SHAPe SINC")

    #Set amplitude and offset CH2
    cmd = "SOURce2:VOLTage:AMPLitude " + str(CH2_Amplitude)
    AFG.write(cmd)
    cmd = "SOURce2:VOLTage:OFFSet " + str(CH2_Offset)
    AFG.write(cmd)

    #Set the frequency CH2
    cmd = "SOURce2:FREQuency " + str(CH2_Frequency) + "MHZ"
    AFG.write(cmd)

    
    
    #Activate the outputs
    AFG.write("OUTPut1:STATe 1")
    AFG.write("OUTPut2:STATe 1")

    #Start the AFG
    AFG.write("AFGControl:START")
    
    #Query for errors until receive: "0,No error"
    print("Errors:")
    err = AFG.query("SYST:ERR?")
    print (err)
    while (err.find("0") < 0) :
        err = AFG.query('SYST:ERR?')
        print (err)                   
  
    #Close the VISA resources
    AFG.close()
    rm.close()

except Exception as e:
    print("Error: instrument communication: ",e)


input("Press enter to exit")
