Instantly share code, notes, and snippets.

Embed
What would you like to do?
python Class for communicating Rigol Dp832 programmable power supply
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 21 13:57:32 2013
@author: bjuluri
Uses Pyvisa for commounication
"""
from visa import *
import time, sys
class Rigoldp832():
def __init__(self, usb_serial):
try:
self.instrument_list = get_instruments_list()
self.usb_address = [elem for elem in self.instrument_list if (elem.find('USB') != -1 and elem.find(
usb_serial) != -1)] # Search a instrument with USB and serial number in the instrument list
if self.usb_address == [] :
self.status = "Not Connected"
elif self.usb_address != []:
self.s = instrument(self.usb_address[0])
self.status = "Connected"
self.connected_with = 'USB'
except VisaIOError:
self.status = "Not Connected"
print "Pyvisa is not able to find the connections"
def mywrite(self, message):
self.s.write(message)
time.sleep(0.1)
error = self.s.ask('SYST:ERR?')
if error[0]:
print message+' recieved. No error occured'
else :
print message+' recieved. An Error occured: '+ str(error)
def reset(self):
"""resets the instrument, registers,buffers"""
self.mywrite("*RST")
time.sleep(0.2)
def close_instrument(self):
self.s.close()
def set_bias(self, channel=1, i=0.1, i_protection_level=0.1, v=0.5):
self.mywrite(':INST CH{channel}'.format(channel=int(channel)))
self.mywrite(':CURR {i}'.format(i = i))
self.mywrite(':CURR:PROT {i_level}'.format(i_level = i_protection_level))
self.mywrite(':CURR:PROT:STAT ON')
self.mywrite(':VOLT {0}'.format(v))
def turn_off(self, channel = 1):
self.mywrite(':OUTP CH{channel},OFF'.format(channel = channel))
def turn_on(self, channel = 1):
self.mywrite(':OUTP CH{channel},ON'.format(channel = channel))
def console(self):
"""
opens a console to send commands. See the commands in the user manual.
"""
cmd = ''
while cmd != 'exit()':
cmd = raw_input('Rigol DP832 console, Type exit() to leave> ')
if cmd.find('?') >= 0:
answer = self.s.ask(cmd)
print answer
elif cmd.find('?') < 0 and cmd != 'exit()':
self.s.write(cmd)
else:
print "Exiting the Rigol DP832 console"
if __name__ == "__main__":
r = Rigoldp832(usb_serial='your_Serial_number')
#To start a console
#r.console()
# This will reset the instrument, apply a bias of 1 V wih 0.1A of protection current for 10 sec and then turn off
r.reset()
r.set_bias(channel=1, i=0.1, i_protection_level=0.2, v=1)
r.turn_on(channel=1)
time.sleep(10)
r.turn_off(channel=1)
r.close_instrument()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment