'Software for controlling Function Generator Juntek PSG9080
'UNILABS EXPERIMENTS 2022
'Version 1.7
'05/07/2022
'Set Speed to 7 or higher

Window MinSize
Controls ClearTextBox

Button 1 Text PSG9080
Button PSG9080 Text "Init COM Port"
Button PSG9080 Color Flashing Gold LightGoldenrodYellow
Button 17 Text PSG9080_2
Button PSG9080_2 Text "Juntek PSG9080"

Button 2 Text start1
Button start1 Text "Start Ch1"
Button start1 Color Tomato
Button 18 Text start2
Button start2 Text "Start Ch2"
Button start2 Color Tomato

Button 3 Text waveform1
Button waveform1 Text "Waveform1"
Button waveform1 Color Plum
Button 19 Text waveform2
Button waveform2 Text "Waveform2"
Button waveform2 Color Plum

Button 4 Text Freq1
Button Freq1 Text "Frequency1"
Button Freq1 Color Gold
Button 20 Text Freq2
Button Freq2 Text "Frequency2"
Button Freq2 Color Gold

Button 5 Text Amplitude1
Button Amplitude1 Text "V Amplitude1"
Button Amplitude1 Color Yellow
Button 21 Text Amplitude2
Button Amplitude2 Text "V Amplitude2"
Button Amplitude2 Color Yellow

Button 6 Text Offset1
Button Offset1 Text "Offset1"
Button Offset1 Color Yellow5
Button 22 Text Offset2
Button Offset2 Text "Offset2"
Button Offset2 Color Yellow5

Button 7 Text Duty1
Button Duty1 Text "Duty Cycle1"
Button Duty1 Color BurlyWood
Button 23 Text Duty2
Button Duty2 Text "Duty Cycle2"
Button Duty2 Color BurlyWood

Button 8 Text Phase1
Button Phase1 Text "Phase ∠ Ch1"
Button Phase1 Color DarkGoldenrod
Button 24 Text Phase2
Button Phase2 Text "Phase ∠ Ch2"
Button Phase2 Color DarkGoldenrod

Button 9 Text Trigger1
Button Trigger1 Text "Trigger 1"
Button Trigger1 Color Red1
Button 25 Text Trigger2
Button Trigger2 Text "Trigger 2"
Button Trigger2 Color Red1

Button 10 Text PWM_set1
Button PWM_set1 Text "PWM set1"
Button PWM_set1 Color LightSteelBlue
Button 26 Text PWM_set2
Button PWM_set2 Text "PWM set2"
Button PWM_set2 Color LightSteelBlue

Button 34 Text Modulation_Select
Button Modulation_Select Text "Modulation Type"
Button Modulation_Select Color LightSteelBlue

Button 35 Text PWM_Inversion
Button PWM_Inversion Text "PWM Inversion"
Button PWM_Inversion Color LightSteelBlue

Button 36 Text Burst_idle
Button Burst_idle Text "Burst idle"
Button Burst_idle Color Green

Button 37 Text Polarity
Button Polarity Text "Polarity"
Button Polarity Color Green

Button 38 Text Burst_number
Button Burst_number Text "Burst Pulse number"
Button Burst_number Color Green

Button 33 Text Brightness
Button Brightness Text "Set Brightness"
Button Brightness Color Lavender

Button 49 Text Sweep_set
Button Sweep_set Text "Sweep_set"
Button Sweep_set Color Indigo
Button 50 Text Sweep_VControl
Button Sweep_VControl Text "Sweep V Control"
Button Sweep_VControl Color Indigo
Button 51 Text Sweep_freq
Button Sweep_freq Text "Sweep frequency set"
Button Sweep_freq Color DarkViolet

Button 52 Text Sweep_Vpp
Button Sweep_Vpp Text "Sweep Vpp set"
Button Sweep_Vpp Color DarkViolet
Button 53 Text Sweep_Duty
Button Sweep_Duty Text "Sweep Duty set"
Button Sweep_Duty Color DarkOrchid

Button 54 Text Set_Sweep_Interface
Button Set_Sweep_Interface Text "Set Sweep" + CR + "Interface"
Button Set_Sweep_Interface Color Crimson
Button 55 Text Set_Modulation_interface
Button Set_Modulation_interface Text "Set Modulation" + CR + "Interface"
Button Set_Modulation_interface Color Crimson
Button 56 Text Set_Waveform_Interface
Button Set_Waveform_Interface Text "Set Waveform" + CR + "Interface"
Button Set_Waveform_Interface Color Crimson
Button 57 Text Set_VCO_Interface
Button Set_VCO_Interface Text "Set VCO" + CR + "Interface"
Button Set_VCO_Interface Color Crimson
Button 58 Text Set_Measurement_Interface
Button Set_Measurement_Interface Text "Set Measurement" + CR + "Interface"
Button Set_Measurement_Interface Color Crimson

Button 65 Text Load_mem
Button Load_mem Text "Load" + CR + "from Memory"
Button Load_mem Color DeepSkyBlue
Button 66 Text Save_mem
Button Save_mem Text "Save" + CR + "To Memory"
Button Save_mem Color DeepSkyBlue
Button 67 Text Clear_mem
Button Clear_mem Text "Clear" + CR + "Memory"
Button Clear_mem Color DeepSkyBlue

