Exemplo n.º 1
0
    def _read_spectrum_data(self):
        usb_speed = self._usb_speed
        # Only query the value if there is no cached value
        if usb_speed is None:
            usb_speed = self.usb_speed

        n_packets, packet_length = (8, 512) if usb_speed == 'high' else (64,
                                                                         64)
        # Assign endpoint
        vpp43.set_attribute(self._vi, VI_ATTR_USB_BULK_IN_PIPE, 0x82)

        data = ''
        for i in range(n_packets):
            data += vpp43.read(self._vi, packet_length)

        # Read sync packet to check if properly synchronized
        sync_byte = vpp43.read(self._vi, 1)
        if sync_byte != '\x69' or len(data) != 4096:
            raise visa.VisaIOError(OO_ERROR_SYNC)

        # Reassign endpoint
        vpp43.set_attribute(self._vi, VI_ATTR_USB_BULK_IN_PIPE, 0x81)

        spectrum = N.fromstring(data, N.dtype('<u2'))

        # Query and cache the saturation level if it is not cached
        if self._saturation_level is None:
            vpp43.write(self._vi, '\x05\x11')
            autonull_info = vpp43.read(self._vi, 17)
            self._saturation_level = (
                65536.0 / struct.unpack('<H', autonull_info[6:8])[0])
            # Hmmm, it seems that this is an OceanOptics trick to sell
            # spectrometers with much less dynamic range than advertised!

        return spectrum * self._saturation_level
Exemplo n.º 2
0
    def _read_spectrum_data(self):

        # Assign endpoint
        vpp43.set_attribute(self._vi, VI_ATTR_USB_BULK_IN_PIPE, 0x82)

        data = ''
        for i in range(32):
            lsbs = vpp43.read(self._vi, 64)
            msbs = vpp43.read(self._vi, 64)

            # Mask MSB high bits - instrument has 12-bit A/D
            msbs = ''.join([chr(ord(byte) & 0x0F) for byte in msbs])

            bs = bytearray(128)
            bs[::2] = lsbs
            bs[1::2] = msbs
            data += str(bs)

        # Read sync packet to check if properly synchronized
        sync_byte = vpp43.read(self._vi, 1)
        if sync_byte != '\x69' or len(data) != 4096:
            raise visa.VisaIOError(OO_ERROR_SYNC)

        # Reassign endpoint
        vpp43.set_attribute(self._vi, VI_ATTR_USB_BULK_IN_PIPE, 0x87)

        return N.fromstring(data, N.int16)
Exemplo n.º 3
0
    def check_error(self, ret_code):
        """
        Default error checking, raises an error if return code !=0.

        Does not differentiate between warnings or specific error messages.
        Override this function in your driver if you want to add specific
        error messages.

        Args:
            ret_code (int): A Visa error code. See eg:
                https://github.com/hgrecco/pyvisa/blob/master/pyvisa/errors.py

        Raises:
            visa.VisaIOError: if ``ret_code`` indicates a communication
                problem.
        """
        if ret_code != 0:
            raise visa.VisaIOError(ret_code)
Exemplo n.º 4
0
def autodetect_spectrometer(resource_name, *args, **kwargs):
    """
    Factory method which creates an appropriate instrument object, depending
    on the model code that the unit identifies itself with.
    """
    vi = vpp43.open(visa.resource_manager.session, resource_name)
    model_code = vpp43.get_attribute(vi, vpp43.VI_ATTR_MODEL_CODE)
    vpp43.close(vi)

    if model_code == 4098:
        return USB2000(resource_name, *args, **kwargs)
    if model_code == 4100:
        return ADC1000(resource_name, *args, **kwargs)
    if model_code == 4106:
        return HR2000(resource_name, *args, **kwargs)
    if model_code == 4114:
        return HR4000(resource_name, *args, **kwargs)
    if model_code == 4118:
        return HR2000Plus(resource_name, *args, **kwargs)
    if model_code == 4120:
        return QE65000(resource_name, *args, **kwargs)
    if model_code == 4126:
        return USB2000Plus(resource_name, *args, **kwargs)
    if model_code == 4130:
        return USB4000(resource_name, *args, **kwargs)
    if model_code == 4134:
        return NIRQuest512(resource_name, *args, **kwargs)
    if model_code == 4136:
        return NIRQuest256(resource_name, *args, **kwargs)
    if model_code == 4138:
        return MayaPro(resource_name, *args, **kwargs)
    if model_code == 4140:
        return Maya(resource_name, *args, **kwargs)
    if model_code == 4160:
        return Torus(resource_name, *args, **kwargs)

    raise visa.VisaIOError(OO_ERROR_MODEL_NOT_FOUND)
