Reference Manual

base_classes module

class microlab_instruments.base_classes.AardvarkInstrument[source]

Bases: object

Initialize an Aardvark.

Raises:Exception – Upon instantiation, SPI communication is tested. A 25-long array of bytes is sent twice to the Aardvark (and subsequently to the FPGA). After the second attempt, a response identical to the array must be received. If not, an Exception is raised. In this case, it may be likely that the FPGA did not respond properly.
I2C_STATUS_CODES = {1: 'AA_I2C_STATUS_BUS_ERROR', 2: 'AA_I2C_STATUS_SLA_ACK', 3: 'AA_I2C_STATUS_SLA_NACK', 4: 'AA_I2C_STATUS_DATA_NACK', 5: 'AA_I2C_STATUS_ARB_LOST', 6: 'AA_I2C_STATUS_BUS_LOCKED', 7: 'AA_I2C_STATUS_LAST_DATA_ACK'}

These are the status codes used by i2c_write(), i2c_read(), and i2c_write_read() when raising Exceptions.

i2c_read(address, bufsize)[source]

Read bufsize number of bytes from the I2C slave with address.

Parameters:
  • address (int) – Slave address from which to receive response.
  • bufsize (int) – Size in bytes of expected response from slave.
Returns out:

Response from slave. A bufsize-length list of ints.

Return type:

list

Raises:

Exception – if the status response is not 0. See I2C_STATUS_CODES.

i2c_write(address, bytecode)[source]

Write bytecode to the Aardvark output to be received by I2C slave with address.

Parameters:
  • address (int) – Slave address to receive bytecode. Limited to 8 bits.
  • bytecode (int) – Raw bytecode to send. Limited to 8 bits.
Returns out:

Number of bytes sent.

Return type:

int

Raises:

Exception – if the status response is not 0. See I2C_STATUS_CODES.

i2c_write_read(address, bytecode, bufsize)[source]

Write bytecode to, and read bufsize bytes from, I2C slave with address in one fell swoop!

Parameters:
  • address (int) – Slave address to receive bytecode. Limited to 8 bits.
  • bytecode (int) – Raw bytecode to send. Limited to 8 bits.
  • bufsize (int) – Size in bytes of expected response from slave.
Returns out:

Response from slave. A bufsize-length list of ints.

Return type:

list

Raises:

Exception – if the status response is not 0. See I2C_STATUS_CODES.

spi_write(bytecode)[source]

Write bytecode to, and read 25 bytes from, the SPI channel in one fell swoop!

Parameters:bytecode (list) – Raw bytecodes to send. Must be exactly 25-long list of bytes.
Returns out:Response bytes. A 25-length list of ints.
Return type:list
Raises:Exception – if bytecode does not have exactly 25 8-bit elements.
class microlab_instruments.base_classes.FPGAInstrument(aardvark)[source]

Bases: object

An abstraction layer for the FPGA.

read(register)[source]

Read the contents of register.

Parameters:register (int) – The register address to read. Limited to 1 byte long.
write(register, payload)[source]

Write a 1-byte-long payload to a register address.

Parameters:
  • payload (int) – The data to write. Limited to 1 byte long.
  • register (int) – The register address to write to. Limited to 1 byte long.
class microlab_instruments.base_classes.GPIBInstrument(nickname, reset=True)[source]

Bases: microlab_instruments.base_classes.SCPIInstrument

Initialize a GPIB instrument

Parameters:nickname (str) – A nickname associated with a GPIB primary address and defined in /etc/gpib.conf.
read(bufsize=4096)[source]

Read bufsize bytes from instrument. Using this low-level function, there is no way to ensure that all the response data has been retrieved, or to make sense of binary data. It is strongly recommended to use read_ascii(), read_binary(), or read_ieee754().

Parameters:bufsize (int) – Defaults to 4096 bytes. Expected size in bytes of the response from the instrument.
Returns out:Response from the instrument.
Return type:str
reset()[source]

Reset the GPIB instrument.

write(scpi_string)[source]

Write SCPI command to the instrument. The end-of-string character (for example, \n) is automatically appended.

Parameters:scpi_string (str) – A valid SCPI command. See the instrument’s SCPI command reference.
class microlab_instruments.base_classes.I2CMuxInstrument(aardvark)[source]

Bases: object

An abstraction layer for the I2C multiplexer chip.

