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
def setPSU(vi, voltage, current): buf = ":volt " + str(voltage) + ";:curr " + str(current) + "\n" vpp43.write(vi, buf) buf = ":apply?\n" vpp43.write(vi, buf) result = vpp43.read(vi, 256) return result
def _send_and_read(self, message): ''' Send <message> to the device and read answer. Raises an error if one occurred Returns a list of bytes Input: message (string) : string conform the IVVI protocol Output: data_out_numbers (int[]) : return message ''' logging.debug('Sending %r', message) # clear input buffer visafunc.read_all(self._vi) vpp43.write(self._vi, message) # In stead of blocking, we could also poll, but it's a bit slower # print visafunc.get_navail(self._vi) # if not visafunc.wait_data(self._vi, 2, 0.5): # logging.error('Failed to receive reply from IVVI rack') # return False data1 = visafunc.readn(self._vi, 2) data1 = [ord(s) for s in data1] # 0 = no error, 32 = watchdog reset if data1[1] not in (0, 32): logging.error('Error while reading: %s', data1) data2 = visafunc.readn(self._vi, data1[0] - 2) data2 = [ord(s) for s in data2] return data1 + data2
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
def _send_and_read(self, message): ''' Performs the communication with the device Raises an error if one occurred Returns a list of bytes Input: message (string) : string conform the IVVI protocol Output: data_out_numbers (int[]) : return message ''' logging.debug(__name__ + ' : do communication with instrument') vpp43.write(self._vi, message) sleep(0.1) data_out_string = vpp43.read( self._vi, vpp43.get_attribute(self._vi, vpp43.VI_ATTR_ASRL_AVAIL_NUM)) sleep(0.1) data_out_numbers = [ord(s) for s in data_out_string] if (data_out_numbers[1] != 0) or (len(data_out_numbers) != data_out_numbers[0]): logging.error(__name__ + ' : Error while reading : %s', data_out_numbers) return data_out_numbers
def open(self): # Open instrument self._vi = vpp43.open(visa.resource_manager.session, self._resource_name) vpp43.set_attribute(self._vi, vpp43.VI_ATTR_TMO_VALUE, self._timeout) # Timeout value should always be higher than integration time # Assign endpoint vpp43.set_attribute(self._vi, VI_ATTR_USB_BULK_IN_PIPE, self._in_pipe) vpp43.set_attribute(self._vi, VI_ATTR_USB_BULK_OUT_PIPE, self._out_pipe) # Initialize vpp43.write(self._vi, '\x01') # Reset command
def __set_frequency(self, value): # example in 488 commands # ibsic # ibsre 1 # ibcmd "?@!" # ibwrt "F0050000000\n" # ibcmd "?" self._frequency = value s = str(int(round(10 * value))).zfill(10) gpib = visa.Gpib(self.board_n) vpp43.gpib_command(gpib.vi, '?@' + self.address_chr) vpp43.write(gpib.vi, 'F' + s + '\n') vpp43.gpib_command(gpib.vi, '?')
def __set_frequency(self, value): # example in 488 commands # ibsic # ibsre 1 # ibcmd "?@!" # ibwrt "F0050000000\n" # ibcmd "?" self._frequency = value s = str(int(round(10*value))).zfill(10) gpib = visa.Gpib(self.board_n) vpp43.gpib_command(gpib.vi, '?@' + self.address_chr) vpp43.write(gpib.vi, 'F' + s + '\n') vpp43.gpib_command(gpib.vi, '?')
def _write_to_instrument(self, tekst): ''' Writes a string to the instrument, after the buffer is cleared Input: tekst (string) : data to be written to the instrument Output: None ''' logging.debug(__name__ + ' : Start running _write_to_instrument with:' + tekst) # clear buffer logging.debug(__name__ + ' : clearing buffer') restbuffer = self._read_buffer() sleep(0.05) if (restbuffer!=''): logging.error(__name__ + ' : Buffer contained unread data : ' + restbuffer) logging.debug(__name__ + ' : writing to vpp43') vpp43.write(self._vi, tekst) sleep(0.05)
def _write_to_instrument(self, tekst): ''' Writes a string to the instrument, after the buffer is cleared Input: tekst (string) : data to be written to the instrument Output: None ''' logging.debug(__name__ + ' : Start running _write_to_instrument with:' + tekst) # clear buffer logging.debug(__name__ + ' : clearing buffer') restbuffer = self._read_buffer() sleep(0.05) if (restbuffer != ''): logging.error(__name__ + ' : Buffer contained unread data : ' + restbuffer) logging.debug(__name__ + ' : writing to vpp43') vpp43.write(self._vi, tekst) sleep(0.05)
def _send_and_read(self, message): ''' Performs the communication with the device Raises an error if one occurred Returns a list of bytes Input: message (string) : string conform the IVVI protocol Output: data_out_numbers (int[]) : return message ''' logging.debug(__name__ + ' : do communication with instrument') vpp43.write(self._vi, message) sleep(0.1) data_out_string = vpp43.read(self._vi, vpp43.get_attribute(self._vi, vpp43.VI_ATTR_ASRL_AVAIL_NUM)) sleep(0.1) data_out_numbers = [ord(s) for s in data_out_string] if (data_out_numbers[1] != 0) or (len(data_out_numbers) != data_out_numbers[0]): logging.error(__name__ + ' : Error while reading : %s', data_out_numbers) return data_out_numbers
def _send_message(self, message): vpp43.write(self._vi, message) sleep(self._sleeptime)
def __set_amplitude(self, value): self._amplitude = value gpib = visa.Gpib(self.board_n) vpp43.gpib_command(gpib.vi, '?@' + self.address_chr) vpp43.write(gpib.vi, 'A' + str(int(-value)) + '\n') vpp43.gpib_command(gpib.vi, '?')
def _query_eeprom(self, configuration_index): vpp43.write(self._vi, '\x05' + chr(configuration_index)) answer = vpp43.read(self._vi, 18) return answer[2:answer.find('\x00', 2)] # from byte 3 to the next null
def _query_status(self): vpp43.write(self._vi, '\xFE') return vpp43.read(self._vi, 17)
def read_spectrum(self): # Request spectrum vpp43.write(self._vi, '\x09') return self._read_spectrum_data()
def integration_time(self, value): if value < self._min_integration_time: raise ValueError('Minimum integration time is ' '{}'.format(self._min_integration_time)) packed_value = struct.pack('<I', value * 1e6) vpp43.write(self._vi, '\x02' + packed_value)
def do_set_measurement_time(self, dt): (DataH, DataL) = self._short_to_bytes(dt) message = "%c%c" % (DataH, DataL) vpp43.write(self._vi, message)