'Programma per controllare una scheda ZXY6005s oppure ZXY6020s
'UNILABS EXPERIMENTS 2020
'Emanuele F. Marano
'Version 2.05
'26/08/2020
'Set Speed to 7 or higher

'Dichiarazione variabili globali
Variable Numeric Tempo_Attesa
Variable Numeric Volt
Variable Numeric Ampere
Variable Numeric Temperatura
Variable Numeric porta_seriale
Variable String Address
Variable String Stato_seriale
Variable Numeric Button_VSET
Variable Numeric Button_ISET
Variable Numeric Button_VOUT
Variable Numeric Button_IOUT
Variable Numeric Button_Temperature
Variable Numeric Button_CCCV
Variable Numeric Button_Status
Variable Numeric Button_Power
Variable Numeric Button_Resistance

'Assegnazione variabili globali
Tempo_Attesa = 0.10
porta_seriale = -1
Address = "ABC"
Stato_seriale = "NULL"
Button_VSET = 6
Button_ISET = 7
Button_Status = 5
Button_VOUT = 8
Button_IOUT = 9
Button_CCCV = 12
Button_Temperature = 13
Button_Power = 10
Button_Resistance = 11

'Dichiarazione bottoni
Button 1 Text Inizializza seriale
Button 1 Color DarkOrange
Button 2 Text Avvia Applicazioni
Button 2 Color LightBlue
Button 3 Text Set0 'Spegne l'output e imposta corrente e tensione a 0V
Button 3 Color Yellow
Button 4 Text Start VRamp
Button 4 Color Yellow
Button Button_VSET Text VSET:
Button Button_VSET Color Salmon
Button Button_ISET Text ISET:
Button Button_ISET Color Salmon
Button Button_Temperature Text "Temperature"
Button Button_Temperature Color Aquamarine
Button Button_Status Text OUT
Button Button_Status Color Red
Button Button_VOUT Text VOUT
Button Button_VOUT Color Red
Button Button_IOUT Text IOUT
Button Button_IOUT Color Red
Button Button_CCCV Text CC-CV
Button Button_CCCV Color LightBlue
Button Button_Power Text Power
Button Button_Power Color Red
Button Button_Resistance Text Resistance
Button Button_Resistance Color Red

Controls OpenTextBox
Print "Available serial ports:" + CRLF
Print COM_PortNames + CRLF
'Inizio Ciclo FOR di lettura principale
Label Ciclo_principale
	Select Stato_seriale
		Case "NULL"
			Print "Please open COM port first" + CRLF
			Print "Press Inizializza seriale" + CRLF
			Wait Seconds 0.2
			Goto Stopped
		Case "CLOSED"
			Print "COM Port Closed" + CRLF
			Print "Please open COM port first" + CRLF
			Print "Press Inizializza seriale" + CRLF
			Wait Seconds 0.2
			Goto Stopped
		Case "OPENED"
			Print "Porta seriale " + porta_seriale + " aperta" + CRLF
			For v9 = 1 To 9e99
				COM WriteString Address + "ru" + LF	'Lettura tensione OUT
				Wait Seconds Tempo_Attesa
				v3 = Right(COM_Received,4)
				Slot(102) = v3/100
				Button Button_VOUT Text "VOUT: " + v3/100 + "V"
				Button Button_VOUT Color Lime
				COM WriteString Address + "ri" + LF	'Lettura corrente OUT
				Wait Seconds Tempo_Attesa
				v3 = Right(COM_Received,4)
				Slot(103) = v3/1000
				Button Button_IOUT Text "IOUT: " + v3/1000 + "A"
				Button Button_IOUT Color Lime
				COM WriteString Address + "rc" + LF	'Lettura Control State
				Wait Seconds Tempo_Attesa
				Button Button_Power Text "Power: " + Format(Slot(102)*Slot(103),"0.000") + "W"
				Button Button_Resistance Text "R: " + Format(Slot(102)/Slot(103),"0.000") + "Ohm"
				v3 = Right(COM_Received,1)
				Slot(104) = v3
				If v3 = 1
					Button Button_CCCV Text CC
				Else
					Button Button_CCCV Text CV
				EndIf
				COM WriteString Address + "rt" + LF	'Lettura temperatura
				Wait Seconds Tempo_Attesa
				Slot(105) = Right(COM_Received,3)
				Button Button_Temperature Text "Temperature" + CRLF + Slot(105) + "°C"
				COM WriteString Address + "ro" + LF	'Lettura Stato output
				Wait Seconds Tempo_Attesa
				v3 = Right(COM_Received,1)
				Slot(106) = v3
				If v3 = 1
					Button Button_Status Text OUT ON
					Button Button_Status Color GreenYellow
				Else
					If v3 = 0
						Button Button_Status Text OUT OFF
						Button Button_Status Color Red
					Else
						Print "Wrong Status Received" + CRLF
					EndIf
				EndIf
				Wait Seconds 1
				Stato_seriale = COM_Status
			Next
		CaseElse
			Print "Error"
		Goto Stopped