switch_to(mux_slave_address)[source]

Setup the multiplexer to relay I2C commands to the device having mux_slave_address

Parameters:slave_address (int) – The device to which the multiplexer will relay I2C commands.
class microlab_instruments.base_classes.SCPIInstrument[source]

Bases: object

ask_ascii(scpi_string)[source]

A convenience function for calling write() and read_ascii() consecutively. Up to 4096 bytes are read from the ASCII response buffer.

Parameters:scpi_string (str) – A valid SCPI query command. See the instrument’s SCPI command reference.
Raises:Exception – If the SCPI command does not end with a ‘?’ (i.e. not a query command)
ask_binary(scpi_string)[source]

A convenience function for calling write() and read_binary() consecutively.

Parameters:scpi_string (str) – A valid SCPI query command. See the instrument’s SCPI command reference.
Raises:Exception – If the SCPI command does not end with a ‘?’ (i.e. not a query command)
ask_ieee754(scpi_string)[source]

A convenience function for calling write() and read_ieee754() consecutively.

Parameters:scpi_string (str) – A valid SCPI query command. See the instrument’s SCPI command reference.
Raises:Exception – If the SCPI command does not end with a ‘?’ (i.e. not a query command)
configure(config_file)[source]

Reads from a text file containing valid SCPI commands separated by newlines to configure the instrument. Only program commands are allowed. Configures the instrument by sending those commands consecutively. Automatically sends an *OPC? command to await pending operations. Prints out those commands to standard output.

Text written after a ‘#’ character are considered comments.

Commands in the configuration file are assumed to be valid for the instrument.

Parameters:config_file (str) – The filename of the configuration file.
Raises:Exception – If any of the SCPI commands contain a ‘?’ (i.e. are query commands)
read_ascii(bufsize=4096)[source]

Read ASCII response from instrument in chunks of bufsize bytes until a \n is encountered.

Parameters:bufsize (int) – Defaults to 4096 bytes. Size of consecutive chunks of data to be read.
Returns out:Response from the instrument.
Return type:str
read_binary()[source]

Read raw binary data from instrument. It is the developer’s responsiblity to make sense of it.

Returns out:Response from the instrument. This is just a string of binary code.
Return type:str

A typical use case is obtaining a screenshot of the instrument panel. The following code is for the Agilent B2902A Precision Source Measure Unit, nicknamed ‘Yveltal’.

import microlab_instruments as mi

yveltal = mi.Yveltal()
yveltal.write(':DISP:ENAB ON')
yveltal.write(':DISP:VIEW GRAP')
yveltal.write(':HCOP:SDUM:FORM JPG')
yveltal.ask_ascii('*OPC?')
yveltal.write(':HCOP:SDUM:DATA?')
image_data = yveltal.read_binary()

file_handle = open('screendump.jpg', 'wb')
file_handle.write(image_data)
file_handle.close()
read_ieee754()[source]

A convenience function to read binary data known to be formatted in IEEE-754 floating-point. Internally calls read_binary() and automatically determines half-, single-, or double-precision based on the instrument’s settings.

Returns out:A list of floating-point numbers.
Return type:list
class microlab_instruments.base_classes.SerialInstrument(device_port)[source]

Bases: object

Initialize an RS-232 instrument.

class microlab_instruments.base_classes.TCPIPInstrument(socket_pair, reset=True)[source]

Bases: microlab_instruments.base_classes.SCPIInstrument

Initialize TCP/IP instrument.

Parameters:socket_pair (tuple) – A 2-tuple of the form ('192.168.1.2', 5025).
read(bufsize=4096)[source]

Read bufsize bytes from instrument. Using this low-level function, there is no way to ensure that all the response data has been retrieved, or to make sense of binary data. It is strongly recommended to use read_ascii(), read_binary(), or read_ieee754().

Parameters:bufsize (int) – Defaults to 4096 bytes. Expected size in bytes of the response from the instrument.
Returns out:Response from the instrument.
Return type:str
reset()[source]

Reset the instrument.

write(scpi_string)[source]

Write SCPI command to the instrument. The end-of-string character (for example, \n) is automatically appended.

Parameters:scpi_string (str) – A valid SCPI command. See the instrument’s SCPI command reference.
class microlab_instruments.base_classes.TempSensorInstrument(aardvark, mux)[source]

Bases: object

An abstraction layer for the Sensirion STS21 temperature sensor with an I2C communication interface.

