diff options
Diffstat (limited to 'monitor_supply.py')
-rwxr-xr-x | monitor_supply.py | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/monitor_supply.py b/monitor_supply.py new file mode 100755 index 0000000..c5b085a --- /dev/null +++ b/monitor_supply.py @@ -0,0 +1,173 @@ +#!/usr/bin/env python +# +# agilent66332Adriver.py +# +# Agilent 66332A power supply driver +# + +import serial, time, sys, os + +class agilent66332Adriver(): + def __init__(self, ttyport): + self.port = ttyport + + def open(self): + try: + self.serialPort = serial.Serial(self.port, 9600, timeout=2, writeTimeout=2, bytesize=8, parity='N', stopbits=1, xonxoff=0, rtscts=1) + except ValueError: + print "!!! Error: parameter are out of range" + raise ValueError + except serial.SerialException: + print "!!! Error: the device can not be found or can not be configured" + raise serial.SerialException + except Exception as e: + raise e + + def close(self): + self.serialPort.flushOutput() + self.serialPort.flushInput() + self.serialPort.close() + + def sendCmd(self, cmd, read=False): + output = "" + try: + self.serialPort.write(cmd+"\n") + while read == True: + line = self.serialPort.readline() + if not line: + break; + if cmd == line.rstrip(): + continue + else: + output=output+line + break; + except ValueError: + print "!!! Error: closed port" + raise ValueError + except serial.SerialTimeoutException: + print "!!! Error: write timeout" + raise serial.SerialTimeoutException + except Exception as e: + raise e + + return output + + + def reset(self): + try: + self.sendCmd("*RST") + except Exception as e: + raise e + + def getId(self): + value = '-1.0' + try: + value = self.sendCmd("*IDN?", read=True).rstrip() + except Exception as e: + raise e + + return (value) + + # Configure output voltage + def setOutputVolt(self, volt): + try: + self.sendCmd("VOLT %s"%volt) + except Exception as e: + raise e + + # Configure output current + def setOutputCurrent(self, current): + try: + self.sendCmd("CURR %s"%current) + except Exception as e: + raise e + + # Enable output + def setOutputState(self, state): + if (not state == "ON" and not state == "OFF"): + raise ValueError + try: + self.sendCmd("OUTP %s"%state) + except Exception as e: + raise e + + def configMeasure(self, period=15.6, size=4096): + + interval = '%2.1f' % period + "E-6" + print "setting interval to "+interval+" with "+str(size)+" samples" + try: + self.sendCmd("SENS:SWE:TINT %s"%interval) + self.sendCmd("SENS:SWE:POIN %s"%size) + except Exception as e: + raise e + + pass + + def readCurrent(self): + value = '-1.0' + try: + value = self.sendCmd("MEAS:CURR?", read=True).rstrip() + except Exception as e: + raise e + + return (float(value)) + + def readVoltage(self): + value = '-1.0' + try: + value = self.sendCmd("MEAS:VOLT?", read=True).rstrip() + except Exception as e: + raise e + + return (float(value)) + + def readCurrentBuffer(self): + array = '0' + try: + array = self.sendCmd("MEAS:ARR:CURR?", read=True).rstrip() + except Exception as e: + raise e + list_current = array.split(",") + return list_current + + def writeBuffer(self, outfile, list_current, period=15.6): + try: + fo = open(outfile, "w+") + except IOError: + print "WARN: Unable to open %s", outfile + sys.exit(2) + + myline="Time I_supply\n" + fo.write(myline) + + time = 0 + for value in list_current: + myline = str(int(time))+" "+str(int(float(value)*1000000))+"\n" + time += period + + fo.write(myline) + + fo.close() + + return + + +if __name__ == '__main__': + period = 400 + # simple interactive demo/test + driver=agilent66332Adriver('/dev/ttyUSB0') + driver.open() + print driver.getId() + driver.reset() + driver.configMeasure(period, 4096) + driver.setOutputVolt("5") + driver.setOutputCurrent("4.0") + driver.setOutputState("ON") + print driver.readCurrent() + print driver.readVoltage() + + raw_input("Press Enter to continue...") + +# driver.writeBuffer("tmp.txt", driver.readCurrentBuffer(), period) + driver.close() + |