-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathpolyscience_4100.py
More file actions
132 lines (114 loc) · 3.9 KB
/
polyscience_4100.py
File metadata and controls
132 lines (114 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
""" Driver and test case for Polyscience 4100 """
from __future__ import print_function
import serial
from PyExpLabSys.common.supported_versions import python2_and_3
python2_and_3(__file__)
class Polyscience4100(object):
""" Driver for Polyscience 4100 chiller """
def __init__(self, port='/dev/ttyUSB0'):
self.ser = serial.Serial(port, 9600, timeout=0.5)
self.max_setpoint = 30
self.min_setpoint = 10
assert self.min_setpoint < self.max_setpoint
def comm(self, command):
""" Send serial commands to the instrument """
command = command + '\r'
command = command.encode('ascii')
self.ser.write(command)
reply = self.ser.readline().decode()
return reply[:-1]
def set_setpoint(self, value):
""" Set the temperature setpoint """
if value > self.max_setpoint:
value = self.max_setpoint
if value < self.min_setpoint:
value = self.min_setpoint
string = '{0:.0f}'.format(value)
if len(string) == 1:
string = '00' + string
else:
string = '0' + string
assert len(string) == 3
value = self.comm('SS' + string)
success = value == '!'
return success
def turn_unit_on(self, turn_on):
""" Turn on or off the unit """
if turn_on is True:
value = self.comm('SO1')
if turn_on is False:
value = self.comm('SO0')
return value
def read_setpoint(self):
""" Read the current value of the setpoint """
try:
value = float(self.comm('RS'))
except ValueError:
value = float('NaN')
return float(value)
def read_unit(self):
""" Read the measure unit """
value = self.comm('RU')
return value
def read_temperature(self):
""" Read the actual temperature of the water """
try:
status = self.comm('RW')
if status == '1':
value = float(self.comm('RT'))
else:
value = float('nan')
except ValueError:
value = float('nan')
return value
def read_pressure(self):
""" Read the output pressure """
try:
status = self.comm('RW')
if status == '1':
value = float(self.comm('RK')) / 100.0
else:
value = float('nan')
except ValueError:
value = float('nan')
return value
def read_flow_rate(self):
""" Read the flow rate """
try:
status = self.comm('RW')
if status == '1':
value = float(self.comm('RL'))
else:
value = float('nan')
except ValueError:
value = float('nan')
return value
def read_ambient_temperature(self):
""" Read the ambient temperature in the device """
try:
status = self.comm('RW')
if status == '1':
value = float(self.comm('RA'))
else:
value = float('nan')
except ValueError:
value = float('nan')
return value
def read_status(self):
""" Answers if the device is turned on """
value = self.comm('RW')
status = 'error'
if value == '0':
status = 'Off'
if value == '1':
status = 'On'
return status
if __name__ == '__main__':
CHILLER = Polyscience4100('/dev/ttyUSB0')
print(CHILLER.read_status())
print('Setpoint: {0:.1f}'.format(CHILLER.read_setpoint()))
print('Temperature: {0:.1f}'.format(CHILLER.read_temperature()))
print('Flow rate: {0:.2f}'.format(CHILLER.read_flow_rate()))
print('Pressure: {0:.3f}'.format(CHILLER.read_pressure()))
print('Status: ' + CHILLER.read_status())
print('Ambient temperature: {0:.2f}'.format(CHILLER.read_ambient_temperature()))