streamlined

This commit is contained in:
Alex Janka 2022-11-17 10:27:48 +11:00
parent 3bcb1209e6
commit e53f666d23

View file

@ -177,6 +177,10 @@ struct Args {
#[clap(long)] #[clap(long)]
get_serial_ports: bool, get_serial_ports: bool,
/// Use fake data - for testing
#[clap(long)]
fake: bool,
#[clap(subcommand)] #[clap(subcommand)]
command: Option<Commands>, command: Option<Commands>,
} }
@ -187,7 +191,10 @@ enum Commands {
Get { name: String }, Get { name: String },
/// Set single EEPROM value /// Set single EEPROM value
Set { name: String }, Set { name: String, value: f32 },
/// Get all RAM values
GetRam,
} }
struct Info { struct Info {
@ -234,59 +241,120 @@ fn main() {
println!("{}", s); println!("{}", s);
return; return;
} }
let baud = 9600;
let parity = 'N';
let data_bit = 8;
let stop_bit = 2;
if !Path::new(&args.serial_port).exists() { if !Path::new(&args.serial_port).exists() && !args.fake {
println!( println!(
"Serial port {} does not exist\nTry \"mppt-control --help\" for usage instructions", "Serial port {} does not exist\nTry \"mppt-control --help\" for usage instructions",
args.serial_port args.serial_port
); );
// return; return;
} }
let eeprom_data = MpptEeprom { let (_info, ram_data, eeprom_data) = if !args.fake {
ev_absorp: Datapoint::from_u16(0x0000), println!("Connecting to device on {}", args.serial_port);
ev_float: Datapoint::from_u16(0x0000), let modbus = connect_modbus(&args.serial_port);
et_absorp: Datapoint::from_u16(0x0000), get_data(&modbus)
et_absorp_ext: Datapoint::from_u16(0x0000), } else {
ev_absorp_ext: Datapoint::from_u16(0x0000), let info = Info {
ev_float_cancel: Datapoint::from_u16(0x0000), v_scale: 1.,
et_float_exit_cum: Datapoint::from_u16(0x0000), i_scale: 1.,
ev_eq: Datapoint::from_u16(0x0000), };
et_eqcalendar: Datapoint::from_u16(0x0000), let ram_data = MpptRam {
et_eq_above: Datapoint::from_u16(0x0000), v_pu: 0.,
et_eq_reg: Datapoint::from_u16(0x0000), i_pu: 0.,
et_batt_service: Datapoint::from_u16(0x0000), ver_sw: 0,
ev_tempcomp: Datapoint::from_u16(0x0000), adc_vb_f_med: 0.,
ev_hvd: Datapoint::from_u16(0x0000), adc_vbterm_f: 0.,
ev_hvr: Datapoint::from_u16(0x0000), adc_vbs_f: 0.,
evb_ref_lim: Datapoint::from_u16(0x0000), adc_va_f: 0.,
etb_max: Datapoint::from_u16(0x0000), adc_ib_f_shadow: 0.,
etb_min: Datapoint::from_u16(0x0000), adc_ia_f_shadow: 0.,
ev_soc_g_gy: Datapoint::from_u16(0x0000), adc_p12_f: 0.,
ev_soc_gy_y: Datapoint::from_u16(0x0000), adc_p3_f: 0.,
ev_soc_y_yr: Datapoint::from_u16(0x0000), adc_pmeter_f: 0.,
ev_soc_yr_r: Datapoint::from_u16(0x0000), adc_p18_f: 0.,
emodbus_id: Datapoint::from_u16(0x0000), adc_v_ref: 0.,
emeterbus_id: Datapoint::from_u16(0x0000), t_hs: 0,
eib_lim: Datapoint::from_u16(0x0000), t_rts: 0,
eva_ref_fixed_init: Datapoint::from_u16(0x0000), t_batt: 0,
eva_ref_fixed_pct_init: Datapoint::from_u16(0x0000), adc_vb_f_1m: 0.,
adc_ib_f_1m: 0.,
vb_min: 0.,
vb_max: 0.,
hourmeter_hi: 0,
hourmeter_lo: 0,
fault_all: 0,
alarm_hi: 0,
alarm_lo: 0,
dip_all: 0,
led_state: 0,
charge_state: 0,
vb_ref: 0.,
ahc_r_hi: 0,
ahc_r_lo: 0,
ahc_t_hi: 0,
ahc_t_lo: 0,
kwhc_r: 0,
kwhc_t: 0,
power_out_shadow: 0.,
power_in_shadow: 0.,
sweep_pin_max: 0.,
sweep_vmp: 0.,
sweep_voc: 0.,
vb_min_daily: 0.,
vb_max_daily: 0.,
va_max_daily: 0.,
ahc_daily: 0.,
whc_daily: 0,
flags_daily: 0,
pout_max_daily: 0.,
tb_min_daily: 0,
tb_max_daily: 0,
fault_daily: 0,
alarm_daily_hi: 0,
alarm_daily_lo: 0,
time_ab_daily: 0,
time_eq_daily: 0,
time_fl_daily: 0,
ib_ref_slave: 0.,
vb_ref_slave: 0.,
va_ref_fixed: 0.,
va_ref_fixed_pct: 0.,
};
let eeprom_data = MpptEeprom {
ev_absorp: Datapoint::from_u16(0x0000),
ev_float: Datapoint::from_u16(0x0000),
et_absorp: Datapoint::from_u16(0x0000),
et_absorp_ext: Datapoint::from_u16(0x0000),
ev_absorp_ext: Datapoint::from_u16(0x0000),
ev_float_cancel: Datapoint::from_u16(0x0000),
et_float_exit_cum: Datapoint::from_u16(0x0000),
ev_eq: Datapoint::from_u16(0x0000),
et_eqcalendar: Datapoint::from_u16(0x0000),
et_eq_above: Datapoint::from_u16(0x0000),
et_eq_reg: Datapoint::from_u16(0x0000),
et_batt_service: Datapoint::from_u16(0x0000),
ev_tempcomp: Datapoint::from_u16(0x0000),
ev_hvd: Datapoint::from_u16(0x0000),
ev_hvr: Datapoint::from_u16(0x0000),
evb_ref_lim: Datapoint::from_u16(0x0000),
etb_max: Datapoint::from_u16(0x0000),
etb_min: Datapoint::from_u16(0x0000),
ev_soc_g_gy: Datapoint::from_u16(0x0000),
ev_soc_gy_y: Datapoint::from_u16(0x0000),
ev_soc_y_yr: Datapoint::from_u16(0x0000),
ev_soc_yr_r: Datapoint::from_u16(0x0000),
emodbus_id: Datapoint::from_u16(0x0000),
emeterbus_id: Datapoint::from_u16(0x0000),
eib_lim: Datapoint::from_u16(0x0000),
eva_ref_fixed_init: Datapoint::from_u16(0x0000),
eva_ref_fixed_pct_init: Datapoint::from_u16(0x0000),
};
(info, ram_data, eeprom_data)
}; };
match args.command { match args.command {
Some(Commands::Get { name }) => { Some(Commands::Get { name }) => {
// println!(
// "{}: {:#?}",
// name.to_lowercase(),
// match_datapoint(name.as_str(), &eeprom_data).get_scaled(&Info {
// v_scale: 1.,
// i_scale: 1.
// })
// );
let t = match_datapoint_type( let t = match_datapoint_type(
name.as_str(), name.as_str(),
&eeprom_data, &eeprom_data,
@ -298,7 +366,7 @@ fn main() {
println!("{}: {} - {:?}", name, t.val, t.dt); println!("{}: {} - {:?}", name, t.val, t.dt);
return; return;
} }
Some(Commands::Set { name }) => { Some(Commands::Set { name, value: _ }) => {
println!("set var {}", name); println!("set var {}", name);
let t = match_datapoint_type( let t = match_datapoint_type(
name.as_str(), name.as_str(),
@ -309,17 +377,23 @@ fn main() {
}, },
); );
println!("type: {:?}", t); println!("type: {:?}", t);
// let new: Datapoint<{ t }> = Datapoint::from_u16(0x0000);
// let b: Datapoint<_> = Datapoint::<{ t }>::from_u16(0x0000);
return; return;
} }
Some(Commands::GetRam) => {
println!("ram: {:#?}", ram_data);
}
None => { None => {
// println!("ram: {:#?}", ram_data);
println!("eeprom: {:#?}", eeprom_data); println!("eeprom: {:#?}", eeprom_data);
} }
} }
println!("Connecting to device on {}", args.serial_port); }
let mut modbus = Modbus::new_rtu(&args.serial_port, baud, parity, data_bit, stop_bit)
fn connect_modbus(serial_port: &str) -> Modbus {
let baud = 9600;
let parity = 'N';
let data_bit = 8;
let stop_bit = 2;
let mut modbus = Modbus::new_rtu(serial_port, baud, parity, data_bit, stop_bit)
.expect("Could not create modbus device"); .expect("Could not create modbus device");
modbus modbus
.set_slave(DEVICE_ID) .set_slave(DEVICE_ID)
@ -327,6 +401,10 @@ fn main() {
modbus modbus
.connect() .connect()
.expect("Could not connect to client device"); .expect("Could not connect to client device");
return modbus;
}
fn get_data(modbus: &Modbus) -> (Info, MpptRam, MpptEeprom) {
let mut data_in: [u16; RAM_DATA_SIZE as usize + 1] = [0; RAM_DATA_SIZE as usize + 1]; let mut data_in: [u16; RAM_DATA_SIZE as usize + 1] = [0; RAM_DATA_SIZE as usize + 1];
modbus modbus
.read_registers(0x0000, RAM_DATA_SIZE + 1, &mut data_in) .read_registers(0x0000, RAM_DATA_SIZE + 1, &mut data_in)
@ -436,30 +514,7 @@ fn main() {
eva_ref_fixed_pct_init: Datapoint::from_u16(data_in[OffsetsEeprom::EVA_REF_FIXED_PCT_INIT]), eva_ref_fixed_pct_init: Datapoint::from_u16(data_in[OffsetsEeprom::EVA_REF_FIXED_PCT_INIT]),
}; };
match args.command { return (info, ram_data, eeprom_data);
Some(Commands::Get { name }) => {
println!("get var {}", name);
// println!(
// "pp: {:#?}",
// match_datapoint(name.as_str(), &eeprom_data).get_scaled(&info)
// );
return;
}
Some(Commands::Set { name }) => {
println!("set var {}", name);
return;
}
None => {
println!("ram: {:#?}", ram_data);
println!("eeprom: {:#?}", eeprom_data);
}
}
let value = 50.;
let _value_scaled = ((value / info.v_scale) / f32::powf(2., -15.)) as u16;
// modbus
// .write_register(EEPROM_BEGIN as u16 + OffsetsEeprom::EV_soc_g_gy as u16, value_scaled)
// .expect("could not set value");
} }
// fn match_datapoint(name: &str, data: &MpptEeprom) -> Datapoint<{ t }> { // fn match_datapoint(name: &str, data: &MpptEeprom) -> Datapoint<{ t }> {