-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_status.py
More file actions
89 lines (67 loc) · 2.69 KB
/
Copy pathdebug_status.py
File metadata and controls
89 lines (67 loc) · 2.69 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
#!/usr/bin/env python3
"""
Debug status during discharge operation
"""
import sys
import time
from modbus_usb import ModbusUSB
from icharger_const import (
ADDR_CHANNEL_1, ADDR_CONTROL, ORDER_UNLOCK, ORDER_RUN, ORDER_STOP,
OP_DISCHARGE, get_status_name
)
from icharger_core import read_status, signed16
def main():
modbus = ModbusUSB(timeout=2.0)
if not modbus.connect():
print(f"Failed to connect: {modbus.get_error_string()}")
return 1
print(f"Connected: {modbus.device.get_product_string()}")
channel = 0
base = ADDR_CHANNEL_1
try:
# Read full channel status
print("\n1. Reading initial channel status...")
header = modbus.read_input_registers(base, 8)
if header:
current = signed16(header[2])
print(f" Current: {current * 10}mA")
print(f" Voltage: {header[3]}mV")
print(f" Int Temp: {header[6]/10:.1f}°C")
status_code, error_code, dialog_id = read_status(modbus, channel)
print(f"\n Run Status: {status_code} = {get_status_name(status_code)}")
print(f" Error Code: {error_code}")
print(f" Dialog ID: {dialog_id}")
# Try to start discharge
print("\n2. Starting discharge...")
# Select memory program
modbus.write_multiple_registers(ADDR_CONTROL + 1, [0, 0, ORDER_UNLOCK])
time.sleep(0.1)
# Write operation
modbus.write_multiple_registers(ADDR_CONTROL, [OP_DISCHARGE, 0, 0])
time.sleep(0.1)
# Run
modbus.write_multiple_registers(ADDR_CONTROL + 3, [ORDER_UNLOCK, ORDER_RUN])
time.sleep(0.5)
# Monitor for 5 seconds
print("\n3. Monitoring (5 seconds)...")
print(f"{'Time':<6} {'Status':<20} {'I(mA)':<8} {'V(mV)':<8} {'Error':<6}")
print("-" * 55)
for i in range(50):
time.sleep(0.1)
header = modbus.read_input_registers(base, 8)
status_code, error_code, _ = read_status(modbus, channel)
if header and status_code >= 0:
current = signed16(header[2])
status_name = get_status_name(status_code)
print(f"{i*0.1:<5.1f}s {status_name:<20} {current*10:<8} {header[3]:<8} {error_code:<6}")
if error_code != 0:
print(f" ERROR CODE: {error_code}")
# Stop
print("\n4. Stopping...")
modbus.write_multiple_registers(ADDR_CONTROL + 2, [0, ORDER_UNLOCK, ORDER_STOP])
finally:
modbus.disconnect()
print("Disconnected.")
return 0
if __name__ == "__main__":
sys.exit(main())