| 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 |
|
| 27 |
from .common_base import CommonBase |
| 28 |
|
| 29 |
log = logging.getLogger(__name__) |
| 30 |
log.addHandler(logging.NullHandler()) |
| 31 |
|
| 32 |
|
| 33 |
class Channel(CommonBase): |
| 34 |
"""The base class for channel definitions. |
| 35 |
|
| 36 |
This class supports dynamic properties like :class:`Instrument`, |
| 37 |
but requires an :class:`Instrument` instance as a parent for communication. |
| 38 |
|
| 39 |
:meth:`insert_id` inserts the channel id into the command string sent to the instrument. |
| 40 |
The default implementation replaces the Channel's `placeholder` (default "ch") |
| 41 |
with the channel id in all command strings (e.g. "CHANnel{ch}:foo"). |
| 42 |
|
| 43 |
:param parent: The instrument (an instance of :class:`~pymeasure.instruments.Instrument`) |
| 44 |
to which the channel belongs. |
| 45 |
:param id: Identifier of the channel, as it is used for the communication. |
| 46 |
""" |
| 47 |
|
| 48 |
placeholder = "ch" |
| 49 |
|
| 50 |
def __init__(self, parent, id): |
| 51 |
self.parent = parent |
| 52 |
self.id = id |
| 53 |
super().__init__() |
| 54 |
|
| 55 |
def insert_id(self, command): |
| 56 |
"""Insert the channel id in a command replacing `placeholder`. |
| 57 |
|
| 58 |
Subclass this method if you want to do something else, |
| 59 |
like always prepending the channel id. |
| 60 |
""" |
| 61 |
return command.format_map({self.placeholder: self.id}) |
| 62 |
|
| 63 |
# Calls to the instrument |
| 64 |
def write(self, command, **kwargs): |
| 65 |
"""Write a string command to the instrument appending `write_termination`. |
| 66 |
|
| 67 |
:param command: command string to be sent to the instrument. |
| 68 |
'{ch}' is replaced by the channel id. |
| 69 |
:param kwargs: Keyword arguments for the adapter. |
| 70 |
""" |
| 71 |
self.parent.write(self.insert_id(command), **kwargs) |
| 72 |
|
| 73 |
def write_bytes(self, content, **kwargs): |
| 74 |
"""Write the bytes `content` to the instrument.""" |
| 75 |
self.parent.write_bytes(content, **kwargs) |
| 76 |
|
| 77 |
def read(self, **kwargs): |
| 78 |
"""Read up to (excluding) `read_termination` or the whole read buffer.""" |
| 79 |
return self.parent.read(**kwargs) |
| 80 |
|
| 81 |
def read_bytes(self, count, **kwargs): |
| 82 |
"""Read a certain number of bytes from the instrument. |
| 83 |
|
| 84 |
:param int count: Number of bytes to read. A value of -1 indicates to |
| 85 |
read the whole read buffer. |
| 86 |
:param kwargs: Keyword arguments for the adapter. |
| 87 |
:returns bytes: Bytes response of the instrument (including termination). |
| 88 |
""" |
| 89 |
return self.parent.read_bytes(count, **kwargs) |
| 90 |
|
| 91 |
def write_binary_values(self, command, values, *args, **kwargs): |
| 92 |
"""Write binary values to the instrument. |
| 93 |
|
| 94 |
:param command: Command to send. |
| 95 |
:param values: The values to transmit. |
| 96 |
:param \\*args, \\**kwargs: Further arguments to hand to the Adapter. |
| 97 |
""" |
| 98 |
self.parent.write_binary_values(self.insert_id(command), |
| 99 |
values, *args, **kwargs) |
| 100 |
|
| 101 |
def read_binary_values(self, **kwargs): |
| 102 |
"""Read binary values from the instrument.""" |
| 103 |
return self.parent.read_binary_values(**kwargs) |
| 104 |
|
| 105 |
def check_errors(self): |
| 106 |
"""Read all errors from the instrument and log them. |
| 107 |
|
| 108 |
:return: List of error entries. |
| 109 |
""" |
| 110 |
return self.parent.check_errors() |
| 111 |
|
| 112 |
def check_get_errors(self): |
| 113 |
"""Check for errors after having gotten a property and log them. |
| 114 |
|
| 115 |
Called if :code:`check_get_errors=True` is set for that property. |
| 116 |
|
| 117 |
If you override this method, you may choose to raise an Exception for certain errors. |
| 118 |
|
| 119 |
:return: List of error entries. |
| 120 |
""" |
| 121 |
return self.parent.check_get_errors() |
| 122 |
|
| 123 |
def check_set_errors(self): |
| 124 |
"""Check for errors after having set a property and log them. |
| 125 |
|
| 126 |
Called if :code:`check_set_errors=True` is set for that property. |
| 127 |
|
| 128 |
If you override this method, you may choose to raise an Exception for certain errors. |
| 129 |
|
| 130 |
:return: List of error entries. |
| 131 |
""" |
| 132 |
return self.parent.check_set_errors() |
| 133 |
|
| 134 |
# Communication functions |
| 135 |
def wait_for(self, query_delay=None): |
| 136 |
"""Wait for some time. Used by 'ask' to wait before reading. |
| 137 |
|
| 138 |
:param query_delay: Delay between writing and reading in seconds. None is default delay. |
| 139 |
""" |
| 140 |
self.parent.wait_for(query_delay) |