; *******************************************************************************************************
; Basic definition for Siglent SPD3303X/X-E 3 Channel PSU's.
; Supports Independent, Series and Parallel operating modes for Channels 1 and 2.
;
; Channels 1 and 2 voltages and currents are fully programmable and readable via SCPI.
; Channel 3 is a fixed voltage auxiliary channel (2.5, 3.3 or 5V, selectable via
; hardware switch on front panel, not via SCPI) that can only be turned ON and OFF.
;
; Only LAN connection is supported, not USB.
;
; *******************************************************************************************************
; Rev 1.0    04 Nov 2021  Initial Release.
; Rev 1.0.1  06 Nov 2021  Updated #interface section per recommendation of HKJ.
;                         Removed leading colons from SCPI commands in #askValues (for consistency).
;                         Added a remark about required firmware version to the #notes section.
;
; *******************************************************************************************************

; TestController must be restarted before any changes in this file will be used.
; Manual is here: https://lygte-info.dk/project/TestControllerConfigDevice%20UK.html


; SPD3303X has 1mV/1mA resolution
; SPD3303X tested with FW 1.01.01.02.07R2
#metadef
#idString Siglent Technologies,SPD3303X,
#name Siglent SPD3303X
#handle SPD33
#replaceText CH1_MaxVoltage 32
#replaceText CH1_MaxCurrent 3.2
#replaceText CH2_MaxVoltage 32
#replaceText CH2_MaxCurrent 3.2


; SPD3303X-E has 10mV/10mA resolution
; SPD3303X-E tested with FW 1.01.01.02.07R2
#metadef
#idString Siglent Technologies,SPD3303X-E,
#name Siglent SPD3303X-E
#handle SPD33
#replaceText CH1_MaxVoltage 32
#replaceText CH1_MaxCurrent 3.2
#replaceText CH2_MaxVoltage 32
#replaceText CH2_MaxCurrent 3.2


; SPD3303C has 10mV/10mA resolution
; SPD3303C has only USBTMC, not LAN. USBTMC connection not supported by TestController.
;#metadef
;#idString Siglent Technologies,SPD3303C,
;#name Siglent SPD3303C
;#handle SPD33
;#replaceText CH1_MaxVoltage 32
;#replaceText CH1_MaxCurrent 3.2
;#replaceText CH2_MaxVoltage 32
;#replaceText CH2_MaxCurrent 3.2


#meta
#idString Siglent SPD3303
#name Siglent SPD3303
#handle SPD33
#author CJS
#port 5025
; Alternate port types: comnobaud or com, com lets the user define baudrate on the "Load devices" page

; *******************************************************************************************************
#notes
Basic definition for Siglent SPD3303X/X-E 3 Channel PSU's.
Supports Independent, Series and Parallel operating modes for CH1 and CH2.

LIMITATIONS:
- Requires firmware version 1.01.01.02.07R2. Firmware versions 1.01.01.02.05 and earlier do not work.
- Only LAN connection is supported, not USB.
- The output state of CH3 cannot be read back. This is a limitation of the firmware.
- No support for Timer function of SPD3303X/X-E.
; *******************************************************************************************************

; A list of possible column name with unit and formatter (SI, Time, Int, D0..D6)
; Format: #value ColumnName Unit Format {Selector}
; Selector is only used when column layout varies with mode, this often require the use of #cmdMode
;
#value CH1_Voltage V D3
#value CH1_Current A D3
#value CH1_Power W D3
#value CH2_Voltage V D3
#value CH2_Current A D3
#value CH2_Power W D3


; How to poll for data, this is used for table and #values?
; a #askMode, #cmdMode and #prepareSample is used before this string is used.
; Number of returned values must match number of columns defined with #value
; This is a single line command
#askValues MEAS:VOLT? CH1; MEAS:CURR? CH1; MEAS:POWER? CH1; MEAS:VOLT? CH2; MEAS:CURR? CH2; MEAS:POWER? CH2

; Format of answer: f=float, u=remove trailing letters, x=skip, *=Zero upper case values if this value is 0
;#askValuesReadFormat

; Accept this delay when reading values (seconds)
#readingDelay 2

; Mode change have a longer delay on reading values (seconds)
#modeChangeDelay 5

; String to ask about actual meter mode, it is mostly used for DMM's
; This is a single line command
;#askMode

; When one of these commands are used through the command interface a new configuration will be done before using #askMode
; Only one word for each #mayModifyMode
; Specify command without initial colon and in the shortest possible form
;#mayModifyMode

; Prepare the meter to response to #askValues
;#prepareSample arm:sour imm;:arm:count 1;:trig:sour imm;:trig:count 1;:trig:samp:count 1;init

