Interfacing to instruments

lantz.messagebased

Implementes base class for message based drivers using PyVISA under the hood.

copyright:2015 by Lantz Authors, see AUTHORS for more details.
license:BSD, see LICENSE for more details.
class lantz.messagebased.MessageBasedDriver(resource_name, name=None, **kwargs)[source]

Bases: lantz.driver.Driver

Base class for message based drivers using PyVISA as underlying library.

Notice that PyVISA can communicate using different backends. For example: - @ni: Using NI-VISA for communication. Backend bundled with PyVISA. - @py: Using PySerial, PyUSB and linux-gpib for communication. Available with PyVISA-py package. - @sim: Simulated devices. Available with PyVISA-sim package.

Parameters:
  • resource_name (str) – The resource name
  • kwargs – keyword arguments passed to the resource during initialization.
Params name:

easy to remember identifier given to the instance for logging purposes.

parse_query(command, *, send_args=(None, None), recv_args=(None, None), format=None)[source]

Send query to the instrument, parse the output using format and return the answer.

See also

TextualMixin.query and stringparser

query(command, *, send_args=(None, None), recv_args=(None, None))[source]

Send query to the instrument and return the answer

Parameters:
  • command (string) – command to be sent to the instrument
  • send_args – (termination, encoding) to override class defaults
  • recv_args – (termination, encoding) to override class defaults
read(termination=None, encoding=None)[source]

Receive string from instrument.

Parameters:
  • termination (str) – termination character (overrides class default)
  • encoding – encoding to transform bytes to string (overrides class default)
Returns:

string encoded from received bytes

classmethod via_gpib(address, name=None, **kwargs)[source]

Return a Driver with an underlying GPIB Instrument resource.

Parameters:
  • address – The gpib address of the instrument.
  • name – Unique name given within Lantz to the instrument for logging purposes. Defaults to one generated based on the class name if not provided.
  • kwargs – keyword arguments passed to the Resource constructor on initialize.
Return type:

MessageBasedDriver

classmethod via_serial(port, name=None, **kwargs)[source]

Return a Driver with an underlying ASRL (Serial) Instrument resource.

Parameters:
  • port – The serial port to which the instrument is connected.
  • name – Unique name given within Lantz to the instrument for logging purposes. Defaults to one generated based on the class name if not provided.
  • kwargs – keyword arguments passed to the Resource constructor on initialize.
Return type:

MessageBasedDriver

classmethod via_tcpip(hostname, port, name=None, **kwargs)[source]

Return a Driver with an underlying TCP Instrument resource.

Parameters:
  • hostname – The ip address or hostname of the instrument.
  • port – the port of the instrument.
  • name – Unique name given within Lantz to the instrument for logging purposes. Defaults to one generated based on the class name if not provided.
  • kwargs – keyword arguments passed to the Resource constructor on initialize.
Return type:

MessageBasedDriver

classmethod via_tcpip_socket(hostname, port, name=None, **kwargs)[source]

Return a Driver with an underlying TCP Socket resource.

Parameters:
  • hostname – The ip address or hostname of the instrument.
  • port – the port of the instrument.
  • name – Unique name given within Lantz to the instrument for logging purposes. Defaults to one generated based on the class name if not provided.
  • kwargs – keyword arguments passed to the Resource constructor on initialize.
Return type:

MessageBasedDriver

classmethod via_usb(serial_number=None, manufacturer_id=None, model_code=None, name=None, board=0, **kwargs)[source]

Return a Driver with an underlying USB Instrument resource.

A connected USBTMC instrument with the specified serial_number, manufacturer_id, and model_code is returned. If any of these is missing, the first USBTMC driver matching any of the provided values is returned.

To specify the manufacturer id and/or the model code override the following class attributes:

class RigolDS1052E(MessageBasedDriver):

    MANUFACTURER_ID = '0x1AB1'
    MODEL_CODE = '0x0588'
Parameters:
  • serial_number – The serial number of the instrument.
  • manufacturer_id – The unique identification number of the manufacturer.
  • model_code – The unique identification number of the product.
  • name – Unique name given within Lantz to the instrument for logging purposes. Defaults to one generated based on the class name if not provided.
  • board – USB Board to use
  • kwargs – keyword arguments passed to the Resource constructor on initialize.
Return type:

MessageBasedDriver

classmethod via_usb_raw(serial_number=None, manufacturer_id=None, model_code=None, name=None, board=0, **kwargs)[source]

Return a Driver with an underlying USB RAW resource.

Parameters:
  • serial_number – The serial number of the instrument.
  • manufacturer_id – The unique identification number of the manufacturer.
  • model_code – The unique identification number of the product.
  • name – Unique name given within Lantz to the instrument for logging purposes. Defaults to one generated based on the class name if not provided.
  • board – USB Board to use
  • kwargs – keyword arguments passed to the Resource constructor on initialize.
Return type:

MessageBasedDriver

write(command, termination=None, encoding=None)[source]

Send command to the instrument.

Parameters:
  • command (string.) – command to be sent to the instrument.
  • termination – termination character to override class defined default.
  • encoding – encoding to transform string to bytes to override class defined default.
Returns:

number of bytes sent.

DEFAULTS = None

Default arguments passed to the Resource constructor on initialize. It should be specified in two layers, the first indicating the interface type and the second the corresponding arguments. The key COMMON is used to indicate keywords for all interfaces. For example:

{'ASRL':     {'read_termination': '\n',
              'baud_rate': 9600},
 'USB':      {'read_termination': \r'},
 'COMMON':   {'write_termination': '\n'}
}
Type:dict[str, dict[str, str]]
MANUFACTURER_ID = None

The identification number of the manufacturer as hex code. :type: str | None

MODEL_CODE = None

The code number of the model as hex code. Can provide a tuple/list to indicate multiple models. :type: str | list | tuple | None

resource = None
Type:pyvisa.resources.MessageBasedResource
resource_kwargs = None

keyword arguments passed to the resource during initialization. :type: dict

resource_name = None

The resource name :type: str

lantz.messagebased.get_resource_manager()[source]

Return the PyVISA Resource Manager, creating an instance if necessary.

Return type:visa.ResourceManager

lantz.foreign

Implements classes and methods to interface to foreign functions.

copyright:2015 by Lantz Authors, see AUTHORS for more details.
license:BSD, see LICENSE for more details.
class lantz.foreign.Library(library, prefix='', wrapper=None)[source]

Bases: object

Library wrapper

Parameters:
  • library – ctypes library
  • wrapper – callable that takes two arguments the name of the function and the function itself. It should return a callable.
class lantz.foreign.LibraryDriver(*args, **kwargs)[source]

Bases: lantz.driver.Driver

Base class for drivers that communicate with instruments calling a library (dll or others)

To use this class you must override LIBRARY_NAME

LIBRARY_NAME = ''

Name of the library