Source code for PyICe.lab_interfaces

'''
Physical Communication Interfaces Hierarchy Manager
===================================================

Required for multithreaded communication.
'''

import sys, abc

try:
    import visa
    visaMissing = False
except:
    visaMissing = True
try:
    import serial   
    serialMissing = False
except:
    serialMissing = True

try:
    from deps.RL1009 import RL1009_SERIAL_CLASS
    RL1009SerialMissing = False
except:
    RL1009SerialMissing = True
try:
    from deps.RL1009 import RL1009_DLL_INTERFACE
    RL1009DLLMissing = False
except:
    RL1009DLLMissing = True
try:
    import deps.vxi11.vxi11 as vxi11
    vxi11Missing = False
except:
    vxi11Missing = True

try:
    import telnetlib
    telnetlibMissing = False
except:
    telnetlibMissing = True
    
import twoWireInterface
import spi_interface

#visa wrappers should probably go away and get merged in here
import visa_wrappers
import multiprocessing

try:
    import usb.core
    ubsMissing = False
except:
    ubsMissing = True

[docs]class communication_node(object): '''The purpose of this is to map a network of communication resources to each channel''' def __init__(self): '''this should never take arguments since it is silently created many ways''' self._parent = None self._thread_safe = False self._children = [] self._lock = multiprocessing.RLock() def get_com_parent(self): return self._parent def set_com_node_parent(self,parent): if self._parent: print "warning: changing a communication_node parent" self._parent = parent self._parent.com_node_register_child(self) def set_com_node_thread_safe(self, safe=True): self._thread_safe = safe def com_node_register_child(self,child): self._children.append(child) def com_node_get_root(self): if self._parent: return self._parent.com_node_get_root() else: return self def com_node_get_children(self): return self._children def com_node_get_all_descendents(self): descendents = set() for child in self.com_node_get_children(): descendents.add(child) descendents = descendents.union( child.com_node_get_all_descendents() ) return descendents def group_com_nodes_for_threads(self,sets=None): #returns a list of sets of com_nodes, each set must be communicated with in 1 thread # because upstream interfaces are not thread safe if sets==None: sets = list() if self._thread_safe: sets.append( set([self]) ) for child in self.com_node_get_children(): child.group_com_nodes_for_threads(sets) #will modify sets else: group = self.com_node_get_all_descendents() group.add(self) sets.append( group ) return sets def group_com_nodes_for_threads_filter(self,com_node_list): #takes a list of interfaces and returns a list of lists # the returned list are interfaces that cannot be used concurrently # each is an ideal candidate for a thread to handle #make sure all interface's root resolves back here if len(set([interface.com_node_get_root() for interface in com_node_list])) > 1: print 'Something is wrong, either:' print ' 1. you did not get all your interfaces from the same master or interface_factory' print ' 2. you did not ask for threads from the root node' print ' 3. you are working on the interface library and broke something' raise Exception("Something is very wrong... {}".format(interface)) #get a list of lists for all interfaces in_sets = self.group_com_nodes_for_threads() out_sets = [] for in_set in in_sets: out_group = [] for item in in_set: if item in com_node_list: out_group.append(item) if len(out_group): out_sets.append(out_group) return out_sets def lock(self): self._lock.acquire() def unlock(self): self._lock.release()
'''all communication is through one of these distinct interface classes interface_visa (visa like communication regardless of physical interface) interface_twi (i2c,smbus) interface_raw_serial (raw, low level serial) this probably covers most anything, new ones should only be added if the communication model doesn't fit any of these (SPI for example) ''' ''' this group of interface_* classes should not be directly created, create them only with the factory '''
[docs]class interface(communication_node): '''base class all lab instruments should in some way talk to, all have a timeout whether or not it does anything''' def __init__(self,name): self._interface_name = name #cannot use hasattr since its sometimes a property if "timeout" not in dir(self): self.timeout = None communication_node.__init__(self) def __str__(self): return self._interface_name
class interface_visa(interface): pass class interface_twi(interface): pass class interface_spi(interface): pass
[docs]class interface_libusb(interface): '''Bulk transfer through LibUSB/WinUSB. Implementation may be overly specific to George B's Direct590 protocol and may need additional options or subclassing later. Transfers must be in multiples of this 64 byte payload size or will result in a babble error in the underlying library. Requires PyUSB: https://github.com/walac/pyusb''' def __init__(self, idVendor, idProduct, timeout): '''PyUSB requires installation of WinUSB filter driver. Use install-filter-win.exe under PyICe/deps/Direct590. Untested on linux; filter driver probably not required. ''' interface.__init__(self, 'WinUSB Communication Interface') self.timeout = 1000 * timeout #ms #https://github.com/walac/pyusb/blob/master/docs/tutorial.rst # find our device self.dev = usb.core.find(idVendor=idVendor, idProduct=idProduct) # was it found? if self.dev is None: raise ValueError('LibUSB Device not found. Is filter driver installed? (see docstring)') # set the active configuration. With no arguments, the first # configuration will be the active one self.dev.set_configuration() # get an endpoint instance self.cfg = self.dev.get_active_configuration() self.intf = self.cfg[(0,0)] self.ep_out = usb.util.find_descriptor( self.intf, # match the first OUT endpoint custom_match = \ lambda e: \ usb.util.endpoint_direction(e.bEndpointAddress) == \ usb.util.ENDPOINT_OUT ) assert self.ep_out is not None self.ep_in = usb.util.find_descriptor( self.intf, # match the first OUT endpoint custom_match = \ lambda e: \ usb.util.endpoint_direction(e.bEndpointAddress) == \ usb.util.ENDPOINT_IN ) assert self.ep_in is not None #self.stream_in = '' # a.dev.configurations()[0].interfaces()[0].endpoints()[0].wMaxPacketSize self.write_packet_size = self.ep_out.wMaxPacketSize self.read_packet_size = self.ep_in.wMaxPacketSize print self.dev def read(self): '''''' resp = self.dev.read(self.ep_in, self.read_packet_size, self.timeout) while len(resp) == self.read_packet_size: #response split across packets resp += self.dev.read(self.ep_in, self.read_packet_size, self.timeout) return resp.tostring() #(resp,remain)
[docs] def write(self, byte_list): '''Send byte_list across subclass-specific communication interface.''' self.dev.write(self.ep_out, byte_list)
[docs]class interface_stream(interface): #(lab_interfaces.interface) '''Generic parent class of all stream-type interfaces. Developed for DC590 board variants, but probably has more generic utility if moved into lab_interfaces Maybe consider change to inherit from https://docs.python.org/2/library/io.html ''' __metaclass__ = abc.ABCMeta @abc.abstractmethod
[docs] def read(self, byte_count): '''Read and return tuple (byte_count bytes, byte_count remaining_bytes) from subclass-specific communication interface. If fewer than byte_count bytes are available, return all available. ''' pass
@abc.abstractmethod
[docs] def write(self, byte_list): '''Send byte_list across subclass-specific communication interface.''' pass
@abc.abstractmethod
[docs] def close(self): '''close the underlying interface if necessary''' pass
[docs]class interface_stream_serial(interface_stream): '''PySerial based stream wrapper.''' def __init__(self,interface_raw_serial): interface.__init__(self, 'Serial Stream Communication Interface') self.ser = interface_raw_serial
[docs] def read(self, byte_count): '''Read and return tuple (byte_count bytes, byte_count remaining_bytes) from subclass-specific communication interface. If fewer than byte_count bytes are available, return all available. If byte_count is None, return all available. ''' if byte_count is None: byte_count = self.ser.inWaiting() resp = self.ser.read(byte_count) remain = self.ser.inWaiting() return (resp,remain)
[docs] def write(self, byte_list): '''Send byte_list across subclass-specific communication interface.''' self.ser.write(byte_list)
[docs] def close(self): '''close the underlying interface''' self.ser.close()
[docs]class interface_ftdi_d2xx(interface_stream): '''write this if you want it https://pypi.python.org/pypi/pylibftdi''' def __init__(self): #need some kind of device descriptor.... interface.__init__(self, 'FTDI D2XX Stream Communication Interface')
class interface_raw_serial(interface,serial.Serial): def __init__(self,serial_port_name,baudrate,timeout,**kwargs): serial.Serial.__init__(self,serial_port_name,**kwargs) interface.__init__(self,'interface_raw_serial @ {}'.format(serial_port_name) ) if baudrate: self.baudrate = baudrate if timeout: self.timeout = timeout self._serial_port_name = serial_port_name def get_serial_port_name(self): return self._serial_port_name def __del__(self): '''Close interface (serial) port on exit''' self.close() super(interface_raw_serial,self).__del__() '''below are specific classes that inherit fro the above general classes''' class interface_visa_tcp_ip(interface_visa,visa_wrappers.visa_wrapper_tcp): def __init__(self,host_address,port,timeout): visa_wrappers.visa_wrapper_tcp.__init__(self,host_address,port,timeout) interface_visa.__init__(self, "interface_visa_tcp_ip @ {}:{}".format(host_address,port)) class interface_visa_telnet(interface_visa,visa_wrappers.visa_wrapper_telnet): def __init__(self,host_address,port,timeout): visa_wrappers.visa_wrapper_telnet.__init__(self,host_address,port,timeout) interface_visa.__init__(self, "interface_visa_telnet @ {}:{}".format(host_address,port)) class interface_visa_serial(interface_visa,visa_wrappers.visa_wrapper_serial): def __init__(self,interface_raw_serial_object): visa_wrappers.visa_wrapper_serial.__init__(self,interface_raw_serial_object) interface_visa.__init__(self,'interface_visa_serial @ {}'.format(interface_raw_serial_object)) class interface_visa_vxi11(interface_visa,visa_wrappers.visa_wrapper_vxi11): def __init__(self,address,timeout): visa_wrappers.visa_wrapper_vxi11.__init__(self, address, timeout) interface_visa.__init__(self, 'interface_visa_vxi11 @ {}'.format(address) ) class interface_visa_direct(interface_visa,visa_wrappers.visa_interface): def __init__(self, visa_address_string, timeout): visa_wrappers.visa_interface.__init__(self,visa_address_string,timeout) interface_visa.__init__(self,'interface_visa_direct @ {}'.format(visa_address_string) ) self.visa_address_string = visa_address_string class interface_visa_rl1009_dll(interface_visa, visa_wrappers.rl1009_visa_wrapper_dll): def __init__(self,GPIBAddress, timeout): visa_wrappers.rl1009_visa_wrapper_dll.__init__(self,GPIBAddress, timeout) interface_visa.__init__(self,'interface_visa_rl1009_dll @ GPIB {}'.format(GPIBAddress) ) class interface_visa_rl1009_serial(interface_visa,visa_wrappers.rl1009_visa_wrapper_serial): def __init__(self,GPIBAddress, timeout, serialPort=None): visa_wrappers.rl1009_visa_wrapper_serial.__init__(self,GPIBAddress, timeout, serialPort) interface_visa.__init__(self,'interface_visa_rl1009_serial @ GPIB {} @ rl1009 @ {}'.format(GPIBAddress,serialPort)) def __getTimeout(self): return self.get_timeout() def __setTimeout(self, timeout): return self.set_timeout(timeout) timeout = property(__getTimeout,__setTimeout) class interface_visa_keithley_kxci(interface_visa,visa_wrappers.visa_wrapper_keithley_kxci): def __init__(self,host_address,port): visa_wrappers.visa_wrapper_keithley_kxci.__init__(self,host_address,port) interface_visa.__init__(self, "interface_visa_keithley_kxci @ {}:{}".format(host_address,port)) # class interface_xbee_serial(interface_visa,visa_wrappers.visa_wrapper_xbee): # def __init__(self,interface_raw_serial_object): # visa_wrappers.visa_wrapper_serial.__init__(self,interface_raw_serial_object) # interface_visa.__init__(self,'interface_visa_xbee @ {}'.format(interface_raw_serial_object) ) '''below are the specific cases of the twi interface''' class interface_twi_dummy(interface_twi, twoWireInterface.i2c_dummy): def __init__(self,delay): twoWireInterface.i2c_dummy.__init__(self,delay) interface_twi.__init__(self,'interface_twi_dummy @ fake') class interface_twi_scpi(interface_twi, twoWireInterface.i2c_scpi): def __init__(self, interface_serial, timeout): twoWireInterface.i2c_scpi.__init__(self, interface_serial) interface_twi.__init__(self,'interface_twi_scpi @ {}'.format(interface_serial)) self.timeout = timeout class interface_twi_scpi_sp(interface_twi, twoWireInterface.i2c_scpi_sp): def __init__(self, interface_serial, portnum, sclpin, sdapin, pullup_en=False, timeout=1): twoWireInterface.i2c_scpi_sp.__init__(self, interface_serial, portnum, sclpin, sdapin, pullup_en) interface_twi.__init__(self,'interface_twi_scpi @ {}'.format(interface_serial)) self.timeout = timeout class interface_twi_scpi_testhook(interface_twi, twoWireInterface.i2c_scpi_testhook): def __init__(self, interface_serial, timeout): twoWireInterface.i2c_scpi.__init__(self, interface_serial) interface_twi.__init__(self,'interface_twi_scpi @ {}'.format(interface_serial)) self.timeout = timeout class interface_twi_dc590_serial(interface_twi, twoWireInterface.i2c_dc590): #DJS TODO: fix interfaces to reconcile with DC590 cleanup def __init__(self, interface_serial, timeout): twoWireInterface.i2c_dc590.__init__(self, interface_serial) #DJS TODO: fix interfaces to reconcile with DC590 cleanup interface_twi.__init__(self,'interface_twi_dc590_serial @ {}'.format(interface_serial)) self.timeout = timeout class interface_twi_dc590listread_serial(interface_twi, twoWireInterface.i2c_dc590_list_read): #DJS TODO: fix interfaces to reconcile with DC590 cleanup def __init__(self, interface_serial, timeout): twoWireInterface.i2c_dc590_list_read.__init__(self, interface_serial) #DJS TODO: fix interfaces to reconcile with DC590 cleanup interface_twi.__init__(self,'interface_twi_dc590listread_serial @ {}'.format(interface_serial)) self.timeout = timeout class interface_twi_buspirate(interface_twi, twoWireInterface.i2c_buspirate): def __init__(self, interface_serial, timeout): twoWireInterface.i2c_buspirate.__init__(self, interface_serial) interface_twi.__init__(self,'interface_twi_buspirate @ {}'.format(interface_serial)) self.timeout = timeout class interface_twi_kernel(interface_twi, twoWireInterface.i2c_kernel): def __init__(self, bus_number): twoWireInterface.i2c_kernel.__init__(self, bus_number) interface_twi.__init__(self,'interface_twi_kernel @ {}'.format(bus_number)) '''SPI interfaces''' class interface_spi_dummy(interface_spi, spi_interface.spi_dummy): def __init__(self, delay=0): spi_interface.spi_dummy.__init__(self, delay) interface_spi.__init__(self, 'interface_spi_dummy @ fake') class interface_spi_dc590(interface_spi, spi_interface.spi_dc590): def __init__(self, interface_stream, ss_ctrl=None): spi_interface.spi_dc590.__init__(self, interface_stream, ss_ctrl) interface_spi.__init__(self, 'interface_spi_dc590 @ {}'.format(interface_stream)) class interface_spi_cfgpro(interface_spi, spi_interface.spi_cfgpro): def __init__(self, visa_interface, CPOL, CPHA, baudrate=1e6, ss_ctrl=None): spi_interface.spi_cfgpro.__init__(self, visa_interface, CPOL, CPHA, baudrate, ss_ctrl) interface_spi.__init__(self, 'interface_spi_cfgpro @ {}'.format(visa_interface)) #TODO: #Missing beaglebone #Missing buspirate class gpib_adapter(communication_node): pass class gpib_adapter_visa(gpib_adapter): pass class gpib_adapter_rl1009_dll(gpib_adapter): pass class gpib_adapter_rl1009_serial(gpib_adapter): def __init__(self, serialPort): gpib_adapter.__init__(self) self.serial_port = serialPort def get_serial_port(self): return self.serial_port
[docs]class interface_factory(communication_node): '''the goal of the interface factory is both to create interfaces it also creates the instruments with communication_node, that they pass on to channels The purpose of this class is to be smart about the interfaces and to simplify their creation. It's purpose is also to encourage portable code, and to remove some low level responsibilities from the instruments. This also forces you to talk to gpib instruments in a way that works with both visa library and non-visa adapters. ''' _instantiated = False def __init__(self): communication_node.__init__(self) if self._instantiated == True: raise Exception("You should only create one of these") self._instantiated = True self.set_com_node_thread_safe() #since there is only one visa if not visaMissing: self._visa_root = communication_node() self._visa_root.set_com_node_parent(self) self._visa_root.set_com_node_thread_safe() #visa is thread safe self._gpib_adapters = {} # communication_node indexed by adapter_number self._gpib_interfaces = {} #indexed by adapter number and address self._raw_serial_interfaces = [] self._direct_visa_interfaces = [] self._vxi11_interfaces = [] self._visa_serial_interfaces = [] self._tcp_ip_interfaces = [] self._telnet_interfaces = [] self._keithley_kxci_interfaces = [] #self._xbee_serial_interfaces = [] self._default_timeout = 2 def get_visa_interface(self,visa_address_string,timeout=None): if visaMissing: raise Exception("pyVisa or VISA is missing on this computer, install one or both") if visa_address_string.lower().startswith('gpib'): print '\n\n\n\n\nDo not add gpib over visa directly for compatability with non visa framwork iterfaces' print ' for example replace: ' print ' .create_visa_interface("GBPIB0::5")' print ' with:' print ' .set_gpib_adapter_visa(0) # this can only be done once for 0' print ' .create_visa_gpib_interface(gpib_adapter_number=0,gpib_address_number=5)' raise Exception('User Error - see above text') timeout = self._set_timeout(timeout) new_interface = interface_visa_direct(visa_address_string,timeout) new_interface.set_com_node_parent(self._visa_root) self._direct_visa_interfaces.append(new_interface) return new_interface def get_visa_gpib_interface(self,gpib_adapter_number,gpib_address_number,timeout=None): timeout = self._set_timeout(timeout) if gpib_adapter_number not in self._gpib_adapters.keys(): print '\n\n\n\nAdapter number "{}" not found in adapter list'.format(gpib_adapter_number) print 'It must be added first with .set_gpib_adapter_*({},*)'.format(gpib_adapter_number) print ' for example:' print ' .set_gpib_adapter_visa({})'.format(gpib_adapter_number) print ' or' print ' .set_gpib_adapter_rl1009({},"COM2")'.format(gpib_adapter_number) raise Exception('Using undefined gpib adapter') #search for an existing gpib_interface if gpib_adapter_number in self._gpib_interfaces: if gpib_address_number in self._gpib_interfaces[gpib_adapter_number]: interface = self._gpib_interfaces[gpib_adapter_number][gpib_address_number] if timeout > interface.timeout: interface.timeout = timeout return interface #determine the type of gpib adapter this_gpib_adapter = self._gpib_adapters[gpib_adapter_number] assert isinstance(this_gpib_adapter, gpib_adapter) if isinstance(this_gpib_adapter,gpib_adapter_rl1009_dll): new_interface = interface_visa_rl1009_dll(gpib_address_number, timeout=timeout) elif isinstance(this_gpib_adapter,gpib_adapter_rl1009_serial): new_interface = interface_visa_rl1009_serial(gpib_address_number, timeout=timeout, serialPort=this_gpib_adapter.get_serial_port()) elif isinstance(this_gpib_adapter,gpib_adapter_visa): visa_address_string = "GPIB{}::{}".format(gpib_adapter_number,gpib_address_number) new_interface = interface_visa_direct(visa_address_string,timeout) else: raise Exception("There is something very wrong...") new_interface.set_com_node_parent(this_gpib_adapter) self._gpib_interfaces[gpib_adapter_number][gpib_address_number] = new_interface return new_interface def get_visa_tcp_ip_interface(self,host_address,port,timeout=None): new_interface = interface_visa_tcp_ip(host_address,port,timeout) new_interface.set_com_node_parent(self) self._tcp_ip_interfaces.append(new_interface) return new_interface def get_visa_telnet_interface(self,host_address,port,timeout=None): if telnetlibMissing: raise Exception("telnetlib is missing on this computer") new_interface = interface_visa_telnet(host_address,port,timeout) new_interface.set_com_node_parent(self) self._telnet_interfaces.append(new_interface) return new_interface def get_visa_vxi11_interface(self,address,timeout): timeout = self._set_timeout(timeout) new_interface = interface_visa_vxi11(address,timeout) new_interface.set_com_node_parent(self) self._vxi11_interfaces.append(new_interface) return new_interface def get_visa_serial_interface(self,serial_port_name,baudrate=None,timeout=None,**kwargs): timeout = self._set_timeout(timeout) serial = self.get_raw_serial_interface(serial_port_name,baudrate,timeout,**kwargs) new_interface = interface_visa_serial(serial) new_interface.set_com_node_parent(serial) self._visa_serial_interfaces.append(new_interface) return new_interface def get_visa_keithley_kxci_interface(self,host_address,port): new_interface = interface_visa_keithley_kxci(host_address,port) new_interface.set_com_node_parent(self) self._keithley_kxci_interfaces.append(new_interface) return new_interface # def get_xbee_local_interface(self,serial_port_name,baudrate=None,timeout=None,**kwargs): # timeout = self._set_timeout(timeout) # serial = self.get_raw_serial_interface(serial_port_name,baudrate,timeout,**kwargs) # new_interface = interface_xbee_serial(serial) # new_interface.set_com_node_parent(serial) # self._xbee_serial_interfaces.append(new_interface) # return new_interface def get_raw_serial_interface(self,serial_port_name,baudrate=None,timeout=None,**kwargs): if serialMissing: raise Exception("pySerial is missing on this computer") timeout = self._set_timeout(timeout) for interface in self._raw_serial_interfaces: if interface.get_serial_port_name() == serial_port_name: if (baudrate != interface.baudrate) and (baudrate is not None): raise Exception("Tried to create a second connection to serial port {} with different baudrate".format(serial_port_name)) if timeout > interface.timeout: interface.timeout = timeout #auto extend time outs return interface new_interface = interface_raw_serial(serial_port_name,baudrate,timeout,**kwargs) new_interface.set_com_node_parent(self) #serial ports as we use them are not thread safe and never will be self._raw_serial_interfaces.append(new_interface) return new_interface def get_interface_libusb(self, idVendor=0x1272, idProduct=0x8004, timeout = 1): new_interface = interface_libusb(idVendor=0x1272, idProduct=0x8004, timeout = 1) new_interface.set_com_node_parent(self) return new_interface def get_interface_stream_serial(self, interface_raw_serial): new_interface = interface_stream_serial(interface_raw_serial) new_interface.set_com_node_parent(interface_raw_serial) return new_interface def get_interface_ftdi_d2xx(self): new_interface = interface_ftdi_d2xx() #need some kind of device descriptor.... new_interface.set_com_node_parent(self) return new_interface def get_twi_dummy_interface(self,delay=0,timeout=None): new_interface = interface_twi_dummy(delay) new_interface.set_com_node_parent(self) return new_interface def get_twi_scpi_interface(self,serial_port_name,baudrate=None,timeout=None): serial = self.get_visa_serial_interface(serial_port_name,baudrate,timeout) new_interface = interface_twi_scpi(serial,timeout) new_interface.set_com_node_parent(serial) return new_interface def get_twi_scpi_sp_interface(self,serial_port_name,portnum,sclpin,sdapin,pullup=False,baudrate=None,timeout=None): serial = self.get_visa_serial_interface(serial_port_name,baudrate,timeout) new_interface = interface_twi_scpi_sp(interface_serial=serial,portnum=portnum,sclpin=sclpin,sdapin=sdapin,pullup_en=pullup,timeout=timeout) new_interface.set_com_node_parent(serial) return new_interface def get_twi_scpi_testhook_interface(self,serial_port_name,baudrate=None,timeout=None): serial = self.get_visa_serial_interface(serial_port_name,baudrate,timeout) new_interface = interface_twi_scpi_testhook(serial,timeout) new_interface.set_com_node_parent(serial) return new_interface def get_twi_dc590_serial(self,serial_port_name,baudrate=None,timeout=None): if baudrate is None: baudrate = 115200 #DC590/Linduino default serial = self.get_raw_serial_interface(serial_port_name,baudrate,timeout) stream = self.get_interface_stream_serial(serial) new_interface = interface_twi_dc590_serial(stream,timeout) new_interface.set_com_node_parent(stream) return new_interface def get_twi_dc590listread_serial(self,serial_port_name,baudrate=None,timeout=None): if baudrate is None: baudrate = 115200 #DC590/Linduino default serial = self.get_raw_serial_interface(serial_port_name,baudrate,timeout) stream = self.get_interface_stream_serial(serial) new_interface = interface_twi_dc590listread_serial(stream,timeout) new_interface.set_com_node_parent(stream) return new_interface def get_twi_buspirate_interface(self,serial_port_name,baudrate=None,timeout=None): serial = self.get_raw_serial_interface(serial_port_name,baudrate,timeout) new_interface = interface_twi_buspirate(serial,timeout) new_interface.set_com_node_parent(serial) return new_interface def get_twi_kernel_interface(self,bus_number): new_interface = interface_twi_kernel(bus_number) new_interface.set_com_node_parent(self) return new_interface #SPI Interfaces def get_spi_dummy_interface(self, delay=0): iface_spi = interface_spi_dummy(delay) iface_spi.set_com_node_parent(self) return iface_spi def get_spi_dc590_interface(self, serial_port_name, uart_baudrate=None, uart_timeout=None, ss_ctrl=None, **kwargs): if uart_baudrate is None: uart_baudrate = 115200 #DC590/Linduino default iface_serial = self.get_raw_serial_interface(serial_port_name,baudrate=uart_baudrate,timeout=uart_timeout,**kwargs) iface_stream = self.get_interface_stream_serial(iface_serial) iface_spi = interface_spi_dc590(iface_stream, ss_ctrl) iface_spi.set_com_node_parent(iface_serial) return iface_spi def get_spi_cfgpro_interface(self, serial_port_name, uart_timeout=None, CPOL=0, CPHA=0, spi_baudrate=1e6, ss_ctrl=None): iface_visa_serial = self.get_visa_serial_interface(serial_port_name,timeout=uart_timeout) iface_spi = interface_spi_cfgpro(iface_visa_serial, CPOL, CPHA, spi_baudrate, ss_ctrl) iface_spi.set_com_node_parent(iface_visa_serial) return iface_spi #Missing spi_bbone #Missing spi_buspirate def get_dummy_interface(self,parent=None,name='dummy interface'): #used only for testing the core lab functions new_interface = interface(name) if parent is None: new_interface.set_com_node_parent(self) else: new_interface.set_com_node_parent(parent) return new_interface def set_gpib_adapter_visa(self,adapter_number): if visaMissing: raise Exception("pyVisa or VISA is missing on this computer, install one or both. Cannot use visa for GPIB adapter") if adapter_number in self._gpib_adapters.keys(): if self._gpib_adapters[adapter_number]._parent == self._visa_root: raise Exception("Attempting to re-define gpib adapter: {}, the same way a second time.".format(adapter_number) ) else: raise Exception("GPIB adapter_number {} was already defined as something other than visa.".format(adapter_number) ) gpib_adapter = gpib_adapter_visa() gpib_adapter.set_com_node_thread_safe() gpib_adapter.set_com_node_parent(self._visa_root) self._gpib_adapters[adapter_number] = gpib_adapter if adapter_number not in self._gpib_interfaces: self._gpib_interfaces[adapter_number] = {} def set_gpib_adapter_rl1009(self,adapter_number,serial_port_name=None,baudrate=None,timeout=None): if adapter_number in self._gpib_adapters.keys(): raise Exception("GPIB adapter_number {} was already defined.".format(adapter_number) ) if serial_port_name == None: gpib_adapter = gpib_adapter_rl1009_dll() gpib_adapter.set_com_node_parent(self) #who knows if this is thread safe, doubt it, maybe try it someday else: if baudrate is None: baudrate = 115200 if timeout is None: timeout = 5 serial_port = self.get_raw_serial_interface(serial_port_name,baudrate,timeout) gpib_adapter = gpib_adapter_rl1009_serial(serialPort=serial_port) gpib_adapter.set_com_node_parent(serial_port) #certainly not thread safe self._gpib_adapters[adapter_number] = gpib_adapter if adapter_number not in self._gpib_interfaces: self._gpib_interfaces[adapter_number] = {} def _set_timeout(self,timeout): if timeout == None: return self._default_timeout else: return timeout
if __name__ == "__main__": c_root = communication_node() c_root.set_com_node_thread_safe(True) c_a = communication_node() c_a.set_com_node_thread_safe(False) c_a.set_com_node_parent(c_root) c_b = communication_node() c_b.set_com_node_thread_safe(True) c_b.set_com_node_parent(c_root) c_c = communication_node() c_c.set_com_node_thread_safe(False) c_c.set_com_node_parent(c_root) c_ca = communication_node() c_ca.set_com_node_thread_safe() c_ca.set_com_node_parent(c_c) c_cb = communication_node() c_cb.set_com_node_thread_safe() c_cb.set_com_node_parent(c_c) ''' print c_root.com_node_get_all_descendents() print 'thread groups' for i in c_root.group_com_nodes_for_threads(): print '--------------------' print i ''' print 'filtered' for i in c_root.group_com_nodes_for_threads_filter([c_b,c_a,c_ca,c_cb]): print '--------------------' print i