def hvb_assembly_test(multimeter_addr="192.168.1.102",hv_supply_addr="RS232",\
    SERIAL_NUMBER="123456",ISEG_VOLTAGE="1000",TIME_PERIOD="0.2",PURPOSE="HVB_RawTest",LOC="/home/pi/daqtest1"):

    filename = LOC+SERIAL_NUMBER+"_"+PURPOSE+".csv" 
    with open(filename,'a') as csvfile: 
	writer = csv.writer(csvfile) 
	writer.writerow(['Purpose','DateTime','Serial_Number','BoardName',\
	    'Channel','ISEG_V','LoadRelay1','LoadRelay2','K','MCPAT','MCPAB',\
	    'MCPBT','MCPBB','Result_V'])

    '''
    # Make iseg object
    iseg = Iseg_SHQ226L["RS232")
    iseg.reset()

    # Initialize parameters
    iseg.channel=1
    channel=iseg.channel
    print "ISEG Channel is: "+channel
    iseg.set_voltage = ISEG_VOLTAGE
    #time.sleep(0.5)
    set2_voltage = iseg.set_voltage
    print "Set voltage is: %r" %set2_voltage
    '''    

    # Make keithley object
    keith=Keithley_2010("192.168.1.102",1234)

    # Initialize parameters
    id=keith.identification()
    print id
    keith.reset()
    keith.configure = "VOLTAGE"
    keith.configure_voltage = "DC"
    func,acdc = keith.configure_voltage
    unit = keith.unit_voltage
    print "Keithley2010 Multimeter configured to: "+acdc+" "+func+"\n"
    

    load_state=[(0,0),(0,1),(1,0),(1,1)]
    mux_state=[(0,0,0,0,1),(0,0,0,1,0),(0,0,1,0,0),(0,1,0,0,0),(1,0,0,0,0)]
    channel_addr={1:(0,0,0),2:(0,0,1),3:(0,1,0),4:(0,1,1),5:(1,0,0),6:(1,0,1),7:(1,1,0),8:(1,1,1)}


    count = 0 
    for channel in range(1,9):
	for j in range(0,4):
	    for k in range (0,5):
		row = list()
		# print "channel address is: ",channel_addr[channel]
		a = channel_addr[channel]
		b = load_state[j]
		c = mux_state[k]
		hvb_gpio.change_states(a,b,c)
		time.sleep(float(TIME_PERIOD)/2)
		result = keith.read_voltage

		row.append('HVB_RawTest')
		ts=time.time()
		DateTime=datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
		row.append(DateTime)
		row.append(SERIAL_NUMBER)
		row.append('HVB_Board_Assembly')
		row.append(channel)
		row.append(ISEG_VOLTAGE)
		row.append(load_state[j][0])
		row.append(load_state[j][1])
		row.append(mux_state[k][0])
                row.append(mux_state[k][1])
                row.append(mux_state[k][2])
                row.append(mux_state[k][3])
                row.append(mux_state[k][4])
		row.append(float(result))
		
		count+=1
		print "Measurement #"+str(count)+"= "+result+" "+unit
		with open(filename,'a') as csvfile:
		    writer = csv.writer(csvfile)
		    writer.writerow(row)	
	    
	        time.sleep(float(TIME_PERIOD)/2)
		
    # Reset parameters
    # iseg.set_voltage = 0
        
    # Reset all GPIO pins of RPi to LOW (0)
    hvb_gpio.reset_all_gpio()