Button 39 Text Read_AWG
Button Read_AWG Text "Read Arbitrary" + CR + "Waveform"
Button Read_AWG Color LemonChiffon

Button 40 Text Write_AWG
Button Write_AWG Text "Write Arbitrary" + CR + "Waveform"
Button Write_AWG Color LemonChiffon

Button 41 Text Create_Example_AW
Button Create_Example_AW Text "Create" + CR + "Example AW"
Button Create_Example_AW Color LemonChiffon

'Variables
Variable Numeric serial_port_number
Variable Numeric port_waiting_time ' in seconds
Variable Numeric start1_flag
Variable Numeric start2_flag
Variable Numeric sweep_freq_on
Variable Numeric PSG9080_detected

'Variables init
serial_port_number = -1
port_waiting_time = 0.1 ' seconds
start1_flag = 0
start2_flag = 0
sweep_freq_on = 0
PSG9080_detected = 0

'First time init buttons
For v1 = 2 To 16
	Button v1 Disabled
Next
For v1 = 18 To 67
	Button v1 Disabled
Next


'---------------------------------
'MAIN CYCLE
'---------------------------------
For v2 = 0 To Infinity
	If PSG9080_detected = 1
		COM Open serial_port_number 115200
		
	'Read Channels ON status
		COM WriteLine ":r10=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,4),1)
		v4 = Left(Right(s3,2),1)
		If v3 = 1
			Button start1 Text "Ch1 ON"
			Button start1 Color GreenYellow
		Else
			Button start1 Text "Ch1 OFF"
			Button start1 Color Tomato
		EndIf
		If v4 = 1
			Button start2 Text "Ch2 ON"
			Button start2 Color GreenYellow
		Else
			Button start2 Text "Ch2 OFF"
			Button start2 Color Tomato
		EndIf
		
	'Read waveforms
		COM WriteLine ":r11=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,4),3)
		Button waveform1 Text "Waveform: " + v3
		COM WriteLine ":r12=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,4),3)
		Button waveform2 Text "Waveform: " + v3
		
	'Read set Frequencies
		COM WriteLine ":r13=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,14),11)
		s4 = "Frequency:" + CR
		s4 = s4 + v3/1000 + "Hz"
		Button Freq1 Text s4
		COM WriteLine ":r14=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,14),11)
		s4 = "Frequency:" + CR
		s4 = s4 + v3/1000 + "Hz"
		Button Freq2 Text s4
		
	'Read V Amplitude
		COM WriteLine ":r15=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,6),5)
		Button Amplitude1 Text "Vpp: " + v3/1000 + "V"
		COM WriteLine ":r16=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,6),5)
		Button Amplitude2 Text "Vpp: " + v3/1000 + "V"
		
	'Read V Offset
		COM WriteLine ":r17=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,5),4)
		Button Offset1 Text "Offset: " + (v3-1000)/100 + "V"
		COM WriteLine ":r18=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,5),4)
		Button Offset2 Text "Offset: " + (v3-1000)/100 + "V"
		
	'Read Duty Cycle
		COM WriteLine ":r19=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,5),4)
		Button Duty1 Text "Duty: " + (v3/100) + "%"
		COM WriteLine ":r20=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,5),4)
		Button Duty2 Text "Duty: " + (v3/100) + "%"
		
	'Read Phase
		COM WriteLine ":r21=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,6),5)
		Button Phase1 Text "Phase ∠: " + (v3/100) + "°"
		COM WriteLine ":r22=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,6),5)
		Button Phase2 Text "Phase ∠: " + (v3/100) + "°"
		
	'Read Brightness
		COM WriteLine ":r28=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,4),3)
		Button Brightness Text "Bright: " + v3 + "%"
		
	'Read Modulation status
		COM WriteLine ":r40=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,4),1)
		v4 = Left(Right(s3,2),1)
		s4 = "Modulation1: " + v3 + CR
		s4 = s4 + "Modulation2: " + v4 + CR
		Button Modulation_Select Text s4
		
	'Read PWM settings
		COM WriteLine ":r53=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,11),10)
		COM WriteLine ":r55=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v4 = Left(Right(s3,11),10)
		s4 = "PWM width: " + (v3/1000) + "µs" + CR
		s4 = s4 + "PWM period: " + (v4/100) + "µs"
		Button PWM_set1 Text s4
		COM WriteLine ":r54=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,11),10)
		COM WriteLine ":r56=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v4 = Left(Right(s3,11),10)
		s4 = "PWM width: " + (v3/1000) + "µs" + CR
		s4 = s4 + "PWM period: " + (v4/100) + "µs"
		Button PWM_set2 Text s4
		
	'Read PWM Inversion
		COM WriteLine ":r57=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,4),1)
		v4 = Left(Right(s3,2),1)
		s4 = "Ch1 PWM Pol: " + v3 + CR
		s4 = s4 + "Ch2 PWM Pol: " + v4
		Button PWM_Inversion Text s4
		
	'Read Burst idle mode
		COM WriteLine ":r58=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,4),1)
		v4 = Left(Right(s3,2),1)
		s4 = "Ch1 Burst idle: " + v3 + CR
		s4 = s4 + "Ch2 Burst idle: " + v4 + CR
		Button Burst_idle Text s4
		
	'Read Polarity
		COM WriteLine ":r59=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,4),1)
		v4 = Left(Right(s3,2),1)
		s4 = "Ch1 Polarity: " + v3 + CR
		s4 = s4 + "Ch2 Polarity: " + v4
		Button Polarity Text s4
		
	'Read Burst Pulse Number
		COM WriteLine ":r61=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,22),10)
		v4 = Left(Right(s3,11),10)
		s4 = "Ch1 Burst number: " + v3 + CR
		s4 = s4 + "Ch2 Burst Number: " + v4
		Button Burst_number Text s4
		
	'Read Sweep setttings
		COM WriteLine ":r64=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,12),1)
		v4 = Left(Right(s3,10),5)
		v6 = Left(Right(s3,4),1)
		v7 = Left(Right(s3,2),1)
		If v3 = 0
			s4 = "Ch1 Sweep" + CR
		Else
			s4 = "Ch2 Sweep" + CR
		EndIf
		s4 = s4 + "Time range: " + (v4/100) + "s" + CR
		Select v6
			Case 0
				s5 = "→"
			Case 1
				s5 = "←"
			Case 2
				s5 = "→←"
		EndSelect
		s4 = s4 + "Direction: " + s5 + CR
		If v7 = 0
			s5 = "Linear"
		Else
			s5 = "Logarithmic"
		EndIf
		s4 = s4 + s5
		Button Sweep_set Text s4
		If sweep_freq_on = 0
			Button Sweep_set Color Indigo
		EndIf
		
	'Read Sweep frequency status
		COM WriteLine ":r65=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,4),1)
		If v3 = 0
			sweep_freq_on = 0
			Button Sweep_VControl Color Indigo
			Button Sweep_VControl Text "Sweep OFF"
		Else
			sweep_freq_on = 1
			Button Sweep_VControl Color GreenYellow
			Button Sweep_VControl Text "Sweep ON"
		EndIf
		
	'Read start and end sweep frequencies
		COM WriteLine ":r66=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,11),10)
		COM WriteLine ":r67=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v4 = Left(Right(s3,11),10)
		s4 = "Start ν: " + (v3/10) + "Hz" + CR
		s4 = s4 + "End ν: " + (v4/10) + "Hz" + CR
		Button Sweep_freq Text s4
		
	'Read start and end sweep voltages
		COM WriteLine ":r68=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,6),5)
		COM WriteLine ":r69=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v4 = Left(Right(s3,6),5)
		s4 = "Start Vpp: " + (v3/1000) + "V" + CR
		s4 = s4 + "End Vpp: " + (v4/1000) + "V" + CR
		Button Sweep_Vpp Text s4
		
	'Read start and end sweep duty cycle
		COM WriteLine ":r70=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v3 = Left(Right(s3,6),5)
		COM WriteLine ":r71=0."
		Wait Seconds port_waiting_time
		s3 = COM_Received
		v4 = Left(Right(s3,6),5)
		s4 = "Start Duty: " + (v3/100) + "%" + CR
		s4 = s4 + "End Duty: " + (v4/100) + "%" + CR
		Button Sweep_Duty Text s4
		
		COM Close
		Wait Seconds 1
	EndIf
