changed modbus communication

moved from all requests at the same time to one request after each other through state machine
This commit is contained in:
Markus.Neukirch
2025-09-05 16:20:36 +02:00
parent f0e6143997
commit 755da644bc
3 changed files with 339 additions and 323 deletions

File diff suppressed because one or more lines are too long

View File

@@ -36,6 +36,9 @@ VAR
// Current state
_iState : INT := 0;
// current state for cyclic data read
_iStateCyclicData : INT := 0;
// State for startup state machine
_iStateStartup : INT := 0;
@@ -82,7 +85,7 @@ VAR
_fbReadACValues : FB_MBReadRegs;
// Time for polling for current dc values and check for inverter error
_timPollingDelay : TIME := T#1S;
_timPollingDelay : TIME := T#500MS;
// Time for setting the current power
_timSetPowerDelay : TIME := T#250MS;
@@ -149,9 +152,6 @@ VAR
// Current PCU state and alarm messages
_stPCUState : ST_KACU_PCU;
// Current PCU cabinet temperature
_iCabTemp : INT;
// Current PCU state and alarm messages
_stPCUStateDebug : ST_KACU_PCU;
@@ -355,7 +355,7 @@ CASE _iState OF
// => 10% = 1000
// Writing a register with scaling factor = value / (10^SF)
//_iWSetPct := LREAL_TO_INT((_rPowerInternal*100)/(_rWMax * EXPT(10,-2)));
_iWSetPct := REAL_TO_INT((_rPowerInternal*100) / (_rWMax * EXPT(10,_iWSetPctSF)));
_iWSetPct := LREAL_TO_INT((_rPowerInternal*100) / (_rWMax * EXPT(10,_iWSetPctSF)));
ELSE
_iWSetPct := 0;
END_IF
@@ -466,20 +466,29 @@ END_IF
// Fetch cyclic data with polling timer
_tonPollingTimer(IN := TRUE, PT := _timPollingDelay);
// Write requested state
CASE _iStateCyclicData OF
0: // init state
IF _tonPollingTimer.Q THEN
_tonPollingTimer(IN := FALSE);
_iStateCyclicData := 10;
END_IF
10: // Write requested state
_fbWriteRequestedState(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#01,
nMBAddr:= REQUESTED_STATE_REGISTER,
nValue:= INT_TO_WORD(_eRequestedState),
bExecute:= _tonPollingTimer.Q AND (NOT _fbWriteRequestedState.bBusy),
bExecute:= TRUE,
tTimeout:= _tTimeoutPolling,
bBusy=> ,
bError=> ,
nErrId=> );
IF (NOT _fbWriteRequestedState.bBusy) THEN
_fbWriteRequestedState(bExecute := FALSE);
_iStateCyclicData := 20;
IF _fbWriteRequestedState.bError THEN
_iCurrentErrorCountWRS := _iCurrentErrorCountWRS + 1;
_iErrorCountWRS := _iErrorCountWRS + 1;
@@ -489,8 +498,7 @@ IF (NOT _fbWriteRequestedState.bBusy) THEN
END_IF
END_IF
// Write current power command
20: // Write current power command
_fbWritePowerCommand(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
@@ -499,13 +507,15 @@ _fbWritePowerCommand(
nMBAddr:= W_SET_PCT,
cbLength := SIZEOF(_iWSetPct),
pSrcAddr:= ADR(_iWSetPct),
bExecute:= _tonPollingTimer.Q AND (NOT _fbWritePowerCommand.bBusy),
bExecute:= TRUE,
tTimeout:= _tTimeoutPolling,
bBusy=> ,
bError=> ,
nErrId=> );
IF (NOT _fbWritePowerCommand.bBusy) THEN
_fbWritePowerCommand(bExecute := FALSE);
_iStateCyclicData := 30;
IF _fbWritePowerCommand.bError THEN
_iCurrentErrorCountWPC := _iCurrentErrorCountWPC + 1;
_iErrorCountWPC := _iErrorCountWPC + 1;
@@ -515,8 +525,7 @@ IF (NOT _fbWritePowerCommand.bBusy) THEN
END_IF
END_IF
// Read current state
30: // Read current state
_fbReadCurrentState(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
@@ -525,7 +534,7 @@ _fbReadCurrentState(
nMBAddr:= CURRENT_STATE_REGISTER,
cbLength:= SIZEOF(_eCurrentState),
pDestAddr:= ADR(_eCurrentState),
bExecute:= _tonPollingTimer.Q AND (NOT _fbReadCurrentState.bBusy),
bExecute:= TRUE,
tTimeout:= _tTimeoutPolling,
bBusy=> ,
bError=> ,
@@ -533,6 +542,8 @@ _fbReadCurrentState(
cbRead=> );
IF (NOT _fbReadCurrentState.bBusy) THEN
_fbReadCurrentState(bExecute := FALSE);
_iStateCyclicData := 40;
IF _fbReadCurrentState.bError THEN
_iCurrentErrorCountRCS := _iCurrentErrorCountRCS + 1;
_iErrorCountRCS := _iErrorCountRCS + 1;
@@ -542,14 +553,7 @@ IF (NOT _fbReadCurrentState.bBusy) THEN
END_IF
END_IF
IF _eCurrentState = E_KACO_CURRENT_STATE.GRID_CONNECTED OR _eCurrentState = E_KACO_CURRENT_STATE.THROTTLED THEN
xActive := TRUE;
ELSE
xActive := FALSE;
END_IF
// Read current pcu status
40: // Read current pcu status
_fbReadPCUState(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
@@ -558,7 +562,7 @@ _fbReadPCUState(
nMBAddr:= PCU_STATUS_START_REGISTER,
cbLength:= SIZEOF(_stPCUState),
pDestAddr:= ADR(_stPCUState),
bExecute:= _tonPollingTimer.Q AND (NOT _fbReadPCUState.bBusy),
bExecute:= TRUE,
tTimeout:= _tTimeoutPolling,
bBusy=> ,
bError=> ,
@@ -566,6 +570,8 @@ _fbReadPCUState(
cbRead=> );
IF (NOT _fbReadPCUState.bBusy) THEN
_fbReadPCUState(bExecute := FALSE);
_iStateCyclicData := 50;
IF _fbReadPCUState.bError THEN
_iCurrentErrorCountRPCUS := _iCurrentErrorCountRPCUS + 1;
_iErrorCountRPCUS := _iErrorCountRPCUS + 1;
@@ -578,24 +584,7 @@ IF (NOT _fbReadPCUState.bBusy) THEN
END_IF
END_IF
IF ((_stPCUState.ePCUState = E_KACO_PCU_STATE.ERROR) OR (_stPCUState.ePCUError <> E_KACO_PCU_ERROR.NO_EVENT)) AND (_stPCUState.ePCUError <> 11) THEN
// ignore undervoltage error when not enabled
_stPCUStateDebug := _stPCUState;
IF NOT xReleasePower AND _stPCUState.ePCUError = E_KACO_PCU_ERROR.UNDER_VOLT THEN
_xErrorInverter := FALSE;
ELSE
_xErrorInverter := TRUE;
_stPCUStateDebug2 := _stPCUState;
END_IF
ELSE
_xErrorInverter := FALSE;
END_IF
IF _eCurrentState = E_KACO_CURRENT_STATE.FAULT AND xReleasePower THEN
_xErrorInverter := TRUE;
END_IF
// Read current dc values
50: // Read current dc values
_fbReadDCValues(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
@@ -604,7 +593,7 @@ _fbReadDCValues(
nMBAddr:= DC_VALUES_START_REGISTER,
cbLength:= SIZEOF(_awCurrentDCValues),
pDestAddr:= ADR(_awCurrentDCValues),
bExecute:= _tonPollingTimer.Q AND (NOT _fbReadDCValues.bBusy),
bExecute:= TRUE,
tTimeout:= _tTimeoutPolling,
bBusy=> ,
bError=> ,
@@ -613,6 +602,8 @@ _fbReadDCValues(
// Check if reading modbus register is done
IF (NOT _fbReadDCValues.bBusy) THEN
_fbReadDCValues(bExecute := FALSE);
_iStateCyclicData := 60;
// If there was no error and the converter has no error continue
IF (NOT _fbReadDCValues.bError) THEN
stCurrentValues.rActDCCurrent := LREAL_TO_REAL(WORD_TO_INT(_awCurrentDCValues[0]) * EXPT(10,WORD_TO_INT(_awCurrentDCValues[1])));
@@ -629,8 +620,7 @@ IF (NOT _fbReadDCValues.bBusy) THEN
END_IF
END_IF
// Read current ac values
60: // Read current ac values
_fbReadACValues(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
@@ -639,7 +629,7 @@ _fbReadACValues(
nMBAddr:= AC_VALUES_START_REGISTER,
cbLength:= SIZEOF(_awCurrentACValues),
pDestAddr:= ADR(_awCurrentACValues),
bExecute:= _tonPollingTimer.Q AND (NOT _fbReadACValues.bBusy),
bExecute:= TRUE,
tTimeout:= _tTimeoutPolling,
bBusy=> ,
bError=> ,
@@ -648,6 +638,8 @@ _fbReadACValues(
// Check if reading mudbus register is done
IF (NOT _fbReadACValues.bBusy) THEN
_fbReadACValues(bExecute := FALSE);
_iStateCyclicData := 0;
// If there was no error and the converter has no error continue
IF (NOT _fbReadACValues.bError) THEN
stCurrentValues.rActACCurrent := LREAL_TO_REAL(WORD_TO_INT(_awCurrentACValues[0]) * EXPT(10,WORD_TO_INT(_awCurrentACValues[4])));
@@ -675,7 +667,34 @@ IF (NOT _fbReadACValues.bBusy) THEN
_iErrorIDRACV := _fbReadACValues.nErrId;
END_IF
END_IF
END_CASE
// evaluate inverter state
IF _eCurrentState = E_KACO_CURRENT_STATE.GRID_CONNECTED OR _eCurrentState = E_KACO_CURRENT_STATE.THROTTLED THEN
xActive := TRUE;
ELSE
xActive := FALSE;
END_IF
// evaluate inverter errors
IF ((_stPCUState.ePCUState = E_KACO_PCU_STATE.ERROR) OR (_stPCUState.ePCUError <> E_KACO_PCU_ERROR.NO_EVENT)) AND (_stPCUState.ePCUError <> 11) THEN
// ignore undervoltage error when not enabled
_stPCUStateDebug := _stPCUState;
IF NOT xReleasePower AND _stPCUState.ePCUError = E_KACO_PCU_ERROR.UNDER_VOLT THEN
_xErrorInverter := FALSE;
ELSE
_xErrorInverter := TRUE;
_stPCUStateDebug2 := _stPCUState;
END_IF
ELSE
_xErrorInverter := FALSE;
END_IF
IF _eCurrentState = E_KACO_CURRENT_STATE.FAULT AND xReleasePower THEN
_xErrorInverter := TRUE;
END_IF
// evaluate modbus errors
IF _iCurrentErrorCountRPCUS >= GVL_CONFIG.udiMaxConsecutiveInvError OR
_iCurrentErrorCountRCS >= GVL_CONFIG.udiMaxConsecutiveInvError OR
_iCurrentErrorCountWPC >= GVL_CONFIG.udiMaxConsecutiveInvError OR
@@ -702,11 +721,6 @@ END_IF
IF _fbCyclicDataAlarm.eConfirmationState = TcEventConfirmationState.WaitForConfirmation AND xReset THEN
_fbCyclicDataAlarm.Confirm(0);
END_IF
// Reset polling timer
IF _tonPollingTimer.Q THEN
_tonPollingTimer(IN := FALSE);
END_IF]]></ST>
</Implementation>
</Action>
@@ -786,7 +800,9 @@ END_VAR
<Implementation>
<ST><![CDATA[_sName := Name;
_fbErrorInverterAlarm.ipArguments.Clear().AddString(_sName);]]></ST>
_fbErrorInverterAlarm.ipArguments.Clear().AddString(_sName);
_fbCyclicDataAlarm.ipArguments.Clear().AddString(_sName);
_fbHeartBeatAlarm.ipArguments.Clear().AddString(_sName);]]></ST>
</Implementation>
</Set>
</Property>

View File

@@ -263,7 +263,7 @@
</System>
<Plc>
<Project GUID="{9AE64910-5EB2-4866-93FD-EFE059C38C36}" Name="PLC" PrjFilePath="PLC\PLC.plcproj" TmcFilePath="PLC\PLC.tmc" ReloadTmc="true" AmsPort="851" FileArchiveSettings="#x000e" CopyTmcToTarget="true" CopyTpyToTarget="false" SymbolicMapping="true">
<Instance Id="#x08502000" TcSmClass="TComPlcObjDef" KeepUnrestoredLinks="2" TmcHash="{3B474192-ABB3-08F3-5793-A53CD21DF750}" TmcPath="PLC\PLC.tmc">
<Instance Id="#x08502000" TcSmClass="TComPlcObjDef" KeepUnrestoredLinks="2" TmcHash="{B0096793-1726-0903-97E7-B7D9EB7ED965}" TmcPath="PLC\PLC.tmc">
<Name>PLC Instance</Name>
<CLSID ClassFactory="TcPlc30">{08500001-0000-0000-F000-000000000064}</CLSID>
<Vars VarGrpType="2" AreaNo="1">