;This is the base for many keysight multimeters

#metadef

#meta
#idString Keysight Technologies,U1461A,
#name Keysight U1461A
#handle K1461
#port com
#driver KeysightDMM
#baudrate 9600


; A list of possible column name with unit and formatter (SI, Time, Int, D0..D6)
#value VoltageDC V d5 VOLT,DIOD
#value VoltageAC V d4 VOLT:AC
#value VoltageACDC V d4 VOLT:ACDC
#value CurrentDC A si CURR
#value CurrentAC A si CURR:AC
#value CurrentACDC A si CURR:ACDC
#value Resistance ohm si RES,CONT,EBR
#value Capacitance F si CAP
#value Temperature C d1 TEMP:K 
#value Frequency Hz si FREQ,FREQ:AC
#value Period s si Period PER
#value Conductance S SI COND
#value DutyCycle % d2 PULS:PDUT
#value Decibel_m dBm d3 VOLT:DBM
#value Decibel_V dBV d3 VOLT:DBV
#value 0-20mA % d2 CPER:0-20mA
#value 4-20mA % d2 CPER:4-20mA
#value PulseWidth s SI PULS:PWID,PULS:PWID:AC
#value HighResistance s SI IR

; How to poll for data, this is used for table and #values?
; a #askMode, #cmdMode and #prepareSample is used before this is string is used.
; This is a single line command
#askValues FETC?

; String to ask about actual meter mode, 
; This is a single line command
#askMode CONF?
#askModeMathFormat getElement(unQuote(value),0," ");


#interfaceType DMM
#interface readValue 0


; Strings to configure device in different modes, using these forces a mode check/change
; When used to select mode first parameter must match a #value (4 parameter) and second parameter must match what #askMode returns
; It can also be used for just checking the mode, then the #value (4 parameter) must match what #askMode returns
; First parameter is also used in shortcut menu
#cmdMode Check_mode _ _


;----------------------------------------------------------------------------------------------------------------------------------
#metasection U125xA

#notes This meter support downloading of log to either table or as text, in table all entries are assumed to be in same mode. 
Actual logging mode must be configured in the setup menu, this will define how the buttons on the meter works.

#otherList
	menuItem[0]="A-log.csv";
	menuItem[1]="H-log.csv";
	menuItem[2]="A-log.txt";
	menuItem[3]="H-log.txt";

#otherData
var samples=0;
var cmd="";
if (name=="A-log.csv") 
	cmd="LOG? A";
	samples=200;
elseif (name=="H-log.csv") 
	cmd="LOG? H";
	samples=100;
endif;

var columnName=keysightU125xALog(deviceRead(handle,cmd+"001"),1);

popupShowProgressAsync("Downloading "+getMatch(name,"^[a-zA-Z-]+")+" data from "+handle,samples);
tableInitHeader("index,"+handle+"."+columnName);
var i;
for i=1 to samples do
	popupShowProgressSetPosition(i);
	var msg=deviceRead(handle,cmd+formatInt(i,3,3));
	if (strlen(msg)>10)
		var v=keysightU125xALog(msg);
		tableAddRow(i,v);
	else
		i=samples+1;
	endif;
	endfor
popupShowProgressSetPosition(samples+1);

#otherText
var samples=0;
var cmd="";
if (name=="A-log.txt") 
	cmd="LOG? A";
	samples=200;
elseif (name=="H-log.txt") 
	cmd="LOG? H";
	samples=100;
endif;

popupShowProgressAsync("Downloading "+getMatch(name,"^[a-zA-Z-]+")+" data from "+handle,samples);
var data="";
var i;
for i=1 to samples do
	popupShowProgressSetPosition(i);
	var msg=deviceRead(handle,cmd+formatInt(i,3,3));
	if (strlen(msg)>10)
		var v=keysightU125xALog(msg);
		print(i);
		print("\t"+formatSI(v)+"\t"+keysightU125xALog(msg,1)+"\r\n")
	else
		i=samples+1;
	endif;
	endfor
popupShowProgressSetPosition(samples+1);

#otherFunc
if (popupShowQuestion("Delete all logs","ok","Yes","No"))	
	deviceWrite(handle,"LOG:HAND 0");
	deviceWrite(handle,"LOG:TRIG 0");
	deviceWrite(handle,"LOG:AUTO 0");
	deviceWrite(handle,"LOG:EXPO 0");
	endif

#metasection
;----------------------------------------------------------------------------------------------------------------------------------

#metasection U128xA

#notes This meter support downloading of log to either table or as text, in table all entries are assumed to be in same mode. 
Actual logging mode must be configured in the setup menu, this will define how the buttons on the meter works.

#otherList
	menuItem[0]="Manual.csv";
	menuItem[1]="Event.csv";
	menuItem[2]="Interval.csv";
	menuItem[3]="Export.csv";
	menuItem[4]="Manual.txt";
	menuItem[5]="Event.txt";
	menuItem[6]="Interval.txt";
	menuItem[7]="Export.txt";
	menuItem[8]="Delete all logs.xxx";

#otherData
var samples=0;
var cmd="";
if (name=="Manual.csv") 
  cmd="LOG:HAND? ";
  samples=100;
elseif (name=="Event.csv") 
  cmd="LOG:TRIG? ";
  samples=10000;
elseif (name=="Interval.csv") 
  cmd="LOG:AUTO? ";
  samples=10000;
elseif (name=="Export.csv") 
  cmd="LOG:EXPO? ";
  samples=100;
endif;

var columnName=keysightU128xALog(deviceRead(handle,cmd+"1"),1);

popupShowProgressAsync("Downloading "+getMatch(name,"^[a-zA-Z]+")+" data from "+handle,samples);
tableInitHeader("index,"+handle+"."+columnName);
var i;
for i=1 to samples do
	popupShowProgressSetPosition(i);
	var msg=deviceRead(handle,cmd+i);
	if (strlen(msg)>10)
		var v=keysightU128xALog(msg);
		tableAddRow(i,v);
	else
		i=samples+1;
	endif;
	endfor
popupShowProgressSetPosition(samples+1);

#otherText
var samples=0;
var cmd="";
if (name=="Manual.txt") 
  cmd="LOG:HAND? ";
  samples=100;
elseif (name=="Event.txt") 
  cmd="LOG:TRIG? ";
  samples=10000;
elseif (name=="Interval.txt") 
  cmd="LOG:AUTO? ";
  samples=10000;
elseif (name=="Export.txt") 
  cmd="LOG:EXPO? ";
  samples=100;
endif;

popupShowProgressAsync("Downloading "+getMatch(name,"^[a-zA-Z]+")+" data from "+handle,samples);
var data="";
var i;
for i=1 to samples do
	popupShowProgressSetPosition(i);
	var msg=deviceRead(handle,cmd+i);
	if (strlen(msg)>10)
		var v=keysightU128xALog(msg);
		print(i);
		print("\t"+formatSI(v)+"\t"+keysightU128xALog(msg,1)+"\r\n")
	else
		i=samples+1;
	endif;
	endfor
popupShowProgressSetPosition(samples+1);

#otherFunc
if (popupShowQuestion("Delete all logs","ok","Yes","No"))	
	deviceWrite(handle,"LOG:HAND 0");
	deviceWrite(handle,"LOG:TRIG 0");
	deviceWrite(handle,"LOG:AUTO 0");
	deviceWrite(handle,"LOG:EXPO 0");
	endif

#metasection
