pymeasure.adapters¶
The adapter classes allow the instruments to be independent of the communication method used.
Adapters for specific instruments should be grouped in an adapters.py
file in the corresponding manufacturer’s folder of pymeasure.instruments
. For example, the adapter for communicating with LakeShore instruments over USB, LakeShoreUSBAdapter
, is found in pymeasure.instruments.lakeshore.adapters
.
Adapter base class¶
-
class
pymeasure.adapters.
Adapter
(preprocess_reply=None, **kwargs)¶ Base class for Adapter child classes, which adapt between the Instrument object and the connection, to allow flexible use of different connection techniques.
This class should only be inherited from.
- Parameters
preprocess_reply – optional callable used to preprocess strings received from the instrument. The callable returns the processed string.
kwargs – all other keyword arguments are ignored.
-
ask
(command)¶ Writes the command to the instrument and returns the resulting ASCII response
- Parameters
command – SCPI command string to be sent to the instrument
- Returns
String ASCII response of the instrument
-
binary_values
(command, header_bytes=0, dtype=<class 'numpy.float32'>)¶ Returns a numpy array from a query for binary data
- Parameters
command – SCPI command to be sent to the instrument
header_bytes – Integer number of bytes to ignore in header
dtype – The NumPy data type to format the values with
- Returns
NumPy array of values
-
read
()¶ Reads until the buffer is empty and returns the resulting ASCII respone
- Returns
String ASCII response of the instrument.
-
values
(command, separator=', ', cast=<class 'float'>, preprocess_reply=None)¶ Writes a command to the instrument and returns a list of formatted values from the result
- Parameters
command – SCPI command to be sent to the instrument
separator – A separator character to split the string into a list
cast – A type to cast the result
preprocess_reply – optional callable used to preprocess values received from the instrument. The callable returns the processed string. If not specified, the Adapter default is used if available, otherwise no preprocessing is done.
- Returns
A list of the desired type, or strings where the casting fails
-
write
(command)¶ Writes a command to the instrument
- Parameters
command – SCPI command string to be sent to the instrument
Fake adapter¶
-
class
pymeasure.adapters.
FakeAdapter
(preprocess_reply=None, **kwargs)¶ Bases:
pymeasure.adapters.adapter.Adapter
Provides a fake adapter for debugging purposes, which bounces back the command so that arbitrary values testing is possible.
a = FakeAdapter() assert a.read() == "" a.write("5") assert a.read() == "5" assert a.read() == "" assert a.ask("10") == "10" assert a.values("10") == [10]
-
ask
(command)¶ Writes the command to the instrument and returns the resulting ASCII response
- Parameters
command – SCPI command string to be sent to the instrument
- Returns
String ASCII response of the instrument
-
binary_values
(command, header_bytes=0, dtype=<class 'numpy.float32'>)¶ Returns a numpy array from a query for binary data
- Parameters
command – SCPI command to be sent to the instrument
header_bytes – Integer number of bytes to ignore in header
dtype – The NumPy data type to format the values with
- Returns
NumPy array of values
-
read
()¶ Returns the last commands given after the last read call.
-
values
(command, separator=', ', cast=<class 'float'>, preprocess_reply=None)¶ Writes a command to the instrument and returns a list of formatted values from the result
- Parameters
command – SCPI command to be sent to the instrument
separator – A separator character to split the string into a list
cast – A type to cast the result
preprocess_reply – optional callable used to preprocess values received from the instrument. The callable returns the processed string. If not specified, the Adapter default is used if available, otherwise no preprocessing is done.
- Returns
A list of the desired type, or strings where the casting fails
-
write
(command)¶ Writes the command to a buffer, so that it can be read back.
-
Serial adapter¶
-
class
pymeasure.adapters.
SerialAdapter
(port, preprocess_reply=None, **kwargs)¶ Bases:
pymeasure.adapters.adapter.Adapter
Adapter class for using the Python Serial package to allow serial communication to instrument
- Parameters
port – Serial port
preprocess_reply – optional callable used to preprocess strings received from the instrument. The callable returns the processed string.
kwargs – Any valid key-word argument for serial.Serial
-
ask
(command)¶ Writes the command to the instrument and returns the resulting ASCII response
- Parameters
command – SCPI command string to be sent to the instrument
- Returns
String ASCII response of the instrument
-
binary_values
(command, header_bytes=0, dtype=<class 'numpy.float32'>)¶ Returns a numpy array from a query for binary data
- Parameters
command – SCPI command to be sent to the instrument
header_bytes – Integer number of bytes to ignore in header
dtype – The NumPy data type to format the values with
- Returns
NumPy array of values
-
read
()¶ Reads until the buffer is empty and returns the resulting ASCII respone
- Returns
String ASCII response of the instrument.
-
values
(command, separator=', ', cast=<class 'float'>, preprocess_reply=None)¶ Writes a command to the instrument and returns a list of formatted values from the result
- Parameters
command – SCPI command to be sent to the instrument
separator – A separator character to split the string into a list
cast – A type to cast the result
preprocess_reply – optional callable used to preprocess values received from the instrument. The callable returns the processed string. If not specified, the Adapter default is used if available, otherwise no preprocessing is done.
- Returns
A list of the desired type, or strings where the casting fails
-
write
(command)¶ Writes a command to the instrument
- Parameters
command – SCPI command string to be sent to the instrument
Prologix adapter¶
-
class
pymeasure.adapters.
PrologixAdapter
(port, address=None, rw_delay=None, serial_timeout=0.5, preprocess_reply=None, **kwargs)¶ Bases:
pymeasure.adapters.serial.SerialAdapter
Encapsulates the additional commands necessary to communicate over a Prologix GPIB-USB Adapter, using the SerialAdapter.
Each PrologixAdapter is constructed based on a serial port or connection and the GPIB address to be communicated to. Serial connection sharing is achieved by using the
gpib()
method to spawn new PrologixAdapters for different GPIB addresses.- Parameters
port – The Serial port name or a serial.Serial object
address – Integer GPIB address of the desired instrument
rw_delay – An optional delay to set between a write and read call for slow to respond instruments.
preprocess_reply – optional callable used to preprocess strings received from the instrument. The callable returns the processed string.
kwargs – Key-word arguments if constructing a new serial object
- Variables
address – Integer GPIB address of the desired instrument
To allow user access to the Prologix adapter in Linux, create the file:
/etc/udev/rules.d/51-prologix.rules
, with contents:SUBSYSTEMS=="usb",ATTRS{idVendor}=="0403",ATTRS{idProduct}=="6001",MODE="0666"
Then reload the udev rules with:
sudo udevadm control --reload-rules sudo udevadm trigger
-
ask
(command)¶ Ask the Prologix controller, include a forced delay for some instruments.
- Parameters
command – SCPI command string to be sent to instrument
-
binary_values
(command, header_bytes=0, dtype=<class 'numpy.float32'>)¶ Returns a numpy array from a query for binary data
- Parameters
command – SCPI command to be sent to the instrument
header_bytes – Integer number of bytes to ignore in header
dtype – The NumPy data type to format the values with
- Returns
NumPy array of values
-
gpib
(address, rw_delay=None)¶ Returns and PrologixAdapter object that references the GPIB address specified, while sharing the Serial connection with other calls of this function
- Parameters
address – Integer GPIB address of the desired instrument
rw_delay – Set a custom Read/Write delay for the instrument
- Returns
PrologixAdapter for specific GPIB address
-
read
()¶ Reads the response of the instrument until timeout
- Returns
String ASCII response of the instrument
-
set_defaults
()¶ Sets up the default behavior of the Prologix-GPIB adapter
-
values
(command, separator=', ', cast=<class 'float'>, preprocess_reply=None)¶ Writes a command to the instrument and returns a list of formatted values from the result
- Parameters
command – SCPI command to be sent to the instrument
separator – A separator character to split the string into a list
cast – A type to cast the result
preprocess_reply – optional callable used to preprocess values received from the instrument. The callable returns the processed string. If not specified, the Adapter default is used if available, otherwise no preprocessing is done.
- Returns
A list of the desired type, or strings where the casting fails
-
wait_for_srq
(timeout=25, delay=0.1)¶ Blocks until a SRQ, and leaves the bit high
- Parameters
timeout – Timeout duration in seconds
delay – Time delay between checking SRQ in seconds
-
write
(command)¶ Writes the command to the GPIB address stored in the
address
- Parameters
command – SCPI command string to be sent to the instrument
VISA adapter¶
-
class
pymeasure.adapters.
VISAAdapter
(resource_name, visa_library='', preprocess_reply=None, **kwargs)¶ Bases:
pymeasure.adapters.adapter.Adapter
Adapter class for the VISA library using PyVISA to communicate with instruments.
- Parameters
resource – VISA resource name that identifies the address
visa_library – VisaLibrary Instance, path of the VISA library or VisaLibrary spec string (@py or @ni). if not given, the default for the platform will be used.
preprocess_reply – optional callable used to preprocess strings received from the instrument. The callable returns the processed string.
kwargs – Any valid key-word arguments for constructing a PyVISA instrument
-
ask
(command)¶ Writes the command to the instrument and returns the resulting ASCII response
- Parameters
command – SCPI command string to be sent to the instrument
- Returns
String ASCII response of the instrument
-
ask_values
(command, **kwargs)¶ Writes a command to the instrument and returns a list of formatted values from the result. This leverages the query_ascii_values method in PyVISA.
- Parameters
command – SCPI command to be sent to the instrument
kwargs – Key-word arguments to pass onto query_ascii_values
- Returns
Formatted response of the instrument.
-
binary_values
(command, header_bytes=0, dtype=<class 'numpy.float32'>)¶ Returns a numpy array from a query for binary data
- Parameters
command – SCPI command to be sent to the instrument
header_bytes – Integer number of bytes to ignore in header
dtype – The NumPy data type to format the values with
- Returns
NumPy array of values
-
static
has_supported_version
()¶ Returns True if the PyVISA version is greater than 1.8
-
read
()¶ Reads until the buffer is empty and returns the resulting ASCII response
- Returns
String ASCII response of the instrument.
-
read_bytes
(size)¶ Reads specified number of bytes from the buffer and returns the resulting ASCII response
- Parameters
size – Number of bytes to read from the buffer
- Returns
String ASCII response of the instrument.
-
values
(command, separator=', ', cast=<class 'float'>, preprocess_reply=None)¶ Writes a command to the instrument and returns a list of formatted values from the result
- Parameters
command – SCPI command to be sent to the instrument
separator – A separator character to split the string into a list
cast – A type to cast the result
preprocess_reply – optional callable used to preprocess values received from the instrument. The callable returns the processed string. If not specified, the Adapter default is used if available, otherwise no preprocessing is done.
- Returns
A list of the desired type, or strings where the casting fails
-
wait_for_srq
(timeout=25, delay=0.1)¶ Blocks until a SRQ, and leaves the bit high
- Parameters
timeout – Timeout duration in seconds
delay – Time delay between checking SRQ in seconds
-
write
(command)¶ Writes a command to the instrument
- Parameters
command – SCPI command string to be sent to the instrument
-
write_binary_values
(command, values, **kwargs)¶ Write binary data to the instrument, e.g. waveform for signal generators
- Parameters
command – SCPI command to be sent to the instrument
values – iterable representing the binary values
kwargs – Key-word arguments to pass onto write_binary_values
- Returns
number of bytes written
VXI-11 adapter¶
-
class
pymeasure.adapters.
VXI11Adapter
(host, preprocess_reply=None, **kwargs)¶ Bases:
pymeasure.adapters.adapter.Adapter
- VXI11 Adapter class. Provides a adapter object that
wraps around the read, write and ask functionality of the vxi11 library.
- Parameters
host – string containing the visa connection information.
preprocess_reply – optional callable used to preprocess strings received from the instrument. The callable returns the processed string.
-
ask
(command)¶ Wrapper function for the ask command using the vx11 interface.
- Parameters
command – string with the command that will be transmitted to the instrument.
:returns string containing a response from the device.
-
ask_raw
(command)¶ Wrapper function for the ask_raw command using the vx11 interface.
- Parameters
command – binary string with the command that will be transmitted to the instrument
:returns binary string containing the response from the device.
-
binary_values
(command, header_bytes=0, dtype=<class 'numpy.float32'>)¶ Returns a numpy array from a query for binary data
- Parameters
command – SCPI command to be sent to the instrument
header_bytes – Integer number of bytes to ignore in header
dtype – The NumPy data type to format the values with
- Returns
NumPy array of values
-
read
()¶ Wrapper function for the read command using the vx11 interface.
:return string containing a response from the device.
-
read_raw
()¶ Wrapper function for the read_raw command using the vx11 interface.
:returns binary string containing the response from the device.
-
values
(command, separator=', ', cast=<class 'float'>, preprocess_reply=None)¶ Writes a command to the instrument and returns a list of formatted values from the result
- Parameters
command – SCPI command to be sent to the instrument
separator – A separator character to split the string into a list
cast – A type to cast the result
preprocess_reply – optional callable used to preprocess values received from the instrument. The callable returns the processed string. If not specified, the Adapter default is used if available, otherwise no preprocessing is done.
- Returns
A list of the desired type, or strings where the casting fails
-
write
(command)¶ Wrapper function for the write command using the vxi11 interface.
- Parameters
command – string with command the that will be transmitted to the instrument.
-
write_raw
(command)¶ Wrapper function for the write_raw command using the vxi11 interface.
- Parameters
command – binary string with the command that will be transmitted to the instrument
Telnet adapter¶
-
class
pymeasure.adapters.
TelnetAdapter
(host, port=0, query_delay=0, preprocess_reply=None, **kwargs)¶ Bases:
pymeasure.adapters.adapter.Adapter
Adapter class for using the Python telnetlib package to allow communication to instruments
- Parameters
host – host address of the instrument
port – TCPIP port
query_delay – delay in seconds between write and read in the ask method
preprocess_reply – optional callable used to preprocess strings received from the instrument. The callable returns the processed string.
kwargs – Valid keyword arguments for telnetlib.Telnet, currently this is only ‘timeout’
-
ask
(command)¶ Writes a command to the instrument and returns the resulting ASCII response
- Parameters
command – command string to be sent to the instrument
- Returns
String ASCII response of the instrument
-
binary_values
(command, header_bytes=0, dtype=<class 'numpy.float32'>)¶ Returns a numpy array from a query for binary data
- Parameters
command – SCPI command to be sent to the instrument
header_bytes – Integer number of bytes to ignore in header
dtype – The NumPy data type to format the values with
- Returns
NumPy array of values
-
read
()¶ Read something even with blocking the I/O. After something is received check again to obtain a full reply.
- Returns
String ASCII response of the instrument.
-
values
(command, separator=', ', cast=<class 'float'>, preprocess_reply=None)¶ Writes a command to the instrument and returns a list of formatted values from the result
- Parameters
command – SCPI command to be sent to the instrument
separator – A separator character to split the string into a list
cast – A type to cast the result
preprocess_reply – optional callable used to preprocess values received from the instrument. The callable returns the processed string. If not specified, the Adapter default is used if available, otherwise no preprocessing is done.
- Returns
A list of the desired type, or strings where the casting fails
-
write
(command)¶ Writes a command to the instrument
- Parameters
command – command string to be sent to the instrument