[s][s]procedure TMainForm.doElecState;
var V, I, P, Today, DAverage, solarData: String;
current, voltage, power, excess, used_today, daily_average, cost, tmp, solarI: Real;
tmp1, j, Chg:integer;
LBV: real;
gud: boolean;
begin
if(DataList.Count <> 16) then
exit;
if Assigned(ElecForm) then
begin
V := DataList.Strings[0];
//spurious 1st char sometimes appears. If so, get rid of it!
if (Ord(V[1]) < 48) or (Ord(V[1]) > 57) then Delete(V, 1, 1);
Val(V, tmp, ErrCode);
if ErrCode <> 0 then exit
else
begin
voltage := 2 * (tmp - 200);
ElecForm.VoltBar.Progress := round(voltage);
ElecForm.VdigLabel.Caption := V + 'V';
ElecForm.VdigLabel.Repaint;
end;
I := DataList.Strings[1];
Val(I, current, ErrCode);
if ErrCode <> 0 then exit
else
begin
ElecForm.IdigLabel.Caption := I + 'A';
ElecForm.IdigLabel.Repaint;
//Set appropriate range for display
if current > 10.0 then
begin
ElecForm.IunitsLabel.Caption := HAScale;
ElecForm.CurrentBar.Progress := round(5 * current);
end
else
begin
ElecForm.IunitsLabel.Caption := LAScale;
ElecForm.CurrentBar.Progress := round(10 * current);
end;
end;
P := DataList.Strings[2];
Val(P, power, ErrCode);
if ErrCode <> 0 then exit
else
begin
if power > 1000 then
begin
ElecForm.PUnitsLabel.Caption := HPScale;
ElecForm.Label14.Caption := 'Power (kW):';
ElecForm.PowerBar.Progress := round(power/50);
ElecForm.PdigLabel.Caption := FloatToStrF(power/1000, ffFixed,2,2) + 'kW';
end
else
begin
ElecForm.PUnitsLabel.Caption := LPScale;
ElecForm.Label14.Caption := 'Power (W):';
ElecForm.PowerBar.Progress := round(power / 10);
ElecForm.PdigLabel.Caption := P + 'W';
end;
end;
//add values to StatusList
{ StatusList.Strings[13] := ls + 'V = ' + ElecForm.VdigLabel.Caption + ', I = '
+ ElecForm.IdigLabel.Caption + ', P = ' + ElecForm.PdigLabel.Caption + le; }
//Used today
Today := DataList.Strings[10];
Val(Today, used_today, ErrCode);
if ErrCode <> 0 then exit
else
if ErrCode = 0 then
begin
if(used_today >= 0) and (used_today < 100) then
begin
ElecForm.TodayBar.Progress := round(used_today * 10);
ElecForm.TdigLabel.Caption := Today + 'kWh';
StatusList.Strings[14] := ls + 'Used today = ' + ElecForm.TdigLabel.Caption + le;
ElecForm.TdigLabel.Repaint;
end;
if(used_today > 6.0) then ElecForm.TodayBar.ForeColor := clRed
else
ElecForm.TodayBar.ForeColor := clLime;
end;
//daily average
DAverage := DataList.Strings[9];
Val(DAverage, daily_average, ErrCode);
if ErrCode <> 0 then exit
else
begin
if(daily_average < 20) then
begin
ElecForm.DAvg.Progress := round(daily_average * 5);
ElecForm.DAdigLabel.Caption := DAverage + 'kWh';
//StatusList.Strings[15] := ls + 'Daily Avg = ' + ElecForm.DAdigLabel.Caption + le;
end;
if(daily_average > 6) then ElecForm.DAvg.ForeColor := clRed
else
ElecForm.DAvg.ForeColor := clLime;
end;
//Excess units
Val(DataList.Strings[11], excess, ErrCode);
if ErrCode <> 0 then exit
else
begin
if(excess < 0) then excess := 0;
ElecForm.ExcessUnits.Caption := FloatToStrF(excess, ffFixed,3,1)
+ 'kWh';
end;
//Currency unit
if not Assigned(setupform) then exit;
if curSymbol = '' then
begin
Val(setupData.Strings[3], tmp1, ErrCode);
if ErrCode <> 0 then exit
else
begin
if tmp1 = 1 then
curSymbol := '€'
else
curSymbol := '£';
end;
end;
//Cost
Val(DataList.Strings[12], cost, ErrCode);
if ErrCode <> 0 then exit
else
begin
if cost < 0 then cost := 0;
ElecForm.Cost.Caption := DataList.Strings[12];
//end;
//StatusList.Strings[16] := '';
{ if CurSymbol = '£' then
StatusList.Strings[16] := ls + 'Cost = ' + '£'
+ DataList.Strings[12] + le
else
StatusList.Strings[16] := ls + 'Cost = ' + DataList.Strings[12]
+ '€' + le; }
ElecForm.Cost.Caption := '';
if CurSymbol = '€' then
ElecForm.Cost.Caption := 'Cost = ' + DataList.Strings[12] + CurSymbol
else
ElecForm.Cost.Caption := 'Cost = ' + CurSymbol + DataList.Strings[12];
end;
//process LBV, ChgI
for j := 0 to 5 do
begin
gud := false;
//read SOLAR battery voltage value
solarData := srData('SOLAR', '12548'); //0x3104
//get rid of CR at end of string
if Length(solarData) > 1 then
SetLength(solarData, Length(solarData) - 1);
Val(solarData, LBV, ErrCode);
if ErrCode = 0 then
begin
gud := true;
break;
end;
end;
//final check in case all 5 read attempts failed
if gud = false then exit
else
begin
LBV := LBV/100; //mV to V
ElecForm.LBVValue.Caption := FloatToStrF(LBV, ffFixed,3,2) + 'V';
{StatusList.Strings[18] := ls + 'Pri Bat = '
+ ElecForm.LBVValue.Caption + ' Sec = ' + le;}
//offset as scale starts at 8V
LBV := LBV - 8;
if LBV < 0 then LBV := 0;
ElecForm.LBVBar.Progress := round(LBV * 10);
end;
//Battery charge percentage
for j := 0 to 5 do
begin
gud := false;
solarData := srData('SOLAR', '12570'); //0x311A
if Length(solarData) > 1 then
SetLength(solarData, Length(solarData) - 1);
//
Val(solarData, Chg, ErrCode);
if ErrCode = 0 then
begin
gud := true;
break;
end;
end;
if gud = false then exit
else
begin
if Chg > 25 then ElecForm.BatState.ForeColor := clLime
else ElecForm.BatState.ForeColor := clRed;
ElecForm.BatState.Progress := Chg;
end;
//LBI
Val(DataList.Strings[6], LBI, ErrCode);
if ErrCode <> 0 then exit
else
begin
LBI := LBI - 499;
if LBI < 0 then LBI := 0;
ElecForm.ChgI.Progress := round (LBI/5.843);
ElecForm.LBIValue.Caption := FloatToStrF(LBI/10.5, ffFixed,3,2) + 'A';
end;
//solar charge current
for j := 0 to 5 do
begin
gud := false;
solarData := srData('SOLAR', '12549'); //0x3105
//get rid of CR at end of string, only if string isn't empty
//or an Access violation occurs
if Length(solarData) > 1 then
SetLength(solarData, Length(solarData) - 1);
Val(solarData, solarI, ErrCode);
if ErrCode = 0 then
begin
gud := true;
break;
end;
end;
if gud = false then exit
else
begin
//mA to A
if solarI < 0 then solarI := 0;
solarI := solarI/100;
ElecForm.SolarIval.Caption := FloatToStrF(solarI, ffFixed,3,2) + 'A';
ElecForm.SolarBar.Progress := 2 * round(solarI);
end;
{StatusList.Strings[19] := ls + 'Load = ' + ElecForm.LBIValue.Caption
+ ', Mchg = ???A, ' + 'Schg = ' + ElecForm.SolarIval.Caption + le;}
end;
end;[/s][/s]