|
@@ -5,8 +5,10 @@ from umodbus.client.serial import rtu
|
5
|
5
|
import umodbus
|
6
|
6
|
from pprint import pprint
|
7
|
7
|
|
8
|
|
-from .config import config
|
9
|
|
-from .models import *
|
|
8
|
+from pyheatpump.config import config
|
|
9
|
+
|
|
10
|
+from pyheatpump.logger import logger_init
|
|
11
|
+logger = logger_init()
|
10
|
12
|
|
11
|
13
|
serial_conn = None
|
12
|
14
|
|
|
@@ -38,6 +40,9 @@ def read_coils(start, end):
|
38
|
40
|
address = -1
|
39
|
41
|
qty = -1
|
40
|
42
|
|
|
43
|
+ logger.info('Read coils [{}, {}]'.format(
|
|
44
|
+ start, end
|
|
45
|
+ ))
|
41
|
46
|
try:
|
42
|
47
|
for address in range(start, end + 1, 125):
|
43
|
48
|
qty = 125 if (end - address) >= 125 else (end - address)
|
|
@@ -67,6 +72,10 @@ def read_holding_registers(start, end):
|
67
|
72
|
address = -1
|
68
|
73
|
qty = -1
|
69
|
74
|
|
|
75
|
+ logger.info('Read registers [{}, {}]'.format(
|
|
76
|
+ start, end
|
|
77
|
+ ))
|
|
78
|
+
|
70
|
79
|
try:
|
71
|
80
|
for address in range(start, end + 1, 125):
|
72
|
81
|
qty = 125 if (end - address) >= 125 else (end - address)
|
|
@@ -86,37 +95,84 @@ def read_holding_registers(start, end):
|
86
|
95
|
|
87
|
96
|
return res
|
88
|
97
|
|
89
|
|
-def write_coil(var_value: VariableValue):
|
|
98
|
+def write_coil(var_value):
|
90
|
99
|
global serial_conn
|
91
|
100
|
connect()
|
92
|
101
|
|
|
102
|
+ logger.info('Write coil at address {} with value {}]'.format(
|
|
103
|
+ var_value.address, var_value.value
|
|
104
|
+ ))
|
|
105
|
+
|
93
|
106
|
try:
|
94
|
107
|
req_adu = rtu.write_single_coil(
|
95
|
108
|
slave_id=1,
|
96
|
109
|
address=var_value.address,
|
97
|
110
|
value=var_value.value)
|
98
|
|
- return resp.send_message(req_adu, serial_conn)
|
|
111
|
+ resp = rtu.send_message(req_adu, serial_conn)
|
|
112
|
+ logger.info('Modbus response : {}'.format(resp))
|
|
113
|
+ if resp != var_value.value:
|
|
114
|
+ return False
|
|
115
|
+ return True
|
99
|
116
|
except Exception as e:
|
100
|
117
|
raise e
|
101
|
118
|
|
102
|
119
|
|
103
|
|
-def write_holding_register(var_value: VariableValue):
|
|
120
|
+def write_holding_register(var_value):
|
104
|
121
|
global serial_conn
|
105
|
122
|
connect()
|
106
|
123
|
|
|
124
|
+ logger.info('Write register at address {} with value {}]'.format(
|
|
125
|
+ var_value.address, var_value.value
|
|
126
|
+ ))
|
|
127
|
+
|
107
|
128
|
try:
|
108
|
129
|
req_adu = rtu.write_single_register(
|
109
|
130
|
slave_id=1,
|
110
|
131
|
address=var_value.address,
|
111
|
132
|
value=var_value.value)
|
112
|
|
- return resp.send_message(req_adu, serial_conn)
|
|
133
|
+ resp = rtu.send_message(req_adu, serial_conn)
|
|
134
|
+ logger.info('Modbus response : {}'.format(resp))
|
|
135
|
+ if resp != var_value.value:
|
|
136
|
+ return False
|
|
137
|
+ return True
|
113
|
138
|
except Exception as e:
|
114
|
139
|
raise e
|
115
|
140
|
|
116
|
141
|
if __name__ == '__main__':
|
117
|
|
- resp = read_holding_registers(1, 5000)
|
|
142
|
+ resp = read_holding_registers(1, 10)
|
118
|
143
|
#pprint(resp)
|
119
|
144
|
print(len(resp))
|
120
|
|
- resp = read_coils(1, 2000)
|
|
145
|
+ resp = read_coils(90, 100)
|
121
|
146
|
#pprint(resp)
|
122
|
147
|
print(len(resp))
|
|
148
|
+
|
|
149
|
+ from pyheatpump.models.variable_value import VariableValue
|
|
150
|
+ """
|
|
151
|
+ write_coil(VariableValue(**{
|
|
152
|
+ 'type':'D',
|
|
153
|
+ 'address':91,
|
|
154
|
+ 'value':1}))
|
|
155
|
+ """
|
|
156
|
+
|
|
157
|
+ resp = read_coils(91, 93)
|
|
158
|
+ print(f'91 : {resp[0]} - 92 : {resp[1]}')
|
|
159
|
+
|
|
160
|
+ """
|
|
161
|
+ write_coil(VariableValue(**{
|
|
162
|
+ 'type':'D',
|
|
163
|
+ 'address':91,
|
|
164
|
+ 'value':0}))
|
|
165
|
+ """
|
|
166
|
+
|
|
167
|
+ resp = read_coils(91, 93)
|
|
168
|
+ print(f'91 : {resp[0]} - 92 : {resp[1]}')
|
|
169
|
+
|
|
170
|
+ resp = read_holding_registers(240, 242)
|
|
171
|
+ print(resp)
|
|
172
|
+
|
|
173
|
+ """
|
|
174
|
+ write_holding_register(VariableValue(**{
|
|
175
|
+ 'type':'D',
|
|
176
|
+ 'address':91,
|
|
177
|
+ 'value':1})
|
|
178
|
+ """
|