| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import logging |
| 26 |
from warnings import warn |
| 27 |
|
| 28 |
from .adapter import Adapter |
| 29 |
|
| 30 |
log = logging.getLogger(__name__) |
| 31 |
log.addHandler(logging.NullHandler()) |
| 32 |
|
| 33 |
try: |
| 34 |
import vxi11 |
| 35 |
except ImportError: |
| 36 |
log.warning('Failed to import vxi11 package, which is required for the VXI11Adapter') |
| 37 |
|
| 38 |
|
| 39 |
class VXI11Adapter(Adapter): |
| 40 |
""" VXI11 Adapter class. Provides a adapter object that |
| 41 |
wraps around the read, write and ask functionality |
| 42 |
of the vxi11 library. |
| 43 |
|
| 44 |
.. deprecated:: 0.11 |
| 45 |
Use VISAAdapter instead. |
| 46 |
|
| 47 |
:param host: string containing the visa connection information. |
| 48 |
:param preprocess_reply: (deprecated) optional callable used to preprocess strings |
| 49 |
received from the instrument. The callable returns the processed string. |
| 50 |
""" |
| 51 |
|
| 52 |
def __init__(self, host, preprocess_reply=None, **kwargs): |
| 53 |
super().__init__(preprocess_reply=preprocess_reply, |
| 54 |
query_delay=kwargs.pop('query_delay', 0)) |
| 55 |
warn("Deprecated, use VISAAdapter instead.", FutureWarning) |
| 56 |
# Filter valid arguments that can be passed to vxi instrument |
| 57 |
valid_args = ["name", "client_id", "term_char"] |
| 58 |
self.conn_kwargs = {} |
| 59 |
for key in kwargs: |
| 60 |
if key in valid_args: |
| 61 |
self.conn_kwargs[key] = kwargs[key] |
| 62 |
|
| 63 |
self.connection = vxi11.Instrument(host, **self.conn_kwargs) |
| 64 |
|
| 65 |
def _write(self, command, **kwargs): |
| 66 |
"""Write a string command to the instrument appending `write_termination`. |
| 67 |
|
| 68 |
:param str command: Command string to be sent to the instrument |
| 69 |
(without termination). |
| 70 |
:param kwargs: Keyword arguments for the connection itself. |
| 71 |
""" |
| 72 |
self.connection.write(command) |
| 73 |
|
| 74 |
def _read(self, **kwargs): |
| 75 |
"""Read up to (excluding) `read_termination` or the whole read buffer. |
| 76 |
|
| 77 |
:param kwargs: Keyword arguments for the connection itself. |
| 78 |
:returns str: ASCII response of the instrument (excluding read_termination). |
| 79 |
""" |
| 80 |
return self.connection.read() |
| 81 |
|
| 82 |
def ask(self, command): |
| 83 |
""" Wrapper function for the ask command using the |
| 84 |
vx11 interface. |
| 85 |
|
| 86 |
.. deprecated:: 0.11 |
| 87 |
Call `Instrument.ask` instead. |
| 88 |
|
| 89 |
:param command: string with the command that will be transmitted |
| 90 |
to the instrument. |
| 91 |
|
| 92 |
:returns string containing a response from the device. |
| 93 |
""" |
| 94 |
warn("Do not call `Adapter.ask`, but `Instrument.ask` instead.", |
| 95 |
FutureWarning) |
| 96 |
return self.connection.ask(command) |
| 97 |
|
| 98 |
def write_raw(self, command): |
| 99 |
"""Write bytes to the device. |
| 100 |
|
| 101 |
.. deprecated:: 0.11 |
| 102 |
Use `write_bytes` instead. |
| 103 |
""" |
| 104 |
warn("Use `write_bytes` instead.", FutureWarning) |
| 105 |
self.write_bytes(command) |
| 106 |
|
| 107 |
def _write_bytes(self, command, **kwargs): |
| 108 |
"""Write the bytes `content` to the instrument. |
| 109 |
|
| 110 |
:param bytes content: The bytes to write to the instrument. |
| 111 |
:param kwargs: Keyword arguments for the connection itself. |
| 112 |
|
| 113 |
.. note:: |
| 114 |
vx11 adds the term_char even for writing_bytes. |
| 115 |
""" |
| 116 |
# Note: vxi11.write_raw adds the term_char! |
| 117 |
self.connection.write_raw(command, **kwargs) |
| 118 |
|
| 119 |
def read_raw(self): |
| 120 |
"""Read bytes from the device. |
| 121 |
|
| 122 |
.. deprecated:: 0.11 |
| 123 |
Use `read_bytes` instead. |
| 124 |
""" |
| 125 |
warn("Use `read_bytes` instead.", FutureWarning) |
| 126 |
return self.read_bytes(-1) |
| 127 |
|
| 128 |
def _read_bytes(self, count, break_on_termchar=False, **kwargs): |
| 129 |
"""Read a certain number of bytes from the instrument. |
| 130 |
|
| 131 |
:param int count: Number of bytes to read. A value of -1 indicates to |
| 132 |
read the whole read buffer. |
| 133 |
:param bool break_on_termchar: Stop reading at a termination character. |
| 134 |
:param kwargs: Keyword arguments for the connection itself. |
| 135 |
:returns bytes: Bytes response of the instrument (including termination). |
| 136 |
""" |
| 137 |
if self.connection.term_char and not break_on_termchar: |
| 138 |
read_termination = self.connection.term_char |
| 139 |
self.connection.term_char = None |
| 140 |
try: |
| 141 |
return self.connection.read_raw(count, **kwargs) |
| 142 |
finally: |
| 143 |
self.connection.term_char = read_termination |
| 144 |
else: |
| 145 |
return self.connection.read_raw(count, **kwargs) |
| 146 |
|
| 147 |
def ask_raw(self, command): |
| 148 |
""" Wrapper function for the ask_raw command using the |
| 149 |
vx11 interface. |
| 150 |
|
| 151 |
.. deprecated:: 0.11 |
| 152 |
Use `Instrument.write_bytes` and `Instrument.read_bytes` instead. |
| 153 |
|
| 154 |
:param command: binary string with the command that will be |
| 155 |
transmitted to the instrument |
| 156 |
|
| 157 |
:returns binary string containing the response from the device. |
| 158 |
""" |
| 159 |
warn("Use `Instrument.write_bytes` and `Instrument.read_bytes` instead.", |
| 160 |
FutureWarning) |
| 161 |
return self.connection.ask_raw(command) |
| 162 |
|
| 163 |
def __repr__(self): |
| 164 |
return f'<VXI11Adapter(host={self.connection.host})>' |