-
Notifications
You must be signed in to change notification settings - Fork 4
/
us2000.py
194 lines (158 loc) · 7.41 KB
/
us2000.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
import logging
import time
from datetime import datetime
from threading import Thread
import serial # pip install pyserial
from pylontech import read_analog_value, read_alarm_info
"""
Pylontech / US2000 service class for cyclic polling inside a thread.
See demo_us2000.py for a simple example.
{
'analog': [
{'u_cell': (3225, 3224, 3225, 3224, 3226, 3226, 3225, 3226, 3228, 3225, 3227, 3227, 3227, 3226, 3225),
't': [17.5, 16.8, 16.9, 16.5, 17.2],
'q': 6415, 'q_total': 50000, 'cycle': 132, 'i': -0.1, 'u': 48.386, 'soc': 13},
{'u_cell': (3229, 3228, 3229, 3226, 3227, 3227, 3227, 3226, 3228, 3228, 3229, 3226, 3226, 3228, 3228),
't': [20.0, 17.0, 16.0, 17.0, -100.0],
'q': 4815, 'q_total': 50000, 'cycle': 376, 'i': 0.0, 'u': 48.412, 'soc': 10}
],
'alarm': [
{'u_cell': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
't': [0, 0, 0, 0, 0], 'i_chg': 0, 'u_pack': 0, 'i_dis': 0, 'status': [0, 14, 64, 0, 0], 'ready': True},
{'u_cell': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
't': [0, 0, 0, 0, 0], 'i_chg': 0, 'u_pack': 0, 'i_dis': 0, 'status': [0, 14, 0, 0, 0], 'ready': True}
],
'analog_timeout': [22300564.212521262, 22300564.212521262],
'alarm_timeout': [22300564.212521262, 22300564.212521262]
}
30.11.2022 Martin Steppuhn Split in pylontech.py (baisc packets) and us2000.py (threaded service class)
"""
class US2000:
def __init__(self, port=None, baudrate=115200, pack_number=1, lifetime=10, log_name='us2000', pause=0.25):
"""
Service class with polling thread
:param port: Serial port, '/dev/ttyUSB0'
:param baudrate: Integer, 9600, 115200, ...
:param pack_number: Number of packs
:param lifetime: Time in seconds data is still valid
:param log_name: Name
:param pause: Pause between polling
"""
self.port = port
self.baudrate = baudrate # save baudrate
self.pack_number = pack_number # number of devices
self.pause = pause
self.lifetime = lifetime
self.log = logging.getLogger(log_name)
self.log.info('init port={}'.format(port))
self.com = None
self.data = {
'ready': False,
'error': None,
'u': None,
'i': None,
't': None,
'soc': None,
'u_pack': [None] * self.pack_number,
'i_pack': [None] * self.pack_number,
't_pack': [None] * self.pack_number,
'soc_pack': [None] * self.pack_number,
'cycle_pack': [None] * self.pack_number,
}
self.data_detail = {"analog": [None] * pack_number,
"alarm": [None] * pack_number,
"analog_timeout": [time.perf_counter() + self.lifetime] * pack_number,
"alarm_timeout": [time.perf_counter() + self.lifetime] * pack_number}
self.stats = {"frame_count": 0,
"error_analog": [0]*self.pack_number,
"error_alarm": [0]*self.pack_number}
self.connect()
self.thread = Thread(target=self.run, daemon=True)
self.thread.start()
def connect(self):
try:
self.com = serial.Serial(self.port, baudrate=self.baudrate, timeout=0.5) # non blocking
except Exception as e:
self.com = None
self.log.error("connect: {}".format(e))
def run(self):
while True:
if self.com is None:
self.connect()
else:
self.stats['frame_count'] += 1 # count read cycle
for i in range(self.pack_number):
try:
d = read_analog_value(self.com, i)
self.log.debug("read_analog_value[{}] {}".format(i, d))
self.data_detail['analog'][i] = d
self.data_detail['analog'][i]['time'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.data_detail['analog_timeout'][i] = time.perf_counter() + self.lifetime
except IOError:
self.com = None
self.data_detail['analog'][i] = None
self.log.error("read_analog_value: io port failed")
except Exception as e:
self.data_detail['analog'][i] = None
self.stats['error_analog'][i] += 1 # count error
self.log.debug("EXCEPTION read_analog_value[{}] {}".format(i, e))
self.update()
time.sleep(self.pause)
try:
d = read_alarm_info(self.com, i)
self.log.debug("read_alarm_info[{}] {}".format(i, d))
self.data_detail['alarm'][i] = d
self.data_detail['alarm'][i]['time'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.data_detail['alarm_timeout'][i] = time.perf_counter() + self.lifetime
except IOError:
self.com = None
self.data_detail['alarm'][i] = None
self.log.error("read_alarm_info: io port failed")
except Exception as e:
self.data_detail['alarm'][i] = None
self.stats['error_alarm'][i] += 1 # count error
self.log.debug("EXCEPTION read_alarm_info[{}] {}".format(i, e))
self.update()
time.sleep(self.pause)
def update(self):
t = time.perf_counter()
error = None
ready = True
for i in range(self.pack_number):
try:
if self.data_detail['alarm'][i]['error']: # active error
error = 'alarm'
if self.data_detail['alarm'][i]['ready'] is False: # not ready
ready = False
except:
pass
try:
if self.data_detail['analog'][i]:
self.data['u_pack'][i] = self.data_detail['analog'][i]['u']
self.data['i_pack'][i] = self.data_detail['analog'][i]['i']
self.data['t_pack'][i] = max(self.data_detail['analog'][i]['t'])
self.data['soc_pack'][i] = self.data_detail['analog'][i]['soc']
self.data['cycle_pack'][i] = self.data_detail['analog'][i]['cycle']
except Exception:
self.log.exception("update failed")
pass
if t > self.data_detail['analog_timeout'][i] or t > self.data_detail['alarm_timeout'][i]:
self.data['u_pack'][i] = None
self.data['i_pack'][i] = None
self.data['t_pack'][i] = None
self.data['soc_pack'][i] = None
self.data['cycle_pack'][i] = None
error = 'timeout'
ready = False
try:
self.data['u'] = max(self.data['u_pack'])
self.data['i'] = sum(self.data['i_pack'])
self.data['t'] = max(self.data['t_pack'])
self.data['soc'] = round(sum(self.data['soc_pack']) / self.pack_number)
except:
self.data['u'] = None
self.data['i'] = None
self.data['t'] = None
self.data['soc'] = None
self.data['error'] = error
self.data['ready'] = ready