Next
Stop

'---------------------------------
'Button labels
'---------------------------------
Label PSG9080
	s5 = "Available COM Ports:" + CR
	s5 = s5 + COM_PortNames
	serial_port_number = Input s5
	COM Open serial_port_number 115200
	COM WriteLine ":r15=0."
	Wait Seconds port_waiting_time
	s1 = COM_Received
	If Left(s1,5) = ":r15="
		Print "Device PSG9080 succesfully recognized. Read test string: " + s1 + CR
		Button PSG9080 Text "PSG9080"
		Button PSG9080 Color GreenYellow
		Button PSG9080_2 Text "Function" + CR + "Generator"
		Button PSG9080_2 Color GreenYellow
		PSG9080_detected = 1
		For v1 = 2 To 16
			Button v1 Enabled
		Next
		For v1 = 18 To 67
			Button v1 Enabled
		Next
	Else
		Print "PSG9080 NOT RECOGNIZED" + CR
		Button PSG9080 Color DarkOrange
		Button PSG9080 Text "NOT" + CR + "Recognized"
		Wait Seconds 1
		Button PSG9080 Text "Init COM Port"
		Button PSG9080 Color Flashing DarkOrange Gold
		Button PSG9080_2 Text "PSG9080"
		Button PSG9080_2 Color DarkOrange
		PSG9080_detected = 0
		For v1 = 2 To 16
			Button v1 Disabled
		Next
		For v1 = 18 To 67
			Button v1 Disabled
		Next
	EndIf
	'COM Close
Return

Label start1
	If start1_flag = 0
		start1_flag = 1
		COM Open serial_port_number 115200
		COM WriteLine ":w10=" + start1_flag + "," + start2_flag + "."
		'COM Close
		Button start1 Text "Ch 1 ON"
		Button start1 Color GreenYellow
	Else
		start1_flag = 0
		COM Open serial_port_number 115200
		COM WriteLine ":w10=" + start1_flag + "," + start2_flag + "."
		'COM Close
		Button start1 Text "Ch 1 OFF"
		Button start1 Color Tomato
	EndIf
