Function TMainForm.doElecState:Boolean; //
var V, I, P, Today, DAverage, solarData,anyString: String;
current, voltage, power, excess, used_today, daily_average, cost, tmp, solarI: Real;
tmp1, j, Chg:integer;
LBV: real;
gud: boolean;
working_Data:TStringList;// DataList Type;
Function Check_Value_Is_Valid_Integer(Var AString:String; Var AValue:Integer):Boolean;
var VErrorCode;
begin
result:=true;
If AString='' then exit(False);
if (Ord(AString[1]) < 48) or (Ord(AString[1]) > 57) then Delete(AString, 1, 1);
If AString='' then exit(False);
Val(AString, AValue, VErrorCode);
If VErrorCode<>0 then exit(False);
// You can check for valid range if you like ie If ((AValue<-100) or (AValue>200)) then exit(false);
end;
Function Check_Value_Is_Valid_Real(Var AString:String; Var AValue:Real):Boolean;
var VErrorCode;
begin
result:=true;
If AString='' then exit(False);
if (Ord(AString[1]) < 48) or (Ord(AString[1]) > 57) then Delete(AString, 1, 1);
If AString='' then exit(False);
Val(AString, AValue, VErrorCode);
If VErrorCode<>0 then exit(False);
// You can check for valid range if you like ie If ((AValue<-100.00) or (AValue>200.00)) then exit(false);
end;
begin
Result:=False;
Working_Data.TStringList.Create;
Working_Data:=DataList;// Make a copy of DataList immediately; incase it gets changed mid run of routine.
if(Working_Data.Count <> 16) then exit(False); // Exit function with reult set as false
if Assigned(ElecForm) then
begin
v:=Working_Data.Strings[0];
If Check_Value_Is_Valid_Real(v,tmp) Then
begin
voltage := 2 * (tmp - 200);
ElecForm.VoltBar.Progress := round(voltage);
ElecForm.VdigLabel.Caption := V + 'V';
ElecForm.VdigLabel.Repaint;
End
else
begin
Working_Data.Free;
Exit(False);
end;
i:=Working_Data.Strings[1];
If Check_Value_Is_Valid_Real(i,current) Then
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
else
begin
Working_Data.Free;
Exit(False);
end;
P := Working_Data.Strings[2];
If Check_Value_Is_Valid_Real(p,power) Then
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
else
begin
Working_Data.Free;
Exit(False);
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 := Working_Data.Strings[10];
If Check_Value_Is_Valid_Real(today,used_today) 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 := Working_Data.Strings[9];
If Check_Value_Is_Valid_Real(DAverage,daily_average) Then
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
AnyString:= Working_Data.Strings[11];
If Check_Value_Is_Valid_Real(AnyString,excess) Then
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
AnyString:= setupData.Strings[3];
If Check_Value_Is_Valid_Integer(AnyString,tmp1) Then
Begin
if tmp1 = 1 then curSymbol := '€' else curSymbol := '£';
end
else
begin
Working_Data.Free;
Exit(False);
end;
end;
//Cost
AnyString:= Working_Data.Strings[12];
If Check_Value_Is_Valid_Real(AnyString,cost) Then
begin
if cost < 0 then cost := 0;
ElecForm.Cost.Caption := AnyString;
end
else
begin
Working_Data.Free;
Exit(False);
end;
//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];
//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);
If Check_Value_Is_Valid_Real(solarData,LBV) Then
begin
gud := true;
break;
end;
end;
//final check in case all 5 read attempts failed
if gud = false then
begin
Working_Data.Free;
Exit(False);
end
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);
If Check_Value_Is_Valid_Real(solarData,Chg) 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
AnyString:= Working_Data.Strings[6];
If Check_Value_Is_Valid_Real(AnyString,LBI) Then
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
else
begin
Working_Data.Free;
Exit(False);
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);
If Check_Value_Is_Valid_Real(solarData,solarI) Then
begin
gud := true;
break;
end;
end;
if gud = false then
begin
Working_Data.Free;
Exit(False);
end
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;}
Result:=True;
end;
end;