EndSelect
Goto Ciclo_principale

Label Stopped 'QUI IL PROGRAMMA VA IN CRASH
	Wait Seconds 0.2
Stop

'Inizializza la porta seriale
Label Inizializza seriale
	If COM_Status = "CLOSED"
		If porta_seriale = -1 Or Address = "ABC"
			porta_seriale = Input ("Scrivi il numero della porta seriale da aprire")
			Address = Input "Indirizzo dispositivo (A, B...)"
		EndIf
		COM Open porta_seriale 9600
		If COM_Status = "OPENED"
			Controls ClearTextBox
			Print "Porta " + porta_seriale + " Aperta" + CRLF
			Button 1 Color PaleGreen
			COM WriteString Address + "a" + LF
			Wait Seconds Tempo_Attesa
			s1 = COM_Received
			Print s1 + CRLF
			If Left(s1,3) = "ZXY"
				Print "Dispositivo Power Supply ZXY60XXs Trovato!" + CRLF
				Button 1 Color GreenYellow
			Else
				Print "Dispositivo ZXY60XXs non trovato!" + CRLF
				Button 1 Color Red
				Return
			EndIf
			COM WriteString Address + "v" + LF
			Wait Seconds Tempo_Attesa
			Print COM_Received + CRLF
			COM WriteString Address + "rt" + LF
			Wait Seconds Tempo_Attesa
			s2 = COM_Received
			Button Button_Temperature Text "Temperature" + CRLF + Right(s2,3) + "°C"
			Button Button_Power Color Green3
			Button Button_Resistance Color Green3
		Else
			Print "Unable to open Specified COM Port" + CRLF
		EndIf
	Else
		COM Close
		Print "Porta " + porta_seriale + " Chiusa" + CRLF
		Button 1 Color DarkOrange
	EndIf
	Stato_seriale = COM_Status
Return

'Avvia le altre applicazioni per monitorare
Label Avvia Applicazioni
	'Load Theremino_HAL\Theremino_HAL.exe
	'Load Theremino_ArduHAL\Theremino_ArduHAL.exe
	Load Theremino_HAL\Theremino_SlotViewer.exe
	'Load Theremino_Buttons\Theremino_Buttons.exe
	Load Theremino_SignalScope\Theremino_SignalScope.exe
	Print "Applicazioni aperte" + CRLF
	Button 2 Color DarkOrange
Return

'Spegne l'uscita dell'alimentatore e imposta a 0 tensione e corrente
Label Set0
	If COM_Status = "CLOSED"
		Print "La porta COM è chiusa"
		Return
	Else
		COM WriteString Address + "so0" + LF	'Spegne l'output
		Wait Seconds Tempo_Attesa
		COM WriteString Address + "su00000" + LF	'Imposta 0V
		Wait Seconds Tempo_Attesa
		COM WriteString Address + "si0000" + LF	'Imposta 0A
		Button Button_VSET Text "VSET: 0V"
		Button Button_ISET Text "ISET: 0A"
	EndIf
Return

