def connect(self): 'initializes the hardware. Instrument Name is created here' if dbg: print "inside connect" self.__AcquireMutex__() try: self.connectcount += 1 if self.Device: debug( "connecting to the same instrument using the same instance too many times.... Count : %s InstanceOf: %s" % (self.connectcount, self.pid)) _warn( "connecting to the same instrument using the same instance too many times.... Count : %s InstanceOf: %s" % (self.connectcount, self.pid)) return None if (self.__class__.__sDevicedict[self.key] != None): self.Device = self.__class__.__sDevicedict[self.key] self.device = None #self.Device #legacy support <dropping all legacy support) self.disconnected = 0 self.__class__.__sConnectCntr[self.key] += 1 if dbg: print "Instrument already connected", self.Device return None self.__class__.__sDevicedict[self.key] = self.__initHW() self.Device = self.__class__.__sDevicedict[self.key] self.device = None #self.Device #legacy support <dropping all legacy support) self.__class__.__sConnectCntr[ self.key] = 1 #this will have to be the first connect self.disconnected = 0 finally: self.__ReleaseMutex__() if dbg: print "inst connected (new)"
def __del__( self ): #disconnect the instrument if it goes outof scope even if no explicit command is given 'disconnect the instrument if it goes outof scope even if no explicit command is given' if self.__class__.__sDevicedict[self.key] != None: if not self.disconnected: _warn("Instrument instance of %s Not disconnected explicitly" % self.pid) time.sleep(5) self.disconnect() if dbg: print "disconnected..." self.__class__.__sCounter[self.key] -= 1 if not self.__class__.__sCounter[self.key]: print "Destroying Mutex :", self.key debug(DestroyMutex=self.key) win32api.CloseHandle(self.__class__.__sMutex[self.key]) del self.__class__.__sDevicedict[self.key]
def poll(self): self.__AcquireMutex__() try: if self.__Type == 0: count = 0 while (not (ibrsp(self.Device) & (1 << 4))): time.sleep(0.1) if (count > 50): raise GPIBTimeout, "Instrument Busy" count += 1 elif self.__Type == 1: s = None _warn("supported only for gpib") elif self.__Type == 2: s = None _warn("supported only for gpib") finally: self.__ReleaseMutex__()
def reset(self): self.__AcquireMutex__() if self.__Type == 0: ibclr(self.Device) cmd = '*RST' ibwrt(self.Device, cmd, len(cmd)) self.__ReleaseMutex__() s = None elif self.__Type == 1: self.Device.write('*RST\n') s = None self.__ReleaseMutex__() #_warn("supported only for gpib") elif self.__Type == 2: s = None self.__ReleaseMutex__() _warn("supported only for gpib") return s
def identity(self): self.__AcquireMutex__() try: if self.__Type == 0: ibclr(self.Device) cmd = '*IDN?' ibwrt(self.Device, cmd, len(cmd)) # Remove the newline. s = ibrd(self.Device, 255)[:-2] self.__Name = s elif self.__Type == 1: s = None _warn("supported only for gpib") elif self.__Type == 2: s = None _warn("supported only for gpib") return s finally: self.__ReleaseMutex__()
def disconnect(self): # Take the device offline. if dbg: print "inside disconnect" if self.disconnected: debug( "Disconnecting instrument that has been already disconnected: Instance of :" + self.pid) _warn( "Disconnecting instrument that has been already disconnected: Instance of :" + self.pid) if dbg: print "Already Disconnected" return self.disconnected = 1 self.connectcount -= 1 if dbg: print "Type :", self.__Type, self.__class__.__sCounter[ self.key], self.__class__.__sConnectCntr[self.key] self.__class__.__sConnectCntr[self.key] -= 1 if not self.__class__.__sConnectCntr[self.key] == 1: if self.__Type == 0: self.__closeHW() self.device = self.Device = None #legacy support self.__class__.__sDevicedict[self.key] = None # del self.__class__.__sMutex[self.key] if dbg: print self.key, " disconnected" elif self.__Type == 1: self.__closeHW() self.device = self.Device = None self.__class__.__sDevicedict[self.key] = None elif self.__Type == 2: self.__class__.__sDevicedict[self.key].close() self.__class__.__sDevicedict[self.key] = None self.device = self.Device = None #legacy support ## self.disconnected = 1 else: if self.Device: pass #self.__class__.__sCounter[self.key] -=1 else: debug("disconnect attempted without connection") self.device = self.Device = None #legacy support if dbg: print self.key, " count :", self.__class__.__sCounter[self.key]