-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathve_aggregator.py
More file actions
593 lines (500 loc) · 18.2 KB
/
Copy pathve_aggregator.py
File metadata and controls
593 lines (500 loc) · 18.2 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ve_aggregator.py — VE.Direct Aggregator client module
Version: 2.0
Provides a single class VEDirect that combines:
- readtext: continuous parsing of the aggregated VE.Direct stream
- sendhex: SET and HEX command interface (readtext_sendhex firmware only)
Usage:
from ve_aggregator import VEDirect
vd = VEDirect('/dev/ttyACM3')
vd.start()
# read device data
data = vd.get_all() # {'SER#': {field: value, ...}}
mppt = vd.get('0xA060:HQ2529K6QK4') # single device or None
# set charge power (readtext_sendhex only)
vd.set_watts('0xA053', 500) # single device by PID
vd.set_watts('ALL', 1500) # all devices
# send arbitrary HEX command
vd.hex_cmd('0xA053', ':154') # restore text mode
vd.hex_cmd('ALL', ':154')
# read replies
while True:
reply = vd.get_reply() # blocks until reply available
print(reply) # 'OK 0xA053 500W 19.5A'
vd.stop()
# or use as context manager:
with VEDirect('/dev/ttyACM3') as vd:
...
Dependencies:
pyserial
See also: vedirect_deaggregator.py — splits the aggregated stream into
individual virtual serial ports for Venus OS / Cerbo GX.
"""
VERSION = '2.0'
from serial import Serial
from threading import Thread, Event, Lock
from queue import Queue, Empty
from time import time, sleep
# ── VE.Direct field conversion ────────────────────────────────────────────────
_STRING_FIELDS = {
'PID', 'SER#', 'FW', 'Checksum', 'MODE', 'WARN', 'ALARM',
'RELAY', 'LOAD', 'AR', 'OR', 'ERR', 'CS', 'MPPT', 'MON',
'Alarm', 'Relay', 'BMV',
}
def _parse_value(name, raw):
"""Convert raw VE.Direct string value to Python type."""
if name in _STRING_FIELDS:
return raw
try:
v = int(raw)
if name in ('V', 'VS', 'VM', 'VPV'): return v / 1000.0 # mV -> V
if name in ('I', 'IL'): return v / 1000.0 # mA -> A
if name in ('CE',): return v / 1000.0 # mAh -> Ah
return v
except (ValueError, TypeError):
pass
try:
return float(raw) # e.g. TEMP field from DS18B20 pseudo-block
except (ValueError, TypeError):
return raw
def _check_checksum(raw_bytes):
"""
Validate VE.Direct block checksum.
The sum of all bytes in the block (including the Checksum byte) must be 0 mod 256.
Returns True if valid, False if invalid.
"""
return sum(raw_bytes) % 256 == 0
def parse_block(raw_bytes):
"""
Parse a complete VE.Direct text block into a dict.
Returns dict with field names as keys, or None if block has no PID or invalid checksum.
"""
if not _check_checksum(raw_bytes):
return None
fields = {}
try:
text = raw_bytes.decode('ascii', errors='replace')
except Exception:
return None
for line in text.splitlines():
line = line.strip()
if not line or line.startswith('Checksum'):
continue
if '\t' not in line:
continue
name, _, raw = line.partition('\t')
name = name.strip(); raw = raw.strip()
if name:
fields[name] = _parse_value(name, raw)
return fields if 'PID' in fields else None
# ── Reply types ───────────────────────────────────────────────────────────────
class Reply:
"""Parsed reply from readtext_sendhex firmware."""
__slots__ = ('raw', 'ok', 'pid', 'watts', 'amps', 'error',
'is_hex', 'hex_response')
def __init__(self, line):
self.raw = line.strip()
self.ok = False
self.pid = None
self.watts = None
self.amps = None
self.error = None
self.is_hex = False
self.hex_response = None
self._parse()
def _parse(self):
parts = self.raw.split()
if not parts: return
if parts[0] == 'OK' and len(parts) >= 4:
# OK <pid> <watts>W <amps>A
self.ok = True
self.pid = parts[1]
try: self.watts = int(parts[2].rstrip('W'))
except Exception: pass
try: self.amps = float(parts[3].rstrip('A'))
except Exception: pass
elif parts[0] == 'ERR' and len(parts) >= 3:
self.pid = parts[1]
self.error = ' '.join(parts[2:])
elif parts[0] == 'HEX_REPLY' and len(parts) >= 3:
self.is_hex = True
self.pid = parts[1]
self.hex_response = parts[2]
def __repr__(self):
return f'Reply({self.raw!r})'
def __str__(self):
return self.raw
# ── Main class ────────────────────────────────────────────────────────────────
class VEDirect:
"""
Unified async client for the VE.Direct Aggregator.
Runs two background threads:
_reader — reads bytes from serial, parses blocks, queues replies
_sender — sends SET/HEX commands from queue, enforces hysteresis
Thread-safe: all public methods can be called from any thread.
"""
def __init__(self, port, baud=19200, on_block=None, on_alive=None, hysteresis_w=50,
device_timeout=5.0, pid_timeout=10.0):
"""
port — serial device, e.g. '/dev/ttyUSB0'
baud — must match BAUD_OUT in firmware (default 19200)
hysteresis_w — minimum watt change before SET is re-sent (default 50)
device_timeout — devices not seen within this time are excluded from
get_all() by default (seconds, default 5.0)
pid_timeout — stale entry age for last_sent hysteresis (seconds)
"""
self.port = port
self.baud = baud
self.hysteresis_w = hysteresis_w
self.device_timeout = device_timeout
self.pid_timeout = pid_timeout
self._data = {} # {ser: {field: value, 'ts': float}}
self._data_lock = Lock()
self._cmd_queue = Queue() # (cmd_str,) — raw command lines
self._reply_queue= Queue() # Reply objects
self._last_sent = {} # {ser: (watts, timestamp)}
self._last_alive = 0.0 # timestamp of last ALIVE signal or data block
self._firmware = None # firmware identification string (from WHO response)
self._on_block = on_block # optional callback(ser, fields) per block
self._on_alive = on_alive # optional callback() on each ALIVE keepalive
self._stop = Event()
self._ser = None
self._ser_lock = Lock()
self._t_reader = None
self._t_sender = None
# ── lifecycle ─────────────────────────────────────────────────────────────
def start(self):
"""Start background threads and probe firmware type."""
self._stop.clear()
self._last_alive = time()
self._firmware = None
print(f've_aggregator v{VERSION} — opening {self.port} at {self.baud} baud')
self._t_reader = Thread(target=self._reader, daemon=True, name='vd-reader')
self._t_sender = Thread(target=self._sender, daemon=True, name='vd-sender')
self._t_reader.start()
self._t_sender.start()
# probe firmware type after brief settle
def _probe():
from time import sleep as _sleep
_sleep(1.0)
self._cmd_queue.put('WHO\n')
Thread(target=_probe, daemon=True).start()
return self
def stop(self):
"""Stop background threads and close serial port."""
self._stop.set()
self._cmd_queue.put(None) # unblock sender
if self._t_reader: self._t_reader.join(timeout=3)
if self._t_sender: self._t_sender.join(timeout=3)
with self._ser_lock:
if self._ser:
try: self._ser.close()
except Exception: pass
self._ser = None
def __enter__(self):
return self.start()
def __exit__(self, *_):
self.stop()
# ── read interface ────────────────────────────────────────────────────────
def get_all(self, max_age=None):
"""
Return dict of all known devices: {ser: {field: value, ...}}
max_age — exclude devices older than this (seconds).
Defaults to self.device_timeout. Pass 0 for no filter.
"""
if max_age is None:
max_age = self.device_timeout
now = time()
with self._data_lock:
if max_age <= 0:
return dict(self._data)
return {ser: dict(d) for ser, d in self._data.items()
if now - d.get('ts', 0) <= max_age}
def get(self, identifier, max_age=None):
"""
Return data for a single device by SER# or PID, or None.
identifier -- SER# (e.g. 'HQ2529K6QK4') or PID (e.g. '0xA060').
SER# is preferred; PID is ambiguous when multiple
devices share the same PID.
max_age -- return None if older than this (seconds).
"""
if max_age is None:
max_age = self.device_timeout
with self._data_lock:
# 1. exact key match by SER#
d = self._data.get(str(identifier))
for v in self._data.values():
if v.get('SER#') == identifier:
d = v
break
# 2. search by SER# field
if d is None:
for v in self._data.values():
if v.get('SER#') == identifier:
d = v
break
if d is None:
return None
if max_age > 0 and time() - d.get('ts', 0) > max_age:
return None
return dict(d)
def is_alive(self, timeout=15.0):
"""
Return True if MCU is alive — received data or ALIVE signal within timeout.
timeout — seconds since last activity (default 15s, firmware sends every 10s)
"""
return time() - self._last_alive < timeout
def keys(self, max_age=None):
"""Return set of all active device keys (PID:SER# or PID)."""
return set(self.get_all(max_age=max_age).keys())
def ser_numbers(self, max_age=None):
"""Return list of SER# strings for all active devices."""
return [d.get('SER#', '') for d in self.get_all(max_age=max_age).values()
if d.get('SER#')]
def combined(self, max_age=None):
"""
Merge all device data into one dict.
Vbat — averaged across all devices
PPV — summed
I — summed
Other fields taken from first device seen
"""
data = self.get_all(max_age=max_age)
result = {}
vbat_vals = []; ppv_sum = 0.0; i_sum = 0.0
for fields in data.values():
for k, v in fields.items():
if k == 'ts': continue
if k == 'V': vbat_vals.append(v)
elif k == 'PPV': ppv_sum += v
elif k == 'I': i_sum += v
elif k not in result: result[k] = v
if vbat_vals: result['Vbat'] = round(sum(vbat_vals)/len(vbat_vals), 3)
if ppv_sum: result['PPV'] = ppv_sum
if i_sum: result['I'] = round(i_sum, 3)
return result
# ── command interface (readtext_sendhex firmware only) ────────────────────
def set_watts(self, ser, watts):
"""
Limit charge power of an MPPT charger.
ser -- SER# string, e.g. 'HQ2529K6QK4', or 'ALL'
watts -- target watts (int >= 0). 0 stops charging.
Applies hysteresis: command is suppressed if value changed by
less than self.hysteresis_w since last sent value.
Replies are queued and readable via get_reply() / get_replies().
"""
ser = 'ALL' if str(ser).upper() == 'ALL' else str(ser)
watts = max(0, int(watts))
# hysteresis check
last = self._last_sent.get(ser)
if last is not None:
last_w, last_t = last
if (abs(watts - last_w) < self.hysteresis_w and
time() - last_t < self.pid_timeout):
return
self._last_sent[ser] = (watts, time())
self._cmd_queue.put(f'SET {ser} {watts}\n')
def hex_cmd(self, ser, hex_str):
"""
Send arbitrary VE.Direct HEX string to a device.
ser -- SER# string or 'ALL'
hex_str -- HEX command, e.g. ':154' or ':154\n'
No hysteresis. Replies readable via get_reply() / get_replies().
Devices return to text mode on their own after HEX commands.
"""
ser = 'ALL' if str(ser).upper() == 'ALL' else str(ser)
hex_str = hex_str.strip()
self._cmd_queue.put(f'HEX {ser} {hex_str}\n')
def restore_text_mode(self, ser='ALL'):
"""Send :154 to device(s) -- only needed if explicitly requested."""
self.hex_cmd(ser, ':154')
# ── reply interface ───────────────────────────────────────────────────────
def get_reply(self, timeout=3.0):
"""
Wait for and return the next Reply object, or None on timeout.
Blocks up to timeout seconds.
"""
try:
return self._reply_queue.get(timeout=timeout)
except Empty:
return None
def get_replies(self, timeout=3.0, inter_reply=0.5):
"""
Collect all Reply objects received within timeout seconds.
After each reply, waits up to inter_reply seconds for more before stopping.
Returns list of Reply objects (may be empty).
"""
replies = []
deadline = time() + timeout
while time() < deadline:
r = self.get_reply(timeout=max(0, deadline - time()))
if r is None: break
replies.append(r)
# wait briefly for additional replies (e.g. SET ALL sends one per device)
t_next = time() + inter_reply
while time() < t_next and time() < deadline:
r2 = self.get_reply(timeout=min(inter_reply, max(0, deadline - time())))
if r2 is None: break
replies.append(r2)
t_next = time() + inter_reply # reset window on each new reply
return replies
def drain_replies(self):
"""Return all currently queued replies without waiting."""
replies = []
while True:
try:
replies.append(self._reply_queue.get_nowait())
except Empty:
break
return replies
# ── background threads ────────────────────────────────────────────────────
def _open_serial(self):
"""Open serial port, return True on success."""
try:
with self._ser_lock:
if self._ser is None:
self._ser = Serial(self.port, self.baud, timeout=0.1)
return True
except Exception:
sleep(2)
return False
def _reader(self):
"""Background thread: read bytes, parse blocks and reply lines."""
buf = bytearray() # current VE.Direct block accumulator
line_buf = bytearray() # current line accumulator (for ALIVE/reply detection)
while not self._stop.is_set():
if not self._open_serial():
continue
try:
with self._ser_lock:
chunk = self._ser.read(256)
except Exception:
with self._ser_lock:
try: self._ser.close()
except Exception: pass
self._ser = None
buf.clear(); line_buf.clear()
continue
for b in chunk:
c = chr(b)
# accumulate line buffer (strip \r, end on \n)
if c == '\r':
pass
elif c == '\n':
line = line_buf.decode('ascii', errors='replace').strip()
line_buf.clear()
if line == 'ALIVE':
self._last_alive = time()
if self._on_alive: self._on_alive()
buf.clear()
continue
if line.startswith(('OK ', 'ERR ', 'HEX_REPLY ')):
self._reply_queue.put(Reply(line))
buf.clear()
continue
# block end: Checksum line
if line.startswith('Checksum\t') and buf:
buf.append(b) # include the \n
self._handle_block(bytes(buf))
buf.clear()
continue
else:
line_buf.append(b)
# accumulate block bytes — skip \r and \n before first data byte
if buf or c not in ('\r', '\n'):
buf.append(b)
if len(buf) > 1024:
buf.clear()
def _handle_block(self, raw):
"""Parse block — either a VE.Direct data block, a reply line or ALIVE."""
try:
text = raw.decode('ascii', errors='replace').strip()
except Exception:
return
# ALIVE keepalive from firmware — update connection timestamp
if text == 'ALIVE':
self._last_alive = time()
if self._on_alive: self._on_alive()
return
# firmware identification reply
if text.startswith('READTEXT ') or text.startswith('SENDHEX '):
self._firmware = text
print(f'firmware: {text}')
return
# single-line replies from sendhex firmware
if text.startswith(('OK ', 'ERR ', 'HEX_REPLY ')):
self._reply_queue.put(Reply(text))
return
# VE.Direct data block
block = parse_block(raw)
if block:
pid = str(block.get('PID', ''))
ser = str(block.get('SER#', ''))
key = ser if ser else pid
block['ts'] = time()
with self._data_lock:
self._data[key] = block
if self._on_block and ser:
self._on_block(ser, dict(block))
self._last_alive = time()
def _sender(self):
"""Background thread: send queued commands over serial."""
while not self._stop.is_set():
try:
cmd = self._cmd_queue.get(timeout=1)
except Empty:
continue
if cmd is None:
break
if not self._open_serial():
# re-queue command
self._cmd_queue.put(cmd)
continue
try:
with self._ser_lock:
self._ser.write(cmd.encode())
self._ser.flush()
except Exception:
with self._ser_lock:
try: self._ser.close()
except Exception: pass
self._ser = None
# re-queue command
self._cmd_queue.put(cmd)
# ── convenience ───────────────────────────────────────────────────────────────
def iter_devices(data):
"""Iterate over get_all() result, yielding (pid, fields)."""
for pid, fields in data.items():
yield pid, fields
# ── example ───────────────────────────────────────────────────────────────────
if __name__ == '__main__':
PORT = '/dev/ttyACM3'
print(f'Connecting to {PORT}...\n')
with VEDirect(PORT, hysteresis_w=50) as vd:
# wait for first data
sleep(2)
# print all device data once per second
for _ in range(5):
sleep(1)
data = vd.get_all()
for pid, fields in iter_devices(data):
age = round(time() - fields.get('ts', 0), 1)
print(f' {pid} V={fields.get("V","?")}V '
f'I={fields.get("I","?")}A '
f'PPV={fields.get("PPV","?")}W '
f'age={age}s')
c = vd.combined()
print(f' combined: Vbat={c.get("Vbat","?")}V '
f'PPV={c.get("PPV","?")}W I={c.get("I","?")}A\n')
# send SET command (readtext_sendhex firmware required)
print('Setting all devices to 1500W...')
vd.set_watts('ALL', 1500)
for r in vd.get_replies(timeout=3):
print(f' {r}')
# send HEX command
print('Sending HEX ping...')
vd.hex_cmd('ALL', ':154')
for r in vd.get_replies(timeout=2):
print(f' {r}')