Created
June 6, 2023 15:26
-
-
Save bodokaiser/c3a9ac8e3f4b2f333c5c6b5a92d1e9a5 to your computer and use it in GitHub Desktop.
Python class wrapping the picosdk interface for the Pico Logger USB TC-08
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
import numpy as np | |
import numpy.typing as npt | |
from picosdk.usbtc08 import usbtc08 as tc08 | |
from picosdk.functions import assert_pico2000_ok | |
MAX_CHANNELS = 8 | |
class USBTC08: | |
def __init__(self) -> None: | |
self._handle = None | |
def open(self, thermocouple_type="K") -> None: | |
if self._handle is not None: | |
raise RuntimeError("connection already open") | |
code = tc08.usb_tc08_open_unit() | |
assert_pico2000_ok(code) | |
self._handle = code | |
code = tc08.usb_tc08_set_mains(self._handle, 0) | |
assert_pico2000_ok(code) | |
for channel in range(1, MAX_CHANNELS + 1): | |
code = tc08.usb_tc08_set_channel( | |
self._handle, channel, ord(thermocouple_type) | |
) | |
assert_pico2000_ok(code) | |
def read( | |
self, units=tc08.USBTC08_UNITS["USBTC08_UNITS_CENTIGRADE"] | |
) -> npt.NDArray[np.float64]: | |
if self._handle is None: | |
raise RuntimeError("open connection first") | |
temperature = (ctypes.c_float * MAX_CHANNELS)() | |
overflow = ctypes.c_int16(0) | |
code = tc08.usb_tc08_get_single( | |
self._handle, ctypes.byref(temperature), ctypes.byref(overflow), units | |
) | |
assert_pico2000_ok(code) | |
return np.asarray(temperature) | |
def close(self) -> None: | |
if self._handle is None: | |
raise RuntimeError("open connection first") | |
code = tc08.usb_tc08_close_unit(self._handle) | |
assert_pico2000_ok(code) | |
if __name__ == "__main__": | |
pl = USBTC08() | |
pl.open() | |
while True: | |
try: | |
print(pl.read()) | |
except (KeyboardInterrupt, SystemExit): | |
break | |
pl.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment