Created
July 6, 2023 08:36
-
-
Save bodokaiser/63c760dfbab307a3775eda1a371924c2 to your computer and use it in GitHub Desktop.
Python class for the Stanford Research Instruments (SRI) PTC10 temperature (PID) controller
This file contains hidden or 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
from telnetlib import Telnet | |
class PTC10: | |
def __init__(self, host: str, port=None): | |
self.host = host | |
self.port = port | |
def oven_power(self): | |
with Telnet(self.host, self.port) as conn: | |
return value(conn, "OvenOut") | |
def oven_temperature(self): | |
with Telnet(self.host, self.port) as conn: | |
return value(conn, "OvenTC") | |
def window_current(self): | |
with Telnet(self.host, self.port) as conn: | |
return value(conn, "HWOut") | |
def window_temperature(self): | |
with Telnet(self.host, self.port) as conn: | |
return value(conn, "HWTC") | |
def query(conn, command: str) -> str: | |
""" | |
Writes a command to a telnet connection and returns the response string. | |
""" | |
message = command + "\r\n" | |
conn.write(message.encode()) | |
return conn.read_until(b'\r\n').decode('ascii') | |
def value(conn, channel: str) -> float: | |
""" | |
Returns the value of a channel measurement as float. | |
The response will be of the format "CH Out.Value = 0.1234" and we return 0.1234. | |
""" | |
response = query(conn, f"{channel}.value?") | |
parts = response.split("=") | |
if len(parts) != 2: | |
raise RuntimeError(f"invalid response: {response}") | |
return float(parts[1].strip()) |
This file contains hidden or 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 unittest | |
import time | |
from sri import PTC10 | |
class TestPTC10(unittest.TestCase): | |
def setUp(self): | |
self.ptc10 = PTC10('10.163.103.187') | |
# without sleeping we get a connection refused error maybe server cannot directly accept new connections | |
time.sleep(0.1) | |
def test_oven_power(self): | |
result = self.ptc10.oven_power() | |
self.assertEqual(result, 0.0) | |
def test_oven_temperature(self): | |
result = self.ptc10.oven_temperature() | |
self.assertGreater(result, 0.0) | |
def test_window_current(self): | |
result = self.ptc10.window_current() | |
self.assertGreater(result, 0.5) | |
def test_window_temperature(self): | |
result = self.ptc10.window_temperature() | |
self.assertGreater(result, 350.0) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment