-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECP202Wrapper.py
142 lines (121 loc) · 4.32 KB
/
ECP202Wrapper.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
import minimalmodbus
from time import sleep
class ECP202:
def __init__(self,port):
self.port=port
self.instrument = minimalmodbus.Instrument(self.port,1,mode=minimalmodbus.MODE_RTU)
#Make the settings explicit
self.instrument.serial.baudrate = 9600 # Baud
self.instrument.serial.bytesize = 8
self.instrument.serial.parity = minimalmodbus.serial.PARITY_NONE
self.instrument.serial.stopbits = 1
self.instrument.serial.timeout = 1
self.instrument.close_port_after_each_call = True
self.instrument.clear_buffers_before_each_transaction = True
#from http://www.pego.it/images/stories/PDF/doc_tecnica/MODBUS-RTU_ECP202_EN.pdf
self.T_AMB_REGISTER = 256
self.T_EV_REGISTER = 257
self.T_SET_REGISTER = 768
self.DELTAT_SET_REGISTER = 769
self.FAN_SET_REGISTER = 777
self.IO_STATUS_REGISTER = 1280
self.ALARM_STATUS_REGISTER = 1282
self.DEVICE_STATUS_REGISTER = 1536
self.STANDBY_MASK = 0x1
self.DEFROST_MASK = 0x4
self.COMPR_STATUS_MASK = 0x1
self.DEFROST_STATUS_MASK = 0x2
self.FAN_STATUS_MASK = 0x4
def getRegister(self,register):
try:
value=self.instrument.read_register(register)
return value
except IOError:
print("Failed to read from instrument")
def setRegister(self,register,value):
try:
valu=self.instrument.write_register(register, value, functioncode=6)
return 0
except IOError:
print("Failed to write to instrument")
return -1
def getAmbientTemperature(self):
value=self.getRegister(self.T_AMB_REGISTER)
if (value<32768):
return value*0.1
else:
return (value-65536)*0.1
def getEvaporatorTemperature(self):
value=self.getRegister(self.T_EV_REGISTER)
if (value<32768):
return value*0.1
else:
return (value-65536)*0.1
def getTargetTemperature(self):
value=self.getRegister(self.T_SET_REGISTER)
if (value<32768):
return value*0.1
else:
return (value-65536)*0.1
def setTargetTemperature(self,value):
if (value<0):
value=65536-int(value*10)
else:
value=value*10
r=self.setRegister(self.T_SET_REGISTER,value)
return r
def getDeltaTemperature(self):
value=self.getRegister(self.DELTAT_SET_REGISTER)
return value*0.1
def setDeltaTemperature(self,value):
if (value<0):
value=0.2
value=value*10
r=self.setRegister(self.DELTAT_SET_REGISTER,value)
return r
def getFanSettings(self):
value=self.getRegister(self.FAN_SET_REGISTER)
return value
def setFanSettings(self,value):
if (value<0):
value=0
if (value>1):
value=1
r=self.setRegister(self.FAN_SET_REGISTER,value)
return r
def getDeviceStatus(self):
value=self.getRegister(self.DEVICE_STATUS_REGISTER)
return value
def getAlarmStatus(self):
value=self.getRegister(self.ALARM_STATUS_REGISTER)
return value
def getIOStatus(self):
value=self.getRegister(self.IO_STATUS_REGISTER)
return value
def start(self):
status=self.getDeviceStatus()
if (status&self.STANDBY_MASK): # 1 means standby
#put on
value=self.STANDBY_MASK<<8
r=self.setRegister(self.DEVICE_STATUS_REGISTER,value)
return r
else:
return 0 #already on
def standby(self):
status=self.getDeviceStatus()
if not (status&self.STANDBY_MASK): # 0 means on
#standby bit and modify bit on
value=self.STANDBY_MASK<<8|self.STANDBY_MASK
r=self.setRegister(self.DEVICE_STATUS_REGISTER,value)
return r
else:
return 0 #already on
def defrost(self):
status=self.getDeviceStatus()
if not (status&self.DEFROST_MASK): # checking if already defrost is on
#put on
value=self.DEFROST_MASK<<8|self.DEFROST_MASK
r=self.setRegister(self.DEVICE_STATUS_REGISTER,value)
return r
else:
return 0 #already on