Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Created June 6, 2023 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodokaiser/c3a9ac8e3f4b2f333c5c6b5a92d1e9a5 to your computer and use it in GitHub Desktop.
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
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