Return

Label start2
	If start2_flag = 0
		start2_flag = 1
		COM Open serial_port_number 115200
		COM WriteLine ":w10=" + start1_flag + "," + start2_flag + "."
		'COM Close
		Button start2 Text "Ch 2 ON"
		Button start2 Color GreenYellow
	Else
		start2_flag = 0
		COM Open serial_port_number 115200
		COM WriteLine ":w10=" + start1_flag + "," + start2_flag + "."
		'COM Close
		Button start2 Text "Ch 2 OFF"
		Button start2 Color Tomato
	EndIf
Return

Label waveform1
	s11 = "Choose the waveform type channel 1" + CR
	s11 = s11 + " 0: Sine" + CR
	s11 = s11 + " 1: Square" + CR
	s11 = s11 + " 2: Pulse" + CR
	s11 = s11 + " 3: Triangle" + CR
	s11 = s11 + " 4: Slope" + CR
	s11 = s11 + " 5: CMOS" + CR
	s11 = s11 + " 6: DC Level" + CR
	s11 = s11 + " 7: Partial sine wave" + CR
	s11 = s11 + " 8: Half wave" + CR
	s11 = s11 + " 9: Full wave" + CR
	s11 = s11 + "10: Positive ladder wave" + CR
	s11 = s11 + "11: Negative ladder wave" + CR
	s11 = s11 + "12: Positive trapezoidal wave" + CR
	s11 = s11 + "13: Negative trapezoidal wave" + CR
	s11 = s11 + "14: Noise Wave" + CR
	s11 = s11 + "15: Index rise" + CR
	s11 = s11 + "16: Index fall" + CR
	s11 = s11 + "17: Logarithmic rise" + CR
	s11 = s11 + "18: Logarithmic fall" + CR
	s11 = s11 + "19: Sinker Pulse" + CR
	s11 = s11 + "20: Multi-audio" + CR
	s11 = s11 + "21: Lorenz" + CR
	s11 = s11 + "101 - 199: AWG number"
	v11 = Input s11
	COM Open serial_port_number 115200
	COM WriteLine ":w11=" + v11 + "."
	Print "Ch1 Waveform set to: " + v11 + CR
	'COM Close
Return

Label waveform2
	s11 = "Choose the waveform type channel 2" + CR
	s11 = s11 + " 0: Sine" + CR
	s11 = s11 + " 1: Square" + CR
	s11 = s11 + " 2: Pulse" + CR
	s11 = s11 + " 3: Triangle" + CR
	s11 = s11 + " 4: Slope" + CR
	s11 = s11 + " 5: CMOS" + CR
	s11 = s11 + " 6: DC Level" + CR
	s11 = s11 + " 7: Partial sine wave" + CR
	s11 = s11 + " 8: Half wave" + CR
	s11 = s11 + " 9: Full wave" + CR
	s11 = s11 + "10: Positive ladder wave" + CR
	s11 = s11 + "11: Negative ladder wave" + CR
	s11 = s11 + "12: Positive trapezoidal wave" + CR
	s11 = s11 + "13: Negative trapezoidal wave" + CR
	s11 = s11 + "14: Noise Wave" + CR
	s11 = s11 + "15: Index rise" + CR
	s11 = s11 + "16: Index fall" + CR
	s11 = s11 + "17: Logarithmic rise" + CR
	s11 = s11 + "18: Logarithmic fall" + CR
	s11 = s11 + "19: Sinker Pulse" + CR
	s11 = s11 + "20: Multi-audio" + CR
	s11 = s11 + "21: Lorenz" + CR
	s11 = s11 + "101 - 199: AWG number"
	v11 = Input s11
	COM Open serial_port_number 115200
	COM WriteLine ":w12=" + v11 + "."
	Print "Ch2 Waveform set to: " + v11 + CR
	'COM Close
Return

Label Freq1
	s13 = "Choose Frequency Unit:" + CR
	s13 = s13 + "0:  Hz" + CR
	s13 = s13 + "1: KHz" + CR
	s13 = s13 + "2: MHz" + CR
	s13 = s13 + "3: mHz" + CR
	s13 = s13 + "4: µHz" + CR
	v13 = Input s13
	v14 = Input "Set Frequency value for Ch1"
	Select v13
		Case 0
			v12 = v14 *1000
			s12 = "Hz"
		Case 1
			v12 = v14 *1000000
			s14 = "KHz"
		Case 2
			v12 = v14 *1000000000
			s14 = "MHz"
		Case 3
			v12 = v14 *1000
			s14 = "mHz"
		Case 4
			v12 = v14 *1000
			s14 = "µHz"
	EndSelect
	COM Open serial_port_number 115200
	COM WriteLine ":w13=" + v12 + "," + v13 + "."
	Print "Ch1 Frequency set to: " + v14 + s14 + CR
	'COM Close
Return

Label Freq2
	s13 = "Choose Frequency Unit:" + CR
	s13 = s13 + "0:  Hz" + CR
	s13 = s13 + "1: KHz" + CR
	s13 = s13 + "2: MHz" + CR
	s13 = s13 + "3: mHz" + CR
	s13 = s13 + "4: µHz" + CR
	v13 = Input s13
	v14 = Input "Set Frequency value for Ch2"
	Select v13
		Case 0
			v12 = v14 *1000
			s12 = "Hz"
		Case 1
			v12 = v14 *1000000
			s14 = "KHz"
		Case 2
			v12 = v14 *1000000000
			s14 = "MHz"
		Case 3
			v12 = v14 *1000
			s14 = "mHz"
		Case 4
			v12 = v14 *1000
			s14 = "µHz"
	EndSelect
	COM Open serial_port_number 115200
	COM WriteLine ":w14=" + v12 + "," + v13 + "."
	Print "Ch2 Frequency set to: " + v14 + s14 + CR
	'COM Close
Return

Label Amplitude1
	s15 = "Choose the wave amplitude in V: " + CR
	s15 = s15 + "(0.001 - 25.000V)"
	v15 = Input s15
	COM Open serial_port_number 115200
	COM WriteLine ":w15=" + v15*1000 + "."
	'COM Close
	Print "Ch1 Amplitude set to: " + v15 + "V" + CR
Return

Label Amplitude2
	s15 = "Choose the wave amplitude in V: " + CR
	s15 = s15 + "(0.001 - 25.000V)"
	v15 = Input s15
	COM Open serial_port_number 115200
	COM WriteLine ":w16=" + v15*1000 + "."
	'COM Close
	Print "Ch2 Amplitude set to: " + v15 + "V" + CR
Return

Label Offset1
	s17 = "Choose the offset for Ch1 (V):"
	v17 = Input s17
	COM Open serial_port_number 115200
	COM WriteLine ":w17=" + (v17*100+1000) + "."
	'COM Close
	Print "Ch1 Offset set to: " + v17 + "V" + CR
Return

Label Offset2
	s17 = "Choose the offset for Ch2 (V):"
	v17 = Input s17
	COM Open serial_port_number 115200
	COM WriteLine ":w18=" + (v17*100+1000) + "."
	'COM Close
	Print "Ch2 Offset set to: " + v17 + "V" + CR
Return

Label Duty1
	s19 = "Choose the duty cycle % for Ch1:"
	v19 = Input s19
	COM Open serial_port_number 115200
	COM WriteLine ":w19=" + (v19*100) + "."
	'COM Close
	Print "Ch1 Duty Cycle % set to: " + v19 + "%" + CR
Return

Label Duty2
	s19 = "Choose the duty cycle % for Ch2:"
	v19 = Input s19
	COM Open serial_port_number 115200
	COM WriteLine ":w20=" + (v19*100) + "."
	'COM Close
	Print "Ch2 Duty Cycle % set to: " + v19 + "%" + CR
Return

Label Phase1
	s21 = "Set Phase angle of Ch1 (0-359.99°):"
	v21 = Input s21
	COM Open serial_port_number 115200
	COM WriteLine ":w21=" + (v21*100) + "."
	'COM Close
	Print "Ch1 Phase angle set to: " + v21 + "°" + CR
Return

Label Phase2
	s21 = "Set Phase angle of Ch2 (0-359.99°):"
	v21 = Input s21
	COM Open serial_port_number 115200
	COM WriteLine ":w22=" + (v21*100) + "."
	'COM Close
	Print "Ch2 Phase angle set to: " + v21 + "°" + CR
Return

Label Brightness
	s28 = "Set the brightness of the display in %:"
	v28 = Input s28
	COM Open serial_port_number 115200
	COM WriteLine ":w28=" + v28 + "."
	'COM Close
	Print "Screen brightness set to: " + v28 + "%" + CR
Return

Label Modulation_Select
	s40 = "Ch1, Set the modulation type:" + CR
	s40 = s40 + "0: AM" + CR
	s40 = s40 + "1: FM" + CR
	s40 = s40 + "2: PM" + CR
	s40 = s40 + "3: ASK" + CR
	s40 = s40 + "4: FSK" + CR
	s40 = s40 + "5: PSK" + CR
	s40 = s40 + "6: PWM" + CR
	s40 = s40 + "7: Burst" + CR
	v40 = Input s40
	s40 = "Ch2, Set the modulation type:" + CR
	s40 = s40 + "0: AM" + CR
	s40 = s40 + "1: FM" + CR
	s40 = s40 + "2: PM" + CR
	s40 = s40 + "3: ASK" + CR
	s40 = s40 + "4: FSK" + CR
	s40 = s40 + "5: PSK" + CR
	s40 = s40 + "6: PWM" + CR
	s40 = s40 + "7: Burst" + CR
	v28 = Input s40
	COM Open serial_port_number 115200
	COM WriteLine ":w40=" + v40 + "," + v28 + "."
	'COM Close
	Print "Modulation type set to: " + v40 + "for Ch1 and " + v28 + " for Ch2" + CR
Return

Label Trigger1
	Button Trigger1 Color Chartreuse
	COM Open serial_port_number 115200
	COM WriteLine ":w74=" + 1 + "," + 0 + "."
	'COM Close
	Wait Seconds 0.1
	Button Trigger1 Color Red1
Return

Label Trigger2
	Button Trigger2 Color Chartreuse
	COM Open serial_port_number 115200
	COM WriteLine ":w74=" + 0 + "," + 1 + "."
	'COM Close
	Wait Seconds 0.1
	Button Trigger2 Color Red1
Return

Label PWM_set1
	s53 = "Set Ch1 PWM Pulse width in µs" + CR
	s53 = s53 + "(0.001µs - 4000 000µs):"
	v53 = Input s53
	s53 = "Set Ch1 PWM Period lenght in µs" + CR
	s53 = s53 + "(0.01µs - 40 000 000µs)"
	v55 = Input s53
	COM Open serial_port_number 115200
	COM WriteLine ":w53=" + (v53*1000) + "."
	Wait Seconds port_waiting_time
	COM WriteLine ":w55=" + (v55*100) + "."
	'COM Close
	Print "Ch1 PWM pulse width set to: " + v53 + "µs, PWM width set to: " + v55 + "µs" + CR
Return

Label PWM_set2
	s53 = "Set Ch2 PWM Pulse width in µs" + CR
	s53 = s53 + "(0.001µs - 4000 000µs):"
	v53 = Input s53
	s53 = "Set Ch2 PWM Period lenght in µs" + CR
	s53 = s53 + "(0.01µs - 40 000 000µs)"
	v55 = Input s53
	COM Open serial_port_number 115200
	COM WriteLine ":w54=" + (v53*1000) + "."
	Wait Seconds port_waiting_time
	COM WriteLine ":w56=" + (v55*100) + "."
	'COM Close
	Print "Ch2 PWM pulse width set to: " + v53 + "µs, PWM width set to: " + v55 + "µs" + CR
Return

Label PWM_Inversion
	s57 = "Ch1 Set if PWM signal is inverted" + CR
	s57 = s57 + "0: Normal" + CR
	s57 = s57 + "1: Inverted" + CR
	v57 = Input s57
	s57 = "Ch2 Set if PWM signal is inverted" + CR
	s57 = s57 + "0: Normal" + CR
	s57 = s57 + "1: Inverted" + CR
	v28 = Input s57
	COM Open serial_port_number 115200
	COM WriteLine ":w57=" + v57 + "," + v28 + "."
	'COM Close
	Print "PWM for Ch1 inversion set to " + v57 + ", PWM for Ch2 set to " + v28 + CR
Return

Label Burst_idle
	s58 = "Ch1 Select idle settings for burst:" + CR
	s58 = s58 + "0: zero" + CR
	s58 = s58 + "1: positive maximum" + CR
	s58 = s58 + "2: negative maximum" + CR
	v58 = Input s58
	s58 = "Ch2 Select idle settings for burst:" + CR
	s58 = s58 + "0: zero" + CR
	s58 = s58 + "1: positive maximum" + CR
	s58 = s58 + "2: negative maximum" + CR
	v28 = Input s58
	COM Open serial_port_number 115200
	COM WriteLine ":w58=" + v58 + "," + v28 + "."
	'COM Close
	Print "Ch1 Burst idle status set to " + v58 + ",Ch2 idle status set to " + v28 + CR
Return

Label Polarity
	s59 = "Ch1 set polarity" + CR
	s59 = s59 + "0: Positive" + CR
	s59 = s59 + "1: Negative" + CR
	v59 = Input s59
	s59 = "Ch2 set polarity" + CR
	s59 = s59 + "0: Positive" + CR
	s59 = s59 + "1: Negative" + CR
	v28 = Input s59
	COM Open serial_port_number 115200
	COM WriteLine ":w59=" + v59 + "," + v28 + "."
	'COM Close
	Print "Ch1 polarity set to: " + v59 + ", Ch2 polarity set to: " + v28 + CR
Return

Label Burst_number
	s61 = "Ch1 Burst pulse number: " + CR
	s61 = s61 + "(0 - 1 000 000 000)"
	v61 = Input s61
	s61 = "Ch2 Burst pulse number: " + CR
	s61 = s61 + "(0 - 1 000 000 000)"
	v28 = Input s61
	COM Open serial_port_number 115200
	COM WriteLine ":w61=" + v61 + "," + v28 + "."
	'COM Close
	Print "Ch1 Burst pulse number set to " + v61 + ", Ch2 Burst pulse number set to " + v28 + CR
Return

Label Sweep_set
	s64 = "Set Sweep Channel: " + CR
	s64 = s64 + "0: Ch1" + CR
	s64 = s64 + "1: Ch2"
	v64 = Input s64
	s64 = "Set Sweep time in s" + CR
	s64 = s64 + "(0.01 - 640s)"
	v10 = Input s64
	s64 = "Set Sweep direction" + CR
	s64 = s64 + "0: Increasing →" + CR
	s64 = s64 + "1: Decreasing ←" + CR
	s64 = s64 + "2: Back and forth →←" + CR
	v11 = Input s64
	s64 = "Set sweep mode: " + CR
	s64 = s64 + "0: Linear" + CR
	s64 = s64 + "1: Logarithmic"
	v12 = Input s64
	COM Open serial_port_number 115200
	COM WriteLine ":w64=" + v64 + "," + (v10*100) + "," + v11 + "," + v12 + "."
	'COM Close
	s64 = "Sweep set with following parameters:" + CR
	s64 = s64 + "Channel "  + (v64+1) + CR
	s64 = s64 + "Sweep time: " + v10 + "s" + CR
	s64 = s64 + "Sweep direction: " + v11 + CR
	s64 = s64 + "Sweep mode: " + v12 + CR
	Print s64
Return

Label Sweep_VControl
	If sweep_freq_on = 0
		sweep_freq_on = 1
		COM Open serial_port_number 115200
		COM WriteLine ":w65=" + sweep_freq_on + "," + 1 + "."
		'COM Close
		Button Sweep_set Color GreenYellow
		Button Sweep_VControl Color GreenYellow
		Print "Frequency sweep ON" + CR
	Else
		sweep_freq_on = 0
		COM Open serial_port_number 115200
		COM WriteLine ":w65=" + sweep_freq_on + "," + 1 + "."
		'COM Close
		Button Sweep_set Color Tomato
		Button Sweep_VControl Color Tomato
		Print "Frequency sweep OFF" + CR
		
		's65 = "Start Frequency sweep?" + CR
		's65 = s65 + "0: OFF" + CR
		's65 = s65 + "1: ON "
		'v65 = Input s65
		's65 = "Start Voltage Control?" + CR
		's65 = s65 + "0: OFF" + CR
		's65 = s65 + "1: ON "
		'v10 = Input s65
		'COM Open serial_port_number 115200
		'COM WriteLine ":w65=" + v65 + "," + v10 + "."
		'COM Close
	EndIf
Return

Label Sweep_freq
	s66 = "Sweep Start frequency (Hz)"
	v66 = Input s66
	s67 = "Sweep End frequency (Hz)"
	v67 = Input s67
	COM Open serial_port_number 115200
	COM WriteLine ":w66=" + (v66*10) + "."
	Wait Seconds port_waiting_time
	COM WriteLine ":w67=" + (v67*10) + "."
	'COM Close
	Print "Sweep start frequency set to: " + v66 + "Hz" + CR
	Print "Sweep end frequency set to: " + v67 + "Hz" + CR
Return

Label Sweep_Vpp
	s68 = "Sweep Start Voltage (V)"
	v68 = Input s68
	s69 = "Sweep End Voltage (V)"
	v69 = Input s69
	COM Open serial_port_number 115200
	COM WriteLine ":w68=" + (v68*1000) + "."
	Wait Seconds port_waiting_time
	COM WriteLine ":w69=" + (v69*1000) + "."
	'COM Close
	Print "Sweep start voltage set to: " + v68 + "V" + CR
	Print "Sweep end voltage set to: " + v69 + "V" + CR
Return

Label Sweep_Duty
	s70 = "Sweep Start Duty Cycle (%)" + CR
	s70 = s70 + "(0 - 100%)"
	v70 = Input s70
	s71 = "Sweep End Duty Cycle (%)" + CR
	s71 = s71 + "(0 - 100%)"
	v71 = Input s71
	COM Open serial_port_number 115200
	COM WriteLine ":w70=" + (v70*100) + "."
	Wait Seconds port_waiting_time
	COM WriteLine ":w71=" + (v71*100) + "."
	'COM Close
	Print "Sweep start Duty Cycle (%): " + v70 + "%" + CR
	Print "Sweep end Duty Cycle (%): " + v71 + "%" + CR
Return

Label Set_Sweep_Interface
	COM Open serial_port_number 115200
	COM WriteLine ":w24=0,6,0,1."
	'COM Close
Return

Label Set_Modulation_interface
	COM Open serial_port_number 115200
	COM WriteLine ":w24=0,1,7,7."
	Wait Seconds port_waiting_time
	COM WriteLine ":w24=0,2,7,7."
	'COM Close
Return

Label Set_Waveform_Interface
	COM Open serial_port_number 115200
	COM WriteLine ":w24=0,1,0,0."
	Wait Seconds port_waiting_time
	COM WriteLine ":w24=0,2,0,0."
	'COM Close
Return

Label Set_VCO_Interface
	COM Open serial_port_number 115200
	COM WriteLine ":w24=0,7,0,1."
	'COM Close
Return

Label Set_Measurement_Interface
	COM Open serial_port_number 115200
	COM WriteLine ":w24=0,4,0,1."
	'COM Close
Return

Label Load_mem
	s1 = "Insert the memory location from which you want to LOAD parameters" + CR
	s1 = s1 + "(0 - 99)"
	v10 = Input s1
	COM Open serial_port_number 115200
	COM WriteLine ":w26=" + v10 + ",111."
Return

Label Save_mem
	s1 = "Insert the memory location in which you want to SAVE parameters" + CR
	s1 = s1 + "(0 - 99)"
	v10 = Input s1
	COM Open serial_port_number 115200
	COM WriteLine ":w26=" + v10 + ",222."
Return

Label Clear_mem
	s1 = "Insert the memory location you want to CLEAR" + CR
	s1 = s1 + "(0 - 99)"
	v10 = Input s1
	COM Open serial_port_number 115200
	COM WriteLine ":w26=" + v10 + ",333."
Return

