-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_write_commands.py
More file actions
executable file
·147 lines (114 loc) · 4.77 KB
/
Copy pathtest_write_commands.py
File metadata and controls
executable file
·147 lines (114 loc) · 4.77 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
#!/usr/bin/env python3
"""
Test Write Commands for iCharger 308 DUO
This script tests control register writes (0x8000).
WARNING: This can start charging/discharging if battery is connected!
"""
import sys
import time
from icharger import ICharger, Operation, Order
def test_stop_command(charger: ICharger, channel: int) -> bool:
"""Test STOP command (safe - just stops any running operation)"""
print(f"\nTesting STOP command on channel {channel+1}...")
# First read current status
data = charger.read_channel(channel)
if data is None:
print(f" ERROR: Could not read channel {channel+1}")
return False
print(f" Current status: {charger.get_run_status_name(data.run_status)}")
# Send STOP command
result = charger.stop(channel)
if result:
print(f" SUCCESS: STOP command sent")
else:
print(f" FAILED: {charger.modbus.get_error_string()}")
return False
# Verify status after STOP
time.sleep(0.5)
data = charger.read_channel(channel)
if data:
print(f" Status after STOP: {charger.get_run_status_name(data.run_status)}")
return result
def test_read_control_registers(charger: ICharger) -> bool:
"""Read current control register values"""
print("\nReading control registers (0x8000-0x8006)...")
regs = charger.modbus.read_holding_registers(0x8000, 7)
if regs is None:
print(f" ERROR: {charger.modbus.get_error_string()}")
return False
print(f" [0] Operation: {regs[0]} ({charger.get_operation_name(regs[0])})")
print(f" [1] Memory: {regs[1]}")
print(f" [2] Channel: {regs[2]}")
print(f" [3] Order Lock: 0x{regs[3]:04X}")
print(f" [4] Order: {regs[4]}")
print(f" [5] Limit Current:{regs[5]} mA")
print(f" [6] Limit Voltage:{regs[6]} mV")
return True
def main():
print("=" * 60)
print("iCharger 308 DUO - Write Commands Test")
print("=" * 60)
port = '/dev/cu.usbserial-A50285BI'
charger = ICharger(port, baudrate=115200, debug=True)
print(f"\nConnecting to {port}...")
if not charger.connect():
print("ERROR: Failed to connect")
sys.exit(1)
print("Connected!")
try:
# Test 1: Read control registers
test_read_control_registers(charger)
# Test 2: STOP commands (safe)
test_stop_command(charger, 0) # CH1
test_stop_command(charger, 1) # CH2
# Test 3: Read control registers again to see if they changed
test_read_control_registers(charger)
print("\n" + "=" * 60)
print("Write command tests completed!")
print("=" * 60)
# Ask user if they want to test START commands
print("\n⚠️ WARNING: The following tests will START charging!")
print(" Only proceed if you have a battery safely connected.")
response = input("\nTest START commands? [y/N]: ").strip().lower()
if response == 'y':
print("\nSelect test:")
print(" [1] Start Storage on CH1")
print(" [2] Start Storage on CH2")
print(" [3] Start Charge on CH1")
print(" [4] Start Discharge on CH1")
print(" [0] Cancel")
choice = input("Choice: ").strip()
if choice == '1':
print("\nStarting Storage on CH1...")
if charger.start_storage(0, memory_slot=0):
print("SUCCESS: Storage started")
time.sleep(2)
data = charger.read_channel(0)
if data:
print(f"Status: {charger.get_run_status_name(data.run_status)}")
print(f"Current: {data.current_amps():.3f}A")
else:
print("FAILED to start storage")
elif choice == '2':
print("\nStarting Storage on CH2...")
if charger.start_storage(1, memory_slot=0):
print("SUCCESS: Storage started")
else:
print("FAILED")
elif choice == '3':
print("\nStarting Charge on CH1...")
if charger.start_charge(0, memory_slot=0):
print("SUCCESS: Charge started")
else:
print("FAILED")
elif choice == '4':
print("\nStarting Discharge on CH1...")
if charger.start_discharge(0, memory_slot=0):
print("SUCCESS: Discharge started")
else:
print("FAILED")
finally:
charger.disconnect()
print("\nDisconnected")
if __name__ == "__main__":
main()