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

View File

@@ -263,7 +263,7 @@
</System> </System>
<Plc> <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"> <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> <Name>PLC Instance</Name>
<CLSID ClassFactory="TcPlc30">{08500001-0000-0000-F000-000000000064}</CLSID> <CLSID ClassFactory="TcPlc30">{08500001-0000-0000-F000-000000000064}</CLSID>
<Vars VarGrpType="2" AreaNo="1"> <Vars VarGrpType="2" AreaNo="1">