Initialize a Sensirion STS21 temperature sensor.

Parameters:
  • aardvark (Aardvark) – An Aardvark object through which I2C commands are relayed.
  • mux (I2CMuxInstrument) – The I2C multiplexer through which I2C commands are relayed.
read_temp()[source]

Read measured temperature data.

Returns out:Temperature in degress Celsius
Return type:float

microlab_instruments module

class microlab_instruments.microlab_instruments.Arceus[source]

Bases: microlab_instruments.base_classes.GPIBInstrument

class microlab_instruments.microlab_instruments.Chen(aardvark)[source]

Bases: microlab_instruments.base_classes.I2CMuxInstrument

Initialize the I2C multiplexer.

Parameters:aardvark (Aardvark) – An Aardvark object through which I2C commands are relayed.
import microlab_instruments as mi

aa = mi.Aardvark()
chen = mi.Chen(aa)
class microlab_instruments.microlab_instruments.Darkrai[source]

Bases: microlab_instruments.base_classes.TCPIPInstrument

class microlab_instruments.microlab_instruments.Deoxys[source]

Bases: microlab_instruments.base_classes.TCPIPInstrument

ask_waveform_data()[source]

A convenience function to query the waveform preamble and waveform data in one call. Additionally, it also composes the (x,y) data list.

Returns out:A 2-column list. The first column holds the x values and the second column holds the y values.
Return type:list
compose_waveform_xy(waveform_y, waveform_preamble)[source]

Compose the (x,y) data list according to the y data and preamble obtained from the instrument.

Returns out:A 2-column list. The first column holds the x values and the second column holds the y values.
Return type:list
read_preamble()[source]

Read the waveform preamble from Deoxys. It contains the following metadata about the waveform data:

Returns out:
Return type:dict
class microlab_instruments.microlab_instruments.Genesect[source]

Bases: microlab_instruments.base_classes.TCPIPInstrument

class microlab_instruments.microlab_instruments.Giratina[source]

Bases: microlab_instruments.base_classes.TCPIPInstrument

class microlab_instruments.microlab_instruments.Heatran[source]

Bases: microlab_instruments.base_classes.TCPIPInstrument

class microlab_instruments.microlab_instruments.Ho_oh[source]

Bases: microlab_instruments.base_classes.TCPIPInstrument

class microlab_instruments.microlab_instruments.Kerrigan(aardvark)[source]

Bases: microlab_instruments.base_classes.FPGAInstrument

Initialize the FPGA.

Parameters:aardvark (Aardvark) – An Aardvark object through which I2C commands are relayed.
import microlab_instruments as mi

aa = mi.Aardvark()
kerrigan = mi.Kerrigan(aa)
class microlab_instruments.microlab_instruments.Kyurem[source]

Bases: microlab_instruments.base_classes.TCPIPInstrument

class microlab_instruments.microlab_instruments.Meloetta[source]

Bases: microlab_instruments.base_classes.GPIBInstrument

class microlab_instruments.microlab_instruments.Rayquaza[source]

Bases: microlab_instruments.base_classes.TCPIPInstrument

class microlab_instruments.microlab_instruments.Traxex(aardvark, mux)[source]

Bases: microlab_instruments.base_classes.TempSensorInstrument

Initialize a Sensirion STS21 temperature sensor.

Parameters:aardvark (Aardvark) – An Aardvark object through which I2C commands are relayed.
import microlab_instruments as mi

aa = mi.Aardvark()
traxex = mi.Traxex(aa)
print traxex.read_temp()
class microlab_instruments.microlab_instruments.Xerneas[source]

Bases: microlab_instruments.base_classes.GPIBInstrument

class microlab_instruments.microlab_instruments.Xin(aardvark, mux)[source]

Bases: microlab_instruments.base_classes.TempSensorInstrument

Initialize a Sensirion STS21 temperature sensor.

Parameters:aardvark (Aardvark) – An Aardvark object through which I2C commands are relayed.
import microlab_instruments as mi

aa = mi.Aardvark()
xin = mi.Xin(aa)
print xin.read_temp()
class microlab_instruments.microlab_instruments.Yveltal[source]

Bases: microlab_instruments.base_classes.TCPIPInstrument

class microlab_instruments.microlab_instruments.Zygarde[source]

Bases: microlab_instruments.base_classes.TCPIPInstrument