; Initial commands to meter when establishing connection, used to disable local control
;#initCmd

; Final command to meter before breaking connection, used to restore local control
;#finalCmd

; Used to turn output off for power supplies, generators and electronic loads
#outputOff OUTP CH1,OFF; OUTP CH2,OFF; OUTP CH3,OFF


; *************** Mode Popup  Window *****************
; In Series and Parallel mode, CH1 is Master and CH2 is Slave. In these modes the PSU copies
; CH1 Voltage and Current settings to CH2, and when CH1 is turned ON automatically also CH2
; is turned ON.

#cmdModeLayout 1 3

#cmdMode Independent _
OUTP:TRACK 0

#cmdMode Series _
OUTP:TRACK 1

#cmdMode Parallel _
OUTP:TRACK 2


; ********** Power Supply Setup/Configuration Menu  ********
; Using multi-channel interface in scripts requires vars set for PS, PS:2, PS:3 in scripts.
; For example:
;  =var PS1=getDevice("PS")
;  =var PS2=getDevice("PS:2")

#interfaceType PS PS:2 PS:3

#interface setVoltage CH(channel):VOLT (value)
#interface getVoltage CH(channel):VOLT?
:readmath: getMatch(value, "( |^)[-+.0-9].*")

#interface setCurrent CH(channel):CURR (value)
#interface getCurrent CH(channel):CURR?
:readmath: getMatch(value, "( |^)[-+.0-9].*")

#interface readVoltage 0 3
#interface readCurrent 1 4


; Voltage and Current SET points for CH1 and CH2

#cmdSetup number CH1_Voltage Setup
:read: CH1:VOLT?
:write: CH1:VOLT
:update: CH2_Voltage
:updatemodechange:
V  0 CH1_MaxVoltage

#cmdSetup number CH1_Current Setup
:read: CH1:CURR?
:write: CH1:CURR
:update: CH2_Current
:updatemodechange:
A  0 CH1_MaxCurrent

#cmdSetup number CH2_Voltage Setup
:read: CH2:VOLT?
:write: CH2:VOLT
:updatemodechange:
V  0 CH2_MaxVoltage

#cmdSetup number CH2_Current Setup
:read: CH2:CURR?
:write: CH2:CURR
:updatemodechange:
A  0 CH2_MaxCurrent


; **** Operating Mode selection also added to Setup menu for full control from a single menu.
;
#cmdSetup radio Operating_Mode
:read: SYST:STAT?
:readmath: matchBitsList(binConvBit(value,2,2),"01",0,"11",1,"10",2,0)
:write: OUTP:TRACK #
:update: CH1_Output CH2_Output CH2_Voltage CH2_Current
:updatemodechange:
:tip: Select CH1 and CH2 Operating Mode
Indep 0
Ser 1
Para 2

; ***** Buttons to turn outputs On/Off, common to all tabs ******
;
; SCPI command SYST:STAT? returns the hex value of a 10-bit status word.
; See Chapter 3 in SPD3303X Quick Start Guide for the definition of the system status bits.
; bits 2 and 3 indicate the Operating Mode of CH1 and CH2 (Independent, Series or Parallel).
; bits 4 and 5 indicate the Output State of CH1 and CH2 (ON or OFF).
;
; :readmath: is used to select status bits and to translate them to a number or string.
; displayVar() function can be used in :readmath: to see what is going on.

#cmdSetup buttons ALL_Outputs
:write: OUTP CH1,#; OUTP CH2,#; OUTP CH3,#
:update: CH1_Output CH2_Output
:tip: Turn ALL outputs ON or OFF
OFF OFF
ON ON

#cmdSetup buttonsOn CH1_Output
:read: SYST:STAT?
:readmath: (binConvBit(value,4,1) == "1") ? "ON" : "OFF"
:string:
:write: OUTP CH1,#
:update: CH2_Output
:updatemodechange:
:updatealloff:
:tip: Turn CH1 output ON or OFF
:color: (0,240,0)
:bgcolor: (128,128,128)
OFF OFF
ON ON

#cmdSetup buttonsOn CH2_Output
:enable: Operating_Mode==0
:read: SYST:STAT?
:readmath: (binConvBit(value,5,1) == "1") ? "ON" : "OFF"
:string:
:write: OUTP CH2,#
:updatemodechange:
:updatealloff:
:tip: Turn CH2 output ON or OFF
:color: (255,240,0)
:bgcolor: (128,128,128)
OFF OFF
ON ON

; CH3 has no indicator because SYST:STAT? only returns the output states of CH1 and CH2.
#cmdSetup buttons CH3_Output
:write: OUTP CH3,#
:tip: Turn CH3 output ON or OFF
OFF OFF
ON ON