Exemplo n.º 5
0
    def __init__(self,
                 resource=None,
                 sim_mode=False,
                 backend="@py",
                 query='?*::INSTR',
                 name=None,
                 path='../'):

        if sim_mode:
            # Create a simulated instrument
            self._resource = 'SimVISA'
            self._inst = SimVISA()

        else:
            # Help to find resource
            if resource is None:
                rm = visa.ResourceManager(backend)
                available = rm.list_resources_info(query=query)

                # Filter results with the (optional) name provided
                if name is not None:
                    available = {
                        k: v
                        for k, v in available.items()
                        if v.resource_name == name
                    }

                # Generate a list with results
                if len(available) >= 1:
                    resources = list()
                    options = list()
                    for i, item in enumerate(available):
                        resources.append(item)
                        rsc_name = available[item].resource_name
                        option = '%s\n    (%s)' % (item, rsc_name)
                        options.append(option)

                    # Make a list for multiples values
                    if len(available) > 1:
                        message = "Available devices:\n"
                        number = PT.select_prompt(options, message)
                        self._resource = resources[number]

                    # Or autoselect
                    else:
                        self._resource = resources[0]

                # Raise exception for negative query results
                else:
                    raise (visa.VisaIOError(-1073807343))

            # Use the resource selected
            else:
                self._resource = resource

            # Open resource
            self._inst = rm.open_resource(self._resource)

        self._name = self._inst.query('*IDN?')

        self._fullname = '%s/%.16s' % (path, name)
        self._path = path

        self._log = '%s.log' % self._fullname
        self._temp_list = list()

        with open(self._log, 'a') as f:
            f.write('\n' + '#' * 40 + '\n')
            f.write('# %-36s #\n' % time.strftime('%x - %X'))
            f.write('# %-36s #\n' % 'Session started')
            f.write('# %-36s #\n' % self._resource)
            f.write('# %-36s #\n' % self._name)
            f.write('#' * 40 + '\n')
Exemplo n.º 6
0
    def __init__(self, resource=None, sim_mode=False, backend="@py",
                 query='?*::INSTR', name=None, path='./'):

        if sim_mode:
            # Create a simulated instrument
            self._resource = 'SimVISA'
            self._inst = SimVISA()

        else:
            rm = visa.ResourceManager(backend)
            # Help to find resource
            if resource is None:

                available = rm.list_resources(query=query)

                # Filter results with the (optional) name provided
                if name is not None:
                    available = {k: v for k, v in available.items()
                                 if v.resource_name == name}

                # Generate a list with results
                if len(available) >= 1:
                    resources = list()
                    options = list()
                    for i, item in enumerate(available):
                        resources.append(item)
                        option = '{:s}'.format(item)
                        options.append(option)

                    # Make a list for multiples values
                    if len(available) > 1:
                        message = "Available devices:\n"
                        number = PT.select_prompt(options, message)
                        self._resource = resources[number]

                    # Or autoselect
                    else:
                        self._resource = resources[0]

                # Raise exception for negative query results
                else:
                    raise(visa.VisaIOError(-1073807343))

            # Use the resource selected
            else:
                self._resource = resource

            # Open resource
            self._inst = rm.open_resource(self._resource)

        self._idn = self._inst.query('*IDN?')
        self._name = self._idn.split(',')[0] + '-' + self._idn.split(',')[1]

        self._fullname = '{}/{}'.format(path, self._name)
        self._path = path
        self._path_data = path + '/data/temp.npy'

        self._log = LT('{}.log'.format(self._fullname))
        self._temp_list = list()

        self._log.block(time.strftime('%x - %X'),
                        'Session started',
                        self._resource,
                        self._name)