-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbustcp.py
343 lines (309 loc) · 13.5 KB
/
modbustcp.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
import time
from datetime import datetime
from pyModbusTCP.client import ModbusClient
from pyModbusTCP.utils import encode_ieee, decode_ieee, \
long_list_to_word, word_list_to_long
HOST = "10.68.42.3"
PORT = 502
class EuroTCP:
def __init__(
self,
host: str = HOST,
port: int = PORT,
) -> None:
self.host = host
self.port = port
self.modbustcp = ModbusClient(host, port)
pass
def get_temp_wsp(self):
"""Return the process value (PV) for loop1."""
self.modbustcp.open()
regs_list_1 = format(self.modbustcp.read_holding_registers(2)[0]*0.1, ".1f")
print(regs_list_1)
print(f"WSP Temp = {regs_list_1} degC")
self.modbustcp.close()
def get_temp_tc(self):
"""Return the process value (PV) for loop1."""
self.modbustcp.open()
regs_list_1 = format(self.modbustcp.read_holding_registers(1)[0]*0.1, ".1f")
print(regs_list_1)
print(f"TC Temp = {regs_list_1} degC")
self.modbustcp.close()
def get_temp_prog(self):
"""Return the process value (PV) for loop1."""
self.modbustcp.open()
regs_list_1 = format(self.modbustcp.read_holding_registers(5)[0]*0.1, ".1f")
print(regs_list_1)
print(f"Prog Temp = {regs_list_1} degC")
self.modbustcp.close()
def get_pw_prog(self):
"""Return the process value (PV) for loop1."""
self.modbustcp.open()
regs_list_1 = format(self.modbustcp.read_holding_registers(85)[0]*0.1, ".1f")
print(regs_list_1)
print(f"Prog Power = {regs_list_1}%")
self.modbustcp.close()
def get_heating_rate(self):
"""Return the process value (PV) for loop1."""
self.modbustcp.open()
regs_list_1 = format(self.modbustcp.read_holding_registers(35)[0]*0.1, ".1f")
print(regs_list_1)
print(f"Heating rate = {regs_list_1} degC/min")
self.modbustcp.close()
def write_wsp(self, sp):
"""Return the process value (PV) for loop1."""
self.modbustcp.open()
sp = int(sp)*10
self.modbustcp.write_single_register(2, sp)
self.get_temp_wsp()
self.modbustcp.close()
def write_heating_rate(self, rate):
"""Return the process value (PV) for loop1."""
self.modbustcp.open()
rate = int(rate)*10
self.modbustcp.write_single_register(35, rate)
self.get_heating_rate()
self.modbustcp.close()
def heating_event(self, rate_sp = None, sp = None):
"""Loops over actual temperature in an heating event until setpoint is reached"""
self.modbustcp.open()
print('Starting heating event:')
try:
print('Heating rate: {} C/min'.format(rate_sp))
rate_sp = int(rate_sp*10)
self.modbustcp.write_single_register(35, rate_sp)
except:
rate_sp = None
try:
print('Setpoint: {} C'.format(sp))
sp = int(sp*10)
self.modbustcp.write_single_register(2, sp)
except:
sp = None
while True:
try:
temp_tc = format(self.modbustcp.read_holding_registers(1)[0]*0.1, ".1f")
temp_programmer = format(self.modbustcp.read_holding_registers(5)[0]*0.1, ".1f")
power_out = format(self.modbustcp.read_holding_registers(85)[0]*0.1, ".1f")
sp = format(self.modbustcp.read_holding_registers(2)[0]*0.1, ".1f")
except IOError:
continue
# print("Failed to read from instrument")
except ValueError:
continue
# print("Instrument response is invalid")
try:
result = float(temp_tc) < float(sp)
if result == True:
temp_tc = float(temp_tc)
# self.pressure_report()
print("-----------------------------------------------------------------------------------------------------\n",
f"Setpoint Temp: {sp} C | Programmer Temp: {temp_programmer} C | Reactor Temp: {temp_tc} C | Power out: {power_out}%\n",
"-----------------------------------------------------------------------------------------------------")
print("\033[F\033[F\033[F", end="")
time.sleep(1)
else:
print('{} C setpoint reached!'.format(sp))
break
except TypeError:
continue
self.modbustcp.close()
def cooling_event(self, rate_sp = None, sp = None):
"""Loops over actual temperature in an heating event until setpoint is reached"""
self.modbustcp.open()
print('Starting cooling event:')
try:
print('Heating rate: {} C/min'.format(rate_sp))
rate_sp = int(rate_sp*10)
self.modbustcp.write_single_register(35, rate_sp)
except:
rate_sp = None
try:
print('Setpoint: {} C'.format(sp))
sp = int(sp*10)
self.modbustcp.write_single_register(2, sp)
except:
sp = None
while True:
try:
temp_tc = format(self.modbustcp.read_holding_registers(1)[0]*0.1, ".1f")
temp_programmer = format(self.modbustcp.read_holding_registers(5)[0]*0.1, ".1f")
power_out = format(self.modbustcp.read_holding_registers(85)[0]*0.1, ".1f")
sp = format(self.modbustcp.read_holding_registers(2)[0]*0.1, ".1f")
except IOError:
continue
# print("Failed to read from instrument")
except ValueError:
continue
# print("Instrument response is invalid")
try:
result = float(temp_tc) > float(sp)
if result == True:
temp_tc = float(temp_tc)
# self.pressure_report()
print("-----------------------------------------------------------------------------------------------------\n",
f"Setpoint Temp: {sp} C | Programmer Temp: {temp_programmer} C | Reactor Temp: {temp_tc} C | Power out: {power_out}%\n",
"-----------------------------------------------------------------------------------------------------")
print("\033[F\033[F\033[F", end="")
time.sleep(1)
else:
print('{} C setpoint reached!'.format(sp))
break
except TypeError:
continue
self.modbustcp.close()
def temperature_ramping_event(self, rate_sp = None, sp = None ):
while True:
try:
temp_tc = format(self.modbustcp.read_holding_registers(1)[0]*0.1, ".1f")
except IOError:
continue
# print("Failed to read from instrument")
except ValueError:
continue
# print("Instrument response is invalid")
try:
result = float(temp_tc) > float(sp)
if result == True:
self.cooling_event(rate_sp, sp)
print('Starting cooling event')
break
else:
self.heating_event(rate_sp, sp)
print('Starting heating event')
break
except TypeError:
continue
def setpoint_finish_experiment(self):
"""Loops over actual temperature in an cooling event until setpoint is reached"""
rate_sp=10
sp=18
print('adjust temperature set point to 18C:')
try:
print(f"Cooling rate: {rate_sp} C/min")
rate_sp = int(rate_sp*10)
self.modbustcp.write_single_register(35, rate_sp)
except:
rate_sp = None
try:
print(f"Setpoint: {sp} C")
sp = int(sp*10)
self.modbustcp.write_single_register(2, sp)
except:
sp = None
def time_event(self, time_in_seconds: int, argument: str):
"""Waits for a specified time while printing the elapsed time on the terminal.
Args:
time_in_seconds (int): The time to wait in seconds.
"""
start_time = time.time()
while True:
elapsed_time = time.time() - start_time
if elapsed_time < time_in_seconds:
temp_tc = format(self.modbustcp.read_holding_registers(1)[0]*0.1, ".1f")
# self.pressure_report()
print("-----------------------------------------------------------------------------------------------------\n",
f"Elapsed time for {str(argument)}: {int(elapsed_time)} seconds at {temp_tc} degC\n",
"-----------------------------------------------------------------------------------------------------")
print("\033[F\033[F\033[F", end="")
time.sleep(1)
else:
print("-----------------------------------------------------------------------------------------------------\n",
f"Wait time of {time_in_seconds} seconds at {temp_tc} degC completed.",
"-------------------------------------------------------------------\n",
"-----------------------------------------------------------------------------------------------------", end="\r")
break
def drift_mantis_pid(self):
self.modbustcp.open()
self.modbustcp.write_multiple_registers(6,[869,0,96,16])
regs_list_2 = self.modbustcp.read_holding_registers(6,4)
p = regs_list_2[0]*0.1
i = regs_list_2[2]
d = regs_list_2[3]
print(f"PID for Harrick Mantis DRIFTS cell is:\nProportional band = {p}\nIntegral time = {i}\nDerivative time = {d}\nPlease switch power output to LOCAL")
self.modbustcp.close()
def clausen_coil_local_pid(self):
self.modbustcp.open()
self.modbustcp.write_multiple_registers(6,[9876,0,96,16])
regs_list_2 = self.modbustcp.read_holding_registers(6,4)
p = regs_list_2[0]*0.1
i = regs_list_2[2]
d = regs_list_2[3]
print(f"PID for clausen cell with coil heating elements and REMOTE power supply is:\nProportional band = {p}\nIntegral time = {i}\nDerivative time = {d}\nPlease switch power output to LOCAL")
self.modbustcp.close()
def clausen_coil_remote_pid(self):
self.modbustcp.open()
self.modbustcp.write_multiple_registers(6,[6000,0,20,4])
regs_list_2 = self.modbustcp.read_holding_registers(6,4)
p = regs_list_2[0]*0.1
i = regs_list_2[2]
d = regs_list_2[3]
print(f"PID for clausen cell with coil heating elements and REMOTE power supply is:\nProportional band = {p}\nIntegral time = {i}\nDerivative time = {d}\nPlease switch power output to REMOTE")
self.modbustcp.close()
def MS_ON(self):
"""Sends a logic value (0 or 1) to perform remote MS digital triggering to RlyAA"""
self.modbustcp.write_single_register(363,0)
time.sleep(1)
print('MS recipe started')
def MS_OFF(self):
"""Sends a logic value (0 or 1) to perform remote MS digital triggering to RlyAA"""
self.modbustcp.write_single_register(363,1)
time.sleep(1)
print('MS recipe stopped')
def IR_ON(self):
"""Sends 5V pulse to perform remote IR triggering to logic A"""
self.modbustcp.write_single_register(376,5)
# value_high = self.modbustcp.read_holding_registers(376)[0]
time.sleep(1)
# print(value_high)
self.modbustcp.write_single_register(376,0)
# value_low = self.modbustcp.read_holding_registers(376)[0]
# print(value_low)
print('IR data acquisition started')
now = datetime.now()
dt_start = now.strftime("%m/%d/%Y %H:%M:%S")
print('\ndate and time =', dt_start)
def pulse_ON(self):
"""Sends 3V to perform remote triggering to logic A"""
self.modbustcp.write_single_register(376,3)
#sleep(1)
#self.write_register(376, 0)
print('Pulse ON')
now = datetime.now()
dt_start = now.strftime("%m/%d/%Y %H:%M:%S")
print('\ndate and time =', dt_start)
def pulse_OFF(self):
"""Sends 0V to perform remote triggering to logic A"""
self.modbustcp.write_single_register(376,0)
#sleep(1)
#self.write_register(376, 0)
print('Pulse OFF')
now = datetime.now()
dt_start = now.strftime("%m/%d/%Y %H:%M:%S")
print('\ndate and time =', dt_start)
def IR_STATUS(self):
"""Sends 5V to perform remote triggering to logic A"""
while True:
try:
result = self.modbustcp.read_holding_registers(361)[0]
# print(result)
except IOError:
continue
# print("Failed to read from instrument")
except ValueError:
continue
# print("Instrument response is invalid")
if result == 1:
break
elif result == 0:
time.sleep(0.1)
continue
if __name__ == "__main__":
eurotcp = EuroTCP()
eurotcp.pid_clausen_coiled()
eurotcp.get_temp_wsp()
eurotcp.get_temp_tc()
eurotcp.get_temp_prog()
eurotcp.get_pw_prog()
eurotcp.get_heating_rate()