Label Read_AWG
	If FileExists("Apps\PSG9080_ARB.exe")
		v10 = Input "Type the AW slot number to read from device (1 - 99)"
		COM Close 'Close COM Port otherwise PSG9080_ARB.exe cannot access to it
		Load "C:\Windows\System32\cmd.exe"
		s1 = GetApplicationPath
		SendKeys cd
		PressKeys Space
		SendKeys s1
		SendKeys \Apps\
		PressKeys Enter
		SendKeys PSG9080_ARB.exe
		PressKeys Space
		SendKeys COM
		SendKeys serial_port_number
		PressKeys Space
		SendKeys -read
		PressKeys Space
		SendKeys v10
		PressKeys Space
		SendKeys wave
		v11 = v10 + 100
		SendKeys v11
		SendKeys .txt{ENTER}
		Wait Seconds 1
		Print "Waveform " + v10 + " saved to \Apps\wave" + v11 + ".txt" + CR
	Else
		Print "File not found: Apps\PSG9080_ARB.exe" + CR
		s2 = "Please download PSG9080_ARB.exe from" + CR
		s2 = s2 + "https://github.com/qrp73/PSG9080_ARB/releases/download/1.0/PSG9080_ARB-v1.0.zip" + CR
		s2 = s2 + "and copy it in the Apps folder" + CR
		s2 = s2 + "under the Theremino Application folder."
		s1 = Message s2
		Button Read_AWG Color Coral
		Button Read_AWG Text "PSG9080_ARB.exe" + CR + "Not found"
		Wait Seconds 1.5
		Button Read_AWG Color LemonChiffon
		Button Read_AWG Text "Read Arbitrary" + CR + "Waveform"
	EndIf
	
Return

Label Write_AWG
	If FileExists("Apps\PSG9080_ARB.exe")
		s2 = "Type the AW slot number in which you want to" + CR
		s2 = s2 + "upload waveform to the device (1 - 99)"
		v10 = Input s2
		s2 = SelectFile("Apps\") 'Let user select a file containing the waveform to upload on the signal generator
		COM Close 'Close COM Port otherwise PSG9080_ARB.exe cannot access to it
		Load "C:\Windows\System32\cmd.exe"
		'Go to the Apps folder
		s1 = GetApplicationPath
		SendKeys cd
		PressKeys Space
		SendKeys s1
		SendKeys \Apps\
		PressKeys Enter
		'Send command to upload waveform to the signal generator
		SendKeys PSG9080_ARB.exe
		PressKeys Space
		SendKeys COM
		SendKeys serial_port_number
		PressKeys Space
		SendKeys -write
		PressKeys Space
		SendKeys v10
		PressKeys Space
		SendKeys s2
		SendKeys {ENTER}
		Button Write_AWG Color Chartreuse
		Button Write_AWG Text "Upload" + CR + "Success"
		Wait Seconds 2
		Print "If you heard a beep, Waveform was succesfully uploaded on the device" + CR
		Button Write_AWG Color LemonChiffon
		Button Write_AWG Text "Write Arbitrary" + CR + "Waveform"
		
	'Writing bat file (not working)
		's1 = "@echo off" + CR
		's1 = s1 + "title LOAD Arbitraty waveform to PSG9080" + CR
		's1 = s1 + "echo Writing Arbitrary waveform to PSG9080." + CR
		's1 = s1 + "pause" + CR
		's1 = s1 + "PSG9080_ARB.exe COM" + serial_port_number + " -write " + v10 + " " + s2 + CR
		's1 = s1 + "pause"
		'Save DeleteFile "..\Apps\Load_AWG.bat"
		'Save StringToFile s1 "Apps\Load_AWG.bat"
		'Load "Apps\Load_AWG.bat"
	Else
		Print "File not found: Apps\PSG9080_ARB.exe" + CR
		s2 = "Please download PSG9080_ARB.exe from" + CR
		s2 = s2 + "https://github.com/qrp73/PSG9080_ARB/releases/download/1.0/PSG9080_ARB-v1.0.zip" + CR
		s2 = s2 + "and copy it in the Apps folder" + CR
		s2 = s2 + "under the Theremino Automation folder."
		s1 = Message s2
		Button Write_AWG Color Coral
		Button Write_AWG Text "PSG9080_ARB.exe" + CR + "Not found"
		Wait Seconds 1.5
		Button Write_AWG Color LemonChiffon
		Button Write_AWG Text "Write Arbitrary" + CR + "Waveform"
	EndIf
Return

Label Create_Example_AW
	Save DeleteFile "..\Apps\wave_example.txt"
	s1 = ""
	For v3 = 1 To 1000
		s1 = s1 + 16383 + CRLF
	Next
	For v3 = 1001 To 2000
		s1 = s1 + 8191 + CRLF
	Next
	For v3 = 2001 To 8192
		s1 = s1 + 0 + CRLF
	Next
	Save StringToFile s1 "Apps\wave_example.txt"
	s1 = Message "A file named wave_example.txt was created in the Apps folder."
	Print "A file named wave_example.txt was created in the Apps folder." + CR
Return

Label EventStop
	Window Maximized
	COM Close
End


'EXEC v1 = Rnd : V2 = V1 + 99 : V3 = V2 + 1000