'Inizia una rampa di tensione e salva su file i dati
Label Start VRamp
	If COM_Status = "CLOSED"
		Print "La porta COM è chiusa" + CRLF
		Return
	Else
		Variable Numeric VMax
		Variable Numeric VStep
		Variable Numeric TimeStep
		Variable Numeric IMax
		Variable Numeric Time
		Variable Numeric Initial_Time
		Variable Numeric CC_Status
		VMax = Input "Insert VMax (V)"
		VStep = Input "Insert VStep (V)"
		TimeStep = Input "Insert TimeStep (s)"
		IMax = Input "Insert IMax (A)"
		Time = 0
		Initial_Time = 0
		v2 = Message "Confirm starting VRamp?"	'Messaggio di conferma
		If v2 = 0
			Return
		EndIf
			'Cancellazione vecchi files se presenti
		Print "Old data file will be deleted" + CRLF
		Wait Seconds 1
		Variable String File_Time
		Variable String File_VSET
		Variable String File_ISET
		Variable String File_VOUT
		Variable String File_IOUT
		Variable String File_Temperature
		Variable String File_CCSTatus
		
		File_Time = "ZXY_" + Address + "_Time.txt"
		File_VSET = "ZXY_" + Address + "_VSET.txt"
		File_ISET = "ZXY_" + Address + "_ISET.txt"
		File_VOUT = "ZXY_" + Address + "_VOUT.txt"
		File_IOUT = "ZXY_" + Address + "_IOUT.txt"
		File_Temperature = "ZXY_Data\ZXY_" + Address + "_Temperature.txt"
		File_CCSTatus = "ZXY_Data\ZXY_" + Address + "_CCStatus.txt"
		
		Save DeleteFile File_Time
		Save DeleteFile File_VSET
		Save DeleteFile File_ISET
		Save DeleteFile File_VOUT
		Save DeleteFile File_IOUT
		Save DeleteFile File_Temperature
		Save DeleteFile File_CCSTatus
		Print "Data Files deleted" + CRLF
		
			'Scrittura intestazione dei file di logging
		Save StringToFile "Time (s)" File_Time
		Save StringToFile "VSET (V)" File_VSET
		Save StringToFile "ISET (V)" File_ISET
		Save StringToFile "VOUT (V)" File_VOUT
		Save StringToFile "IOUT (A)" File_IOUT
		Save StringToFile "Temperature (°C)" File_Temperature
		Save StringToFile  "CC=0 CV=1" File_CCSTatus
		Print "Files headings written" + CRLF
		
		Print "Ramping..." + CRLF
		Button 4 Color Green
		Button 4 Text "Ramping..."
		
		COM WriteString Address + "su00000" + LF
		Wait Seconds Tempo_Attesa
		Button Button_VSET Text "VSET: " + "0.00V"
		Button Button_VSET Color GreenYellow
		Slot(100) = 0
		COM WriteString Address + "si" + Format(IMax*100,"0000") + LF	'Imposta il massimo valore di corrente da raggiungere
		Wait Seconds Tempo_Attesa
		Button Button_ISET Text "ISET: " + IMax + "A"
		Button Button_ISET Color GreenYellow
		Slot(101) = IMax
		COM WriteString Address + "so1" + LF	'Accende l'output dell'alimentatore
		Wait Seconds Tempo_Attesa
		COM WriteString Address + "ro" + LF
		Wait Seconds Tempo_Attesa
		v2 = -1
		v2 = Right(COM_Received,1)
		If v2 = 1
			Print "Power supply Output ON" + CRLF
			Button Button_Status Text OUT ON
			Button Button_Status Color LawnGreen
			Slot(106) = 1
		Else
			If v2 = 0
				Print "Power supply Output OFF" + CRLF
				Button Button_Status Text OUT OFF
				Button Button_Status Color Red
				Slot(106) = 0
			Else
				Print "Wrong data received" + CRLF
			EndIf
		EndIf
		
		Initial_Time = ElapsedTime	'Memorizza il tempo iniziale
		
			'Inizio ciclo for della rampa
		For v1 = 0 To VMax Step VStep	'Impostazione dei parametri della rampa di tensione
			COM WriteString Address + "su" + Format(v1*100, "00000") + LF
			Wait Seconds Tempo_Attesa
			Slot(100) = v1
			
			Wait Seconds TimeStep	'Attesa dello step
			
			COM WriteString Address + "rt" + LF
			Wait Seconds Tempo_Attesa
			Temperatura = Right(COM_Received,3)
			Button Button_Temperature Text "Temperature" + CRLF + Temperatura + "°C"
			COM WriteString Address + "ru" + LF
			Wait Seconds Tempo_Attesa
			Volt = Right(COM_Received,5)
			Volt = Volt/100
			COM WriteString Address + "ri" + LF
			Wait Seconds Tempo_Attesa
			Ampere = Right(COM_Received,4)
			Ampere = Ampere/1000
			Save StringToFile ElapsedTime - Initial_Time File_Time
			Save StringToFile Volt File_VOUT
			Save StringToFile Ampere File_IOUT
			Save StringToFile Temperatura File_Temperature
			Save StringToFile v1 File_VSET
			Save StringToFile IMax File_ISET
			COM WriteString Address + "rc" + LF
			Wait Seconds Tempo_Attesa
			CC_Status = Right(COM_Received,1)
			Save StringToFile CC_Status File_CCSTatus
			Slot(102) = Volt
			Slot(103) = Ampere
			Slot(104) = CC_Status
			Slot(105) = Temperatura
			Button Button_VOUT Text "VOUT: " + Volt + "V"
			Button Button_IOUT Text "IOUT: " + Ampere + "A"
			Button Button_VSET Text "VSET: " + v1 + "V"
			Button Button_Power Text "Power: " + Format(Volt*Ampere,"0.000") + "W"
			Button Button_Resistance Text "R: " + Format(Volt/Ampere,"0.000") + "Ohm"
			If CC_Status = 0
				Button Button_CCCV Text "CC"
			Else
				Button Button_CCCV Text "CV"
			EndIf
			
		Next
		Print "Voltage Ramp Finished" + CRLF
		COM WriteString Address + "so0" + LF
		Wait Seconds Tempo_Attesa
		COM WriteString Address + "ro" + LF
		Wait Seconds Tempo_Attesa
		v2 = -1
		v2 = Right(COM_Received,1)
		If v2 = 0
			Print "OUTPUT OFF" + CRLF
			Button Button_Status Text OUT OFF
			Button Button_Status Color Red
			Button 4 Color Yellow
			Button 4 Text Start VRamp
			Slot(106) = 0
		Else
			If v2 = 1
				Print "Warning!! OUTPUT Still ON!" + CRLF
				Slot(106) = 1
				Button Button_Status Text OUT ON
				Button Button_Status Color GreenYellow
				Button 4 Color Yellow
				Button 4 Text Start VRamp
			Else
				Print "Wrong Data2 Received" + CRLF
			EndIf
		EndIf
	EndIf
