Add launch configuration for Python debugger and enhance app.py with charging cycle control
This commit is contained in:
parent
e52b0f22e8
commit
034b83723c
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
|
||||||
|
{
|
||||||
|
"name": "Python Debugger: Current File with Arguments",
|
||||||
|
"type": "debugpy",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "app.py",
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"args": "--interval 30 --repeat 2 --quiet COM14",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
90
app.py
90
app.py
@ -7,7 +7,7 @@ and show the charger voltage, battery voltage and charging current
|
|||||||
stop charging in 30 seconds
|
stop charging in 30 seconds
|
||||||
'''
|
'''
|
||||||
from minimalmodbus import Instrument, MODE_RTU
|
from minimalmodbus import Instrument, MODE_RTU
|
||||||
import serial
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
VERSION = '0.1.0'
|
VERSION = '0.1.0'
|
||||||
@ -24,55 +24,79 @@ charger_voltage_adddress = 30
|
|||||||
batery_voltage_address = 32
|
batery_voltage_address = 32
|
||||||
current_address = 31
|
current_address = 31
|
||||||
|
|
||||||
|
|
||||||
|
def monitor_charger(instrument, interval=1, duration=30):
|
||||||
|
"""
|
||||||
|
Monitor the charger voltage, battery voltage and current for a given duration.
|
||||||
|
"""
|
||||||
|
t = 0
|
||||||
|
while t < duration:
|
||||||
|
try:
|
||||||
|
charger_voltage = instrument.read_registers(registeraddress=charger_voltage_adddress,
|
||||||
|
number_of_registers=1,
|
||||||
|
functioncode=MODBUS_FC_READ_INPUT_REGISTERS)
|
||||||
|
battery_voltage = instrument.read_registers(registeraddress=batery_voltage_address,
|
||||||
|
number_of_registers=1,
|
||||||
|
functioncode=MODBUS_FC_READ_INPUT_REGISTERS)
|
||||||
|
current = instrument.read_registers(registeraddress=current_address,
|
||||||
|
number_of_registers=1,
|
||||||
|
functioncode=MODBUS_FC_READ_INPUT_REGISTERS)
|
||||||
|
|
||||||
|
print(
|
||||||
|
f'Time: {t}s - Charger Voltage: {charger_voltage[0]}({charger_voltage[0] / 128:.2f} V ) '
|
||||||
|
f'Battery Voltage: {battery_voltage[0]}({battery_voltage[0] / 128:.2f} V ) '
|
||||||
|
f'Current: {current[0]}({current[0] / 128:.2f} A )')
|
||||||
|
sys.stdout.flush()
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Error reading registers: {e}')
|
||||||
|
pass
|
||||||
|
time.sleep(interval)
|
||||||
|
t += interval
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='xnergy charger control demo')
|
parser = argparse.ArgumentParser(description='xnergy charger control demo')
|
||||||
|
|
||||||
parser.add_argument('port', type=str,
|
parser.add_argument('port', type=str, help='The serial port(RS485) that connect to xnergy RCU')
|
||||||
help='The serial port(RS485) that connect to xnergy RCU')
|
parser.add_argument('-i',
|
||||||
|
'--interval',
|
||||||
|
type=int,
|
||||||
|
default=1800,
|
||||||
|
help='The interval time in seconds to on/off the charger, default is 1800 seconds')
|
||||||
|
parser.add_argument('-r',
|
||||||
|
'--repeat',
|
||||||
|
type=int,
|
||||||
|
default=1,
|
||||||
|
help='The number of times to repeat the charging cycle, default is 1')
|
||||||
|
|
||||||
# parser.add_argument('-P', '--parallel', type=int, nargs='*', choices=(17, 18, 19),
|
# parser.add_argument('-P', '--parallel', type=int, nargs='*', choices=(17, 18, 19),
|
||||||
# help='set to parallel mode, parameter is the unit id, can be used multiple times')
|
# help='set to parallel mode, parameter is the unit id, can be used multiple times')
|
||||||
|
|
||||||
parser.add_argument('-v', '--verbose', action='count', default=0,
|
parser.add_argument('-q', '--quiet', action='store_true', help='quiet mode, suppress all output')
|
||||||
help='verbose mode, show more information')
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
instrument = Instrument(port=args.port, mode=MODE_RTU, slaveaddress=UNIT, debug=True)
|
instrument = Instrument(port=args.port, mode=MODE_RTU, slaveaddress=UNIT, debug=not args.quiet)
|
||||||
instrument.serial.baudrate = 9600
|
instrument.serial.baudrate = 9600
|
||||||
# start charging
|
|
||||||
instrument.write_bit(registeraddress=enable_charger_address, value=1)
|
print(f'Start Charging, will start/stop charging {args.repeat} times in {args.interval} seconds')
|
||||||
print('Start Charging, will stop charging in 30 seconds')
|
|
||||||
|
|
||||||
counter = 0
|
counter = 0
|
||||||
while True:
|
while counter <= args.repeat:
|
||||||
charger_voltage = instrument.read_registers(registeraddress=charger_voltage_adddress, number_of_registers=1,
|
# start charging
|
||||||
functioncode=MODBUS_FC_READ_INPUT_REGISTERS)
|
instrument.write_bit(registeraddress=enable_charger_address, value=1)
|
||||||
battery_voltage = instrument.read_registers(registeraddress=batery_voltage_address, number_of_registers=1,
|
monitor_charger(instrument, duration=args.interval)
|
||||||
functioncode=MODBUS_FC_READ_INPUT_REGISTERS)
|
print()
|
||||||
current = instrument.read_registers(registeraddress=current_address, number_of_registers=1,
|
# stop charging
|
||||||
functioncode=MODBUS_FC_READ_INPUT_REGISTERS)
|
instrument.write_bit(registeraddress=enable_charger_address, value=0)
|
||||||
|
monitor_charger(instrument, duration=args.interval)
|
||||||
print(
|
print()
|
||||||
str(counter) + ' '
|
|
||||||
|
|
||||||
'Charger voltage: ' + str(charger_voltage) + ' '
|
|
||||||
'Battery voltage: ' + str(battery_voltage) + ' '
|
|
||||||
'Current: ' + str(current)
|
|
||||||
, end='\r', flush=True)
|
|
||||||
|
|
||||||
counter += 1
|
counter += 1
|
||||||
if counter > 30:
|
print(f'Charging cycle {counter} completed.')
|
||||||
break
|
|
||||||
time.sleep(1)
|
|
||||||
print()
|
|
||||||
|
|
||||||
# stop charging
|
|
||||||
instrument.write_bit(registeraddress=enable_charger_address, value=0)
|
|
||||||
print('Stop Charging')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user