Lazarus 3-7/FPC 3.2.3/ macOS Monterey
My question here is not how do I fix this because I've already found my coding error. My question is why was there no complier error, no exception during execution although it seems to me there should have been.
The purpose of the procedure in question is to ask my PV Inverter for State information, to validate the 8 byte response and decode it to strings.
In the code below the problem line is highlighted. Also included is the first part of the include file showing the definition of arrays TransOK and Trans. I have incorrectly called
gTrans:=TransOk[1] instead of
gTrans:=TransOk[0] and the value that is returned is
51-Command is not implemented which is the first element of the array Trans. I would have expected an exception but there was none and perhaps a range error?
Any ideas to explain this or is it expected behaviour
Procedure DoCmd50(Var iErr:Int32);
{$INCLUDE 'IncCmd50.pp'} //Definition of State Request Codes
//For Transmission, Global, Inverter, DC/DC and Alarm
Const
bb: Byte = $1f; //Test '31-DC inj error
Var
ibb: Int32; //Work var for received byte
i: Int32; //Function return var
k: Int32; //Loop counter
RcvBuf: ar8; //Receive buffer
Begin
Try
For k:= 1 to 3 Do //Try 3 times
Begin
Form1.AdvLed1.Flash(LedFlashDur);
Form1.LazSerial1.SynSer.Purge;
i:=Form1.LazSerial1.SynSer.SendBuffer(@RdState,10);
Sleep (RcvInvWait); //Give time for inverter to respond;
Form1.AdvLed2.Flash(LedFlashDur);
i:=Form1.LazSerial1.SynSer.RecvBufferEx(@RcvBuf,8,RcvWait);
If Form1.LazSerial1.SynSer.LastError <> 0 Then
//Timeout or other error
Begin
WriteLn('Error in Get State Request: Try ' + ' - ' + IntToStr(k) + ' Error= ' + IntToStr(Form1.LazSerial1.SynSer.LastError));
iErr:=i;
End
Else
//Ok response
Begin
//Check if response is valid
If IsResponseValid(RcvBuf, 7) = 0 Then
//Yes, response is good
Begin
//Decode state request
//Alarm State first
ibb:=Integer(RcvBuf[5]);
gAlarmState:=Alarm[ibb];
//Then decode Transmission State
ibb:=Integer(RcvBuf[0]);
If ibb = 0 Then
//Special case, OK
Begin
gTrans:=TransOk[1]; //<<=================
End
Else
//Do look up
Begin
gTrans:=Trans[ibb];
End;{If}
//And DC/DC State Inv 1
ibb:=Integer(RcvBuf[3]);
gDcDc1:=DcDc[ibb];
//And DC/DC State Inv 2
ibb:=Integer(RcvBuf[4]);
gDcDc2:=DcDc[ibb];
//Next Inverter State
ibb:=Integer(RcvBuf[2]);
gInverter:=Inverter[ibb];
//And lastly Global State
ibb:=Integer(RcvBuf[1]);
If ibb >= 98 Then
//Special case
Begin
gGlobal:=Global2[ibb];
End
Else
Begin
//Do Look up
gGlobal:=Global[ibb];
End;{If}
//Output to form for testing
Form1.Edit6.Text:=(gAlarmState + '|' + gTrans + '|' + gDcDc1 + '|' + gDcDc2 + '|' + gInverter + '|' + gGlobal);
Application.ProcessMessages;
iErr:=0;
Break;
End
Else
Begin
//Try again. cause is logged in IsResponseValid
End;{If}
//Try again
End;{If}
End;{For}
Except
On E: Exception Do
Begin
WriteLn('Exception in DoCmd50: ' + E.ClassName + ':' + E.Message);
End;
End;{Try}
End;{Procedure DoCmd50}
//Include State Request definitions for 'IncCmd50.pp'
Const
TransOk: Array[0..0] Of String = ('0-Everything is OK');
Trans: Array[51..58] Of String = ('51-Command is not implemented',
'52-Variable does not exist',
'53-Variable value is out of range',
'54-EEprom not accessible',
'55-Not Toggled Service Mode',
'56-Can not send the command to internal micro',
'57-Command not Executed',
'58-The variable is not available, retry');