Return

'Legge la temperatura dell'alimentatore
Label Temperature
	If COM_Status = "CLOSED"
		Print "La porta COM è chiusa"
		Return
	EndIf
	COM WriteString Address + "rt" + LF
	Wait Seconds Tempo_Attesa
	v4 = Right(COM_Received,3)
	Button 5 Text "Temperature: " + CRLF + v4 + "°C"
	Select v4
		Case v4<20
			Button 5 Color Aqua
		Case v4>=20 And v4<30
			Button 5 Color GreenYellow
		Case v4>=30 And v4<40
			Button 5 Color Yellow
		Case v4>=40 And v4<45
			Button 5 Color Orange
		Case v4>=45 And v4<50
			Button 5 Color OrangeRed
		Case v4>50
			Button 5 Color Red
			Beep
	EndSelect
	
Return

Label OUT
	If COM_Status = "CLOSED"
		Print "La porta COM è chiusa"
		Return
	EndIf
	COM WriteString Address + "ro" + LF
	Wait Seconds Tempo_Attesa
	v3 = Right(COM_Received,1)
	If v3 = 1
		COM WriteString Address + "so0"
		Wait Seconds Tempo_Attesa
	Else
		COM WriteString Address + "so1"
		Wait Seconds Tempo_Attesa
	EndIf
	Return
Label Power
	If COM_Status = "CLOSED"
		Print "La porta COM è chiusa"
		Return
	Else
		If COM_Status = "OPENED"
			COM WriteString Address + "ru" + LF
			Wait Seconds Tempo_Attesa
			v2 = Right(COM_Received,5)
			COM WriteString Address + "ri" + LF
			Wait Seconds Tempo_Attesa
			v4 = Right(COM_Received,4)
			Button Button_Power Text "Power: " + v2*v4/10000 + "W"
			Button Button_Power Color Green3
		EndIf
	EndIf
	Return
Label Resistance
	If COM_Status = "CLOSED"
		Print "La porta COM è chiusa"
		Return
	Else
		If COM_Status = "OPENED"
			COM WriteString Address + "ru" + LF
			Wait Seconds Tempo_Attesa
			v2 = Right(COM_Received,5)
			COM WriteString Address + "ri" + LF
			Wait Seconds Tempo_Attesa
			v4 = Right(COM_Received,4)
			Button Button_Resistance Text "R: " + v2/v4 + "Ohm"
			Button Button_Resistance Color Green3
		EndIf
	EndIf
Return

Label VSET:
	If COM_Status = "CLOSED"
		Print "La porta COM è chiusa"
		Return
	Else
		v3 = Input "Inserisci VSET"
		COM WriteString Address + "su" + Format(v3*100,"00000") + LF
		Button Button_VSET Text "VSET: " + v3 + "V"
		Button Button_VSET Color Green3
		Wait Seconds Tempo_Attesa
	EndIf
Return

Label ISET:
	If COM_Status = "CLOSED"
		Print "La porta COM è chiusa"
		Return
	Else
		v3 = Input "Inserisci ISET"
		COM WriteString Address + "si" + Format(v3*100,"0000") + LF
		Wait Seconds Tempo_Attesa
		Button Button_ISET Text "ISET: " + v3 + "A"
		Button Button_ISET Color Green3
		COM WriteString Address + "ri" + LF
		Wait Seconds Tempo_Attesa
		Print COM_Received
	EndIf
Return
End


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