def hvb_assembly_test(multimeter_addr="192.168.1.102",hv_supply_addr="RS232",SERIAL_NUMBER="123456",ISEG_VOLTAGE="1000",ISEG_RAMP_SPEED="10",TIME_PERIOD="0.2",PURPOSE="HVB_RawTest",CodeVersion='1',LOC="/home/pi/daqtest1/"):

    if (float(ISEG_VOLTAGE) < 0) or (float(ISEG_VOLTAGE) > float(ISEG_VOLTAGE_LIMIT)):
	print bcolors.FAIL+"Error!!! ISEG Voltage must be between 0 and "+str(ISEG_VOLTAGE_LIMIT)+\
	    "for test"+bcolors.ENDC
	logging.error('Error!!! ISEG Voltage must be between 0 and '+\
	    str(ISEG_VOLTAGE_LIMIT)+'for test!')
	# Reset parameters
	iseg.channel = 1      # Ch 1
	iseg.set_voltage = 0
	status1 = iseg.start_voltage_change # This triggers it to change voltage
	iseg.channel = 2      # Ch 2
	iseg.set_voltage = 0
	status2 = iseg.start_voltage_change # This triggers it to change voltage
	# Force software to exit
	sys.exit()

    # Set name of csv file for results
    #filename = LOC+SERIAL_NUMBER+"_"+PURPOSE+".csv" 
    filename = LOC+PURPOSE+".csv"    

    # Attach BoardName+CodeVersion
    # Ex. HVB_Board_Assembly1 where 1 is CodeVersion number
    BoardName = 'HVB_Board_Assembly'+str(CodeVersion)

    # If file does not exist, set name of headers for results in csv file
    # If file exists, it is not necessary to set name of headers because
    # it should already have headers for the file
    if os.path.exists(filename):
	passed = False # Initialize variable to check if header exist
	fIn = open(filename)
	for line in fIn:
	    line=line.replace(',',' ') # Replace each blank space
	    value = line.split()
	    # check to see if file has same header
	    try:
		if value[0].lower()=='serial' and (value[1].lower()=='datetime') and \
	       (value[2].lower()=='boardname') and (value[3].lower()=='loadcondition') \
	       and (value[4].lower()=='psvoltageset') and \
	       (value[5].lower()=='psvoltage1') and (value[6].lower()=='pscurrent1_ua') \
	       and (value[7].lower()=='psvoltage2') and (value[8].lower()=='pscurrent2_ua') \
	       and (value[9].lower()=='v0') and (value[10].lower()=='v1') and \
	       (value[11].lower()=='v2') and (value[12].lower()=='v3') and \
	       (value[13].lower()=='v4') and (value[14].lower()=='v5') and \
               (value[15].lower()=='v6') and (value[16].lower()=='v7') and \
               (value[17].lower()=='v8') and (value[18].lower()=='v9') and \
               (value[19].lower()=='v10') and (value[20].lower()=='v11') and \
               (value[21].lower()=='v12') and (value[22].lower()=='v13') and \
               (value[23].lower()=='v14') and (value[24].lower()=='v15') and \
               (value[25].lower()=='v16') and (value[26].lower()=='v17') and \
               (value[27].lower()=='v18') and (value[28].lower()=='v19') and \
               (value[29].lower()=='v20') and (value[30].lower()=='v21') and \
               (value[31].lower()=='v22') and (value[32].lower()=='v23') and \
               (value[33].lower()=='v24') and (value[34].lower()=='v25') and \
               (value[35].lower()=='v26') and (value[36].lower()=='v27') and \
               (value[37].lower()=='v28') and (value[38].lower()=='v29') and \
               (value[39].lower()=='v30') and (value[40].lower()=='v31') and \
               (value[41].lower()=='v32') and (value[42].lower()=='v33') and \
               (value[43].lower()=='v34') and (value[44].lower()=='v35') and \
               (value[45].lower()=='v36') and (value[46].lower()=='v37') and \
               (value[47].lower()=='v38') and (value[48].lower()=='v39'):
		    passed = True
		    break
	    except IndexError:
		pass
	    #print "Checking file to see if it has right header..."
	if not passed:
	    print bcolors.FAIL+"Error!!! File exists already and it is not related to our test!"+bcolors.ENDC
	    logging.error('Error!!! File exists and not related to our test!')
	    # Reset parameters
	    iseg.channel = 1	 # Ch 1
	    iseg.set_voltage = 0
	    status1 = iseg.start_voltage_change # This triggers it to change voltage
	    iseg.channel = 2	 # Ch 2
	    iseg.set_voltage = 0
	    status2 = iseg.start_voltage_change # This triggers it to change voltage
	    # Force software to exit
	    sys.exit()
    else:
	with open(filename,'a') as csvfile: 
	    writer = csv.writer(csvfile) 
	    writer.writerow(['Purpose', PURPOSE])
	    writer.writerow(['CodeVersion', CodeVersion])
	    writer.writerow('')
	    writer.writerow(['Serial','DateTime','BoardName','LoadCondition',\
		'PSvoltageSet','PSvoltage1','PScurrent1_uA','PSvoltage2',
		'PScurrent2_uA','V0','V1','V2','V3','V4','V5','V6','V7','V8','V9',\
		'V10','V11','V12','V13','V14','V15','V16','V17','V18','V19',\
		'V20','V21','V22','V23','V24','V25','V26','V27','V28','V29',\
		'V30','V31','V32','V33','V34','V35','V36','V37','V38','V39'])

    
    # Make iseg object
    iseg = Iseg_SHQ226L("RS232")

    print "Initializing parameters for ISEG SHQ226L Supply..."

    # Initialize parameters for ISEG Channel #1
    iseg.channel=1  # NOTE: Remember to always set channel
                    # of ISEG before sending commands!!!
    iseg.ramp_speed = ISEG_RAMP_SPEED
    iseg.set_voltage = ISEG_VOLTAGE
    #status1 = iseg.start_voltage_change # This triggers it to change voltage    

    # Initialize parameters for ISEG Channel #2
    iseg.channel=2  # NOTE: Remember to always set channel
                    # of ISEG before sending commands!!!
    iseg.ramp_speed = ISEG_RAMP_SPEED
    iseg.set_voltage = ISEG_VOLTAGE
    #status2 = iseg.start_voltage_change # This triggers it to change voltage  

    # Grab values for ISEG Channel #1 	
    iseg.channel=1  # NOTE: Remember to always set channel
		    # of ISEG before sending commands!!!
    channel1 = iseg.channel
    #set_voltage1 = iseg.set_voltage
    time.sleep(CMD_DELAY)
    actual_voltage1 = -1*float(iseg.actual_voltage) # flipping voltage
    time.sleep(CMD_DELAY)
    actual_current1 = float(iseg.actual_current)*10**6
    time.sleep(CMD_DELAY)
    ramp_speed1 = iseg.ramp_speed

    # Grab values for ISEG Channel #2   
    iseg.channel=2  # NOTE: Remember to always set channel
                    # of ISEG before sending commands!!!
    channel2 = iseg.channel
    #set_voltage2 = iseg.set_voltage
    time.sleep(CMD_DELAY)
    actual_voltage2 = -1*float(iseg.actual_voltage) # flipping voltage
    time.sleep(CMD_DELAY)
    actual_current2 = float(iseg.actual_current)*10**6
    time.sleep(CMD_DELAY)
    ramp_speed2 = float(iseg.ramp_speed)
  
    # Calculate how long it will take voltage to ramp up to desired voltage 
    voltage_delay1 = (float(ISEG_VOLTAGE)/float(ramp_speed1))*VOLTAGE_DELAY_ERROR
    voltage_delay2 = (float(ISEG_VOLTAGE)/float(ramp_speed2))*VOLTAGE_DELAY_ERROR
    voltage_delay = voltage_delay1 + voltage_delay2 

    # Update how long it will take for voltage to reach set Voltage
    print "\tISEG Set voltage is: "+bcolors.OKGREEN+"%s V"%ISEG_VOLTAGE+bcolors.ENDC
    print "\tISEG Ramp Speed is: "+str(ISEG_RAMP_SPEED)+" V/s"
    print "\tTherefore, it will take %f sec(s) "%voltage_delay+\
	  "for actual voltage to reach the set voltage...\n"

    # Print current status
    print "Current status is..."
    print "\tISEG HV Supply Channel #"+channel1+":"
    actual_voltage1_error = abs(actual_voltage1-float(ISEG_VOLTAGE))
    if (actual_voltage1_error >= 10): # Error
	print "\t>> actual voltage = "+bcolors.FAIL+bcolors.BOLD+"%0.2f V"%actual_voltage1+bcolors.ENDC
    elif (actual_voltage1_error >= 0.5) and (actual_voltage1_error < 10): # Warning
        print "\t>> actual voltage = "+bcolors.CYAN+bcolors.BOLD+"%0.2f V"%actual_voltage1+bcolors.ENDC
    else:                             # OKAY
        print "\t>> actual voltage = "+bcolors.OKGREEN+bcolors.BOLD+"%0.2f V"%actual_voltage1+bcolors.ENDC

    print "\t>> actual current = %0.2f uA"%actual_current1
    print "\tISEG HV Supply Channel #"+channel2+":"
    actual_voltage2_error = abs(actual_voltage2-float(ISEG_VOLTAGE))
    if (actual_voltage2_error >= 10): # Error
        print "\t>> actual voltage = "+bcolors.FAIL+bcolors.BOLD+"%0.2f V"%actual_voltage2+bcolors.ENDC
    elif (actual_voltage2_error >= 0.5) and (actual_voltage2_error < 10): # Warning
        print "\t>> actual voltage = "+bcolors.CYAN+bcolors.BOLD+"%0.2f V"%actual_voltage2+bcolors.ENDC
    else:                             # OKAY
        print "\t>> actual voltage = "+bcolors.OKGREEN+bcolors.BOLD+"%0.2f V"%actual_voltage2+bcolors.ENDC
    print "\t>> actual voltage = %0.2f V"%actual_voltage2
    print "\t>> actual current = %0.2f uA"%actual_current2

    # Print Wait update
    print "\nWaiting "+str(voltage_delay)+" sec(s)...\n"


    # Trigger ISEG Voltage
    iseg.channel = 1
    status1 = iseg.start_voltage_change # This triggers it to change voltage 
    time.sleep(voltage_delay1) # Waiting for actual voltage of iseg 
                                    # to reach set voltage
    iseg.channel = 2
    status2 = iseg.start_voltage_change # This triggers it to change voltage 
    time.sleep(voltage_delay2) # Waiting for actual voltage of iseg 
                               # to reach set voltage
    
    print "About to start HVB Automated Test for HVB Assembly with"+\
	" Serial #"+SERIAL_NUMBER+" Now."+"\nResults may be found"+\
	" in "+"directory at: "+LOC
    # This while() loop is responsible for the interactive 
    # prompt to see if you are satisfied with the actal voltage.
    # It will wait until you want to proceed.
    while(1):
	# Grab values for ISEG Channel #1   
	iseg.channel=1  # NOTE: Remember to always set channel
			# of ISEG before sending commands!!!
	channel1 = iseg.channel
	#set_voltage1 = iseg.set_voltage
        time.sleep(CMD_DELAY)
	actual_voltage1 = -1*float(iseg.actual_voltage) # flipping voltage
	time.sleep(CMD_DELAY)
	actual_current1 = float(iseg.actual_current)*10**6
        #time.sleep(CMD_DELAY)
	#ramp_speed1 = iseg.ramp_speed
	voltage_delay1 = (float(ISEG_VOLTAGE)/float(ISEG_RAMP_SPEED))*VOLTAGE_DELAY_ERROR

	# Grab values for ISEG Channel #2   
	iseg.channel=2  # NOTE: Remember to always set channel
			# of ISEG before sending commands!!!
	channel2 = iseg.channel
	#set_voltage2 = iseg.set_voltage
        time.sleep(CMD_DELAY)
	actual_voltage2 = -1*float(iseg.actual_voltage) # flipping voltage
        time.sleep(CMD_DELAY)
	actual_current2 = float(iseg.actual_current)*10**6
        #time.sleep(CMD_DELAY)
	#ramp_speed2 = iseg.ramp_speed
	voltage_delay2 = float(ISEG_VOLTAGE)/float(ISEG_RAMP_SPEED)
	print "\nStatus of ISEG HV Supply with PSvoltageSet="+bcolors.OKGREEN+bcolors.BOLD+ISEG_VOLTAGE+\
	    "V"+bcolors.ENDC+" is: "
	print "\tISEG HV Supply Channel #"+channel1+":"
	actual_voltage1_error = abs(actual_voltage1-float(ISEG_VOLTAGE))
	if (actual_voltage1_error >= 10): # Error
	    print "\t>> actual voltage = "+bcolors.FAIL+bcolors.BOLD+"%0.2f V"%actual_voltage1+bcolors.ENDC
	elif (actual_voltage1_error >= 0.5) and (actual_voltage1_error < 10): # Warning
	    print "\t>> actual voltage = "+bcolors.CYAN+bcolors.BOLD+"%0.2f V"%actual_voltage1+bcolors.ENDC
	else:                             # OKAY
	    print "\t>> actual voltage = "+bcolors.OKGREEN+bcolors.BOLD+"%0.2f V"%actual_voltage1+bcolors.ENDC
	print "\t>> actual current = %0.2f uA"%actual_current1
	print "\tISEG HV Supply Channel #"+channel2+":"
	actual_voltage2_error = abs(actual_voltage2-float(ISEG_VOLTAGE))
	if (actual_voltage2_error >= 10): # Error
	    print "\t>> actual voltage = "+bcolors.FAIL+bcolors.BOLD+"%0.2f V"%actual_voltage2+bcolors.ENDC
	elif (actual_voltage2_error >= 0.5) and (actual_voltage2_error < 10): # Warning
	    print "\t>> actual voltage = "+bcolors.CYAN+bcolors.BOLD+"%0.2f V"%actual_voltage2+bcolors.ENDC
	else:                             # OKAY
	    print "\t>> actual voltage = "+bcolors.OKGREEN+bcolors.BOLD+"%0.2f V"%actual_voltage2+bcolors.ENDC	
	print "\t>> actual current = %0.2f uA"%actual_current2
	RESPONSE = raw_input("\tPlease enter (c)ontinue, (e)xit, "+\
	    "(u)pdate, or (s)et_voltage: ")
	try:
	    if (RESPONSE.lower() == "exit" or RESPONSE.lower() == "e"):
		print "\nExiting and resetting parameters..."
		# Reset parameters
		iseg.channel = 1      # Ch 1
		iseg.set_voltage = 0
		status1 = iseg.start_voltage_change # This triggers it to change voltage	
		iseg.channel = 2      # Ch 2
		iseg.set_voltage = 0
		status2 = iseg.start_voltage_change # This triggers it to change voltage
		# Force software to exit
		sys.exit()
	    elif (RESPONSE.lower() == "continue" or RESPONSE.lower() == "c"):
		break
	    elif (RESPONSE.lower() == "update" or RESPONSE.lower() == "u"):
		pass
            elif (RESPONSE.lower() == "set_voltage" or RESPONSE.lower() == "s"):
		while(1):
		    ISEG_VOLTAGE = raw_input('\tPlease enter new set voltage'+\
			' (V): ')
		    try:
			if (ISEG_VOLTAGE.lower() == "exit" or ISEG_VOLTAGE.lower() == "e"):
			    print "\nExiting and resetting parameters..."
			    # Reset parameters
			    iseg.channel = 1     # Ch 1
			    iseg.set_voltage = 0
			    status1 = iseg.start_voltage_change # This triggers it to change voltage
			    iseg.channel = 2     # Ch 2
			    iseg.set_voltage = 0
			    status2 = iseg.start_voltage_change # This triggers it to change voltage
			    # Force software to exit
			    sys.exit()
			elif (int(ISEG_VOLTAGE) < 0) or (int(ISEG_VOLTAGE) > float(ISEG_VOLTAGE_LIMIT)):
			    print bcolors.FAIL+"\t>> Error!!! ISEG_VOLTAGE must be between 0 and %0.2f!"%ISEG_VOLTAGE_LIMIT+bcolors.ENDC
			else:  # Setting new ISEG_VOLTAGE
			    # Initialize parameters for ISEG Channel #1
			    iseg.channel=1  # NOTE: Remember to always set channel
					    # of ISEG before sending commands!!!
			    iseg.set_voltage = ISEG_VOLTAGE
                            # Initialize parameters for ISEG Channel #2
                            iseg.channel=2  # NOTE: Remember to always set channel
                                            # of ISEG before sending commands!!!
                            iseg.set_voltage = ISEG_VOLTAGE
			    
			    # Calculate Waiting time
			    voltage_delay = (abs((float(ISEG_VOLTAGE)-float(actual_voltage1))/\
				float(ISEG_RAMP_SPEED)) + abs((float(ISEG_VOLTAGE) - \
				float(actual_voltage2))/float(ISEG_RAMP_SPEED)))*VOLTAGE_DELAY_ERROR

			    # Print Wait update
			    print "\nWaiting "+str(voltage_delay)+" sec(s)...\n"

			    # Trigger ISEG Voltage
			    iseg.channel = 1
			    status1 = iseg.start_voltage_change # This triggers it to change voltage 
			    iseg.channel = 2
			    status2 = iseg.start_voltage_change # This triggers it to change voltage 
			    time.sleep(voltage_delay) # Waiting for actual voltage of iseg 
					               # to reach set voltage
			    break
		    except ValueError:
			print bcolors.FAIL+"\t>> Error!!! ISEG_VOLTAGE must be between 0 and %0.2f!"%ISEG_VOLTAGE_LIMIT+bcolors.ENDC
	    else:
		print bcolors.FAIL+'\t>> Error!!! Must enter "(c)ontinue", "(e)xit", '+\
		    '"(u)pdate", or "(s)et_voltage"!'+bcolors.ENDC	
	except ValueError:
            print bcolors.FAIL+'\t>> Error!!! Must enter "(c)ontinue", "(e)xit", '+\
                '"(u)pdate", or "(s)et_voltage"!'+bcolors.ENDC 
    print "\n"

    logging.info("Status of ISEG HV Supply with PSvoltageSet="+\
	ISEG_VOLTAGE+"V is: ")
    logging.info("\tISEG HV Supply Channel #"+channel1+":")
    logging.info("\t>> actual voltage = %0.2f V"%actual_voltage1)
    logging.info("\t>> actual current = %0.2f uA"%actual_current1)
    logging.info("\tISEG HV Supply Channel #"+channel2+":")
    logging.info("\t>> actual voltage = %0.2f V"%actual_voltage2)
    logging.info("\t>> actual current = %0.2f uA"%actual_current2)

    
    # Make keithley object
    keith=Keithley_2010("192.168.1.102",1234)

    # Initialize parameters
    id=keith.identification() # Get identification of instrument
    print id
    keith.reset() # reset configurations
    keith.configure = "VOLTAGE"     # configure function to voltage
    keith.configure_voltage = "DC"  # configure voltage to DC
    func,acdc = keith.configure_voltage  # Get configuration settings
					 # to see if it is configured to
					 # voltage and dc
    keith.unit_voltage = 'VPP'  # Set units of voltage 
    unit = keith.unit_voltage	# Get units of voltage

    print "Keithley2010 Multimeter configured to: "+acdc+" "+func+" "+unit+"\n"
   

 
    # Necessary states to control 
    load_state=[(0,0),(0,1),(1,0),(1,1)]
    mux_state=[(0,0,0,0,1),(0,0,0,1,0),(0,0,1,0,0),(0,1,0,0,0),(1,0,0,0,0)]
    channel_addr={0:(0,0,0),1:(0,0,1),2:(0,1,0),3:(0,1,1),4:(1,0,0),5:(1,0,1),6:(1,1,0),7:(1,1,1)}
    ts = time.time()
    DateTime=datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    row = list()
    count = 0

    # Reset all GPIO pins of RPi to LOW (0)
    hvb_gpio.reset_all_gpio()   # Safety practice ensuring GPIO pins are low

    for LoadCondition in range(0,4): # changing load relays
	print "\nUnder LoadCondition = "+str(LoadCondition)
	# Update row to be written on csv
	row.append(SERIAL_NUMBER)
        row.append(DateTime) 
	row.append(BoardName)
	row.append(LoadCondition)  # LoadCondition:  0 = off off
		       #		 1 = off on
		       #		 2 = on off
		       #		 3 = on on
	row.append(ISEG_VOLTAGE)

	iseg.channel=1  # NOTE: Remember to always set channel
			# of ISEG before sending commands!!!
	time.sleep(CMD_DELAY)
        PSvoltage1 = -1*float(iseg.actual_voltage) # Actual voltage from ISEG Ch1
						   # flipping voltage
	time.sleep(CMD_DELAY)
	PScurrent1 = float(iseg.actual_current)    # Actual currrent from ISEG Ch1
	iseg.channel=2  # NOTE: Remember to always set channel
                        # of ISEG before sending commands!!!
	time.sleep(CMD_DELAY)
        PSvoltage2 = -1*float(iseg.actual_voltage) # Actual voltage from ISEG Ch2
						   # flipping voltage
	time.sleep(CMD_DELAY)
	PScurrent2 = float(iseg.actual_current)    # Actual current from ISEG Ch2

	# Update row to be written on csv
	row.append(format(PSvoltage1,'.2f'))
	row.append(format(PScurrent1*10**6,'.2f'))
        row.append(format(PSvoltage2,'.2f'))
        row.append(format(PScurrent2*10**6,'.2f'))

	for channel in range(0,8): # choosing channel
	    for k in range (0,5):  # Choosing different parameters in mux_state
		# print "channel address is: ",channel_addr[channel]
		a = channel_addr[channel]  # channel is associated with test board # on stack
		b = load_state[int(LoadCondition)]
		c = mux_state[k]
		hvb_gpio.change_states(a,b,c)
		time.sleep(float(TIME_PERIOD)/2)
		vdmm = keith.read_voltage # actual reading from keithley2010 mult.
		vdmm = -1*float(vdmm)	
	
		# Formula to convert Keithley DMM measures into real voltage on
		# HVB output terminals/test board input terminals, that this
		# corresponds to. It is actual voltage we want to store in test
		# test database. (NOTE: If it were remeasured at some later date with
		# a replacement test system, the intermediate voltage of the DMM
	        # might differ, it's the actual voltage which is really the 
		# universal thing of interest)
		b = 502.522692 # b constant for eqn
		c = 1.04007e-3 # c constant for eqn
		d = 1.96453e-5 # d constant for eqn
		# result is associated with V0, V1, V2, V3, .... V39
		result = format(b*vdmm*(1-c*vdmm-d*(vdmm**2)),'.2f')
		    
		row.append(result)
		
		print "\tV"+str(count)+" = "+str(result)+" "+"V"
		count+=1
		time.sleep(float(TIME_PERIOD)/2)
	count = 0

	with open(filename,'a') as csvfile:
	    writer = csv.writer(csvfile)
	    writer.writerow(row)	
	row = []
    
	
    # Reset parameters
    iseg.channel = 1
    iseg.set_voltage = 0
    iseg.channel = 2
    iseg.set_voltage = 0
        
    # Reset all GPIO pins of RPi to LOW (0)
    hvb_gpio.reset_all_gpio()
def hvb_assembly_test(multimeter_addr="192.168.1.102",hv_supply_addr="RS232",SERIAL_NUMBER="123456",ISEG_VOLTAGE="1000",ISEG_RAMP_SPEED="10",TIME_PERIOD="0.2",PURPOSE="HVB_RawTest",LOC="/home/pi/daqtest1/"):

    # Set name of csv file for results
    filename = LOC+SERIAL_NUMBER+"_"+PURPOSE+".csv" 
    # Set name of headers for results in csv file
    with open(filename,'a') as csvfile: 
	writer = csv.writer(csvfile) 
	writer.writerow(['Purpose','DateTime','Serial_Number','BoardName',\
	    'Channel','ISEG_V','LoadRelay1','LoadRelay2','K','MCPAT','MCPAB',\
	    'MCPBT','MCPBB','Result_V'])

    
    # Make iseg object
    iseg = Iseg_SHQ226L("RS232")

    # Initialize parameters
    print "Initializing parameters for ISEG SHQ226L Supply..."
    iseg.channel=1
    iseg.ramp_speed = ISEG_RAMP_SPEED
    iseg.set_voltage = ISEG_VOLTAGE

    # Grab values 
    status = iseg.start_voltage_change # This triggers it to change voltage    
    channel = iseg.channel
    set_voltage = iseg.set_voltage
    actual_voltage = iseg.actual_voltage
    ramp_speed = iseg.ramp_speed
    voltage_delay = float(ISEG_VOLTAGE)/float(ISEG_RAMP_SPEED)

    # Print status
    print "\tISEG Channel is: "+channel
    print "\tStatus is: %r" %status
    print "\tSet voltage is: %r V" %set_voltage
    print "\t>> But, currently, actual voltage is: %r V" %actual_voltage    
    print "\tVoltage Ramp Speed is: %r" %ramp_speed
    print "\t>> Therefore, it will take "+str(voltage_delay)+" sec(s) to"+\
	  " reach set voltage="+str(set_voltage)+"V"

    time.sleep(voltage_delay) # Waiting for actual voltage of iseg 
			      # to reach set voltage

    # Print update status to see if actual_voltage=set_voltage now
    actual_voltage = iseg.actual_voltage
    print "\tActual voltage on ISEG Channel "+channel+" is now: "+actual_voltage+" V"

    # Make keithley object
    keith=Keithley_2010("192.168.1.102",1234)

    # Initialize parameters
    id=keith.identification() # Get identification of instrument
    print id
    keith.reset() # reset configurations
    keith.configure = "VOLTAGE"     # configure function to voltage
    keith.configure_voltage = "DC"  # configure voltage to DC
    func,acdc = keith.configure_voltage  # Get configuration settings
					 # to see if it is configured to
					 # voltage and dc
    keith.unit_voltage = 'VPP'  # Set units of voltage 
    unit = keith.unit_voltage	# Get units of voltage

    print "Keithley2010 Multimeter configured to: "+acdc+" "+func+" "+unit+"\n"
    
    # Necessary states to control 
    load_state=[(0,0),(0,1),(1,0),(1,1)]
    mux_state=[(0,0,0,0,1),(0,0,0,1,0),(0,0,1,0,0),(0,1,0,0,0),(1,0,0,0,0)]
    channel_addr={1:(0,0,0),2:(0,0,1),3:(0,1,0),4:(0,1,1),5:(1,0,0),6:(1,0,1),7:(1,1,0),8:(1,1,1)}


    count = 0
    for j in range(0,4): # changing load relays
	for channel in range(1,9): # choosing channel
	    for k in range (0,5):  # Choosing different parameters in mux_state
		row = list()
		# print "channel address is: ",channel_addr[channel]
		a = channel_addr[channel]
		b = load_state[j]
		c = mux_state[k]
		hvb_gpio.change_states(a,b,c)
		time.sleep(float(TIME_PERIOD)/2)
		result = keith.read_voltage

		row.append('HVB_RawTest')
		ts=time.time()
		DateTime=datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
		row.append(DateTime)
		row.append(SERIAL_NUMBER)
		row.append('HVB_Board_Assembly')
		row.append(channel)
		row.append(ISEG_VOLTAGE)
		row.append(load_state[j][0])
		row.append(load_state[j][1])
		row.append(mux_state[k][0])
                row.append(mux_state[k][1])
                row.append(mux_state[k][2])
                row.append(mux_state[k][3])
                row.append(mux_state[k][4])
		row.append(float(result))
		
		count+=1
		print "Measurement #"+str(count)+"= "+result+" "+unit
		with open(filename,'a') as csvfile:
		    writer = csv.writer(csvfile)
		    writer.writerow(row)	
	    
	        time.sleep(float(TIME_PERIOD)/2)
		
    # Reset parameters
    iseg.set_voltage = 0
        
    # Reset all GPIO pins of RPi to LOW (0)
    hvb_gpio.reset_all_gpio()