Debian Codegraph @

pymeasure/instruments/hp/hplegacyinstrument.py

raw
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 import ctypes
25 import logging
26 from pymeasure.instruments import Instrument
27
28
29 log = logging.getLogger(__name__)
30 log.addHandler(logging.NullHandler())
31
32 c_uint8 = ctypes.c_uint8
33 c_uint16 = ctypes.c_uint16
34 c_uint32 = ctypes.c_uint32
35
36
37 class StatusBitsBase(ctypes.BigEndianStructure):
38 """
39 A bitfield structure containing the assignments for the status decoding
40 """
41 _pack_ = 1
42 _get_process_ = {}
43
44 # decoder functions
45 # decimal to BCD & BCD to decimal conversion copied from
46 # https://pymodbus.readthedocs.io/en/latest/source/example/bcd_payload.html
47 @staticmethod
48 def _convert_from_bcd(bcd):
49 """Converts a bcd value to a decimal value
50
51 :param value: The value to unpack from bcd
52 :returns: The number in decimal form
53 """
54 place, decimal = 1, 0
55 while bcd > 0:
56 nibble = bcd & 0xF
57 decimal += nibble * place
58 bcd >>= 4
59 place *= 10
60 return decimal
61
62 @staticmethod
63 def _convert_to_bcd(decimal):
64 """Converts a decimal value to a bcd value
65
66 :param value: The decimal value to to pack into bcd
67 :returns: The number in bcd form
68 """
69 place, bcd = 0, 0
70 while decimal > 0:
71 nibble = decimal % 10
72 bcd += nibble << place
73 decimal //= 10
74 place += 4
75 return bcd
76
77 def __str__(self):
78 """
79 Returns a pretty formatted string showing the status of the instrument
80
81 """
82 ret_str = ""
83 for field in self._fields_:
84 ret_str = ret_str + f"{field[0]}: {hex(getattr(self, field[0]))}\n"
85
86 return ret_str
87
88 def __getattribute__(self, name):
89 val = super().__getattribute__(name)
90 if name == "fields":
91 return val
92 if name in self.fields():
93 process = super().__getattribute__('_get_process_')
94 if name in process:
95 val = process[name](val)
96
97 return val
98
99 def fields(self):
100 return [desc[0] for desc in super().__getattribute__('_fields_')]
101
102
103 class HPLegacyInstrument(Instrument):
104 """
105 Class for legacy HP instruments from the era before SPCI, based on `pymeasure.Instrument`
106
107 """
108 status_desc = StatusBitsBase # To be overriden by subclasses
109
110 def __init__(self, adapter, name="HP legacy instrument", **kwargs):
111 super().__init__(
112 adapter, name,
113 includeSCPI=False,
114 **kwargs,
115 )
116
117 self.status_bytes_count = ctypes.sizeof(self.status_desc)
118 self.status_bits = self.status_desc
119
120 log.info(f"Initializing {self.name}")
121
122 def write(self, command):
123 if command == "B":
124 self.write_bytes(b"B")
125 else:
126 super().write(command)
127
128 def values(self, command, **kwargs):
129 if command == "B":
130 self.write_bytes(b"B")
131 return self.read_bytes(-1, **kwargs)
132 else:
133 return super().values(command, **kwargs)
134
135 @property
136 def status(self):
137 """
138 Get an object representing the current status of the unit.
139
140 """
141 self.write_bytes(b"B")
142 reply = bytearray(self.read_bytes(self.status_bytes_count))
143 return self.status_bits.from_buffer(reply)
144
145 def GPIB_trigger(self):
146 """
147 Initate trigger via low-level GPIB-command (aka GET - group execute trigger)
148
149 """
150 self.adapter.connection.assert_trigger()
151
152 def reset(self):
153 """
154 Initatiates a reset (like a power-on reset) of the HP3478A
155
156 """
157 self.adapter.connection.clear()
158
159 def shutdown(self):
160 """
161 provides a way to gracefully close the connection to the HP3478A
162
163 """
164 self.adapter.connection.clear()
165 self.adapter.close()
166 super().shutdown()