Some refactoring after static code analysis

This commit is contained in:
2026-02-08 12:48:29 +01:00
parent bdaaf1fe9f
commit 69ef2c6904
43 changed files with 3244 additions and 1097 deletions

View File

@@ -3,7 +3,7 @@
<POU Name="FB_TFProtocol" Id="{f0c44f0b-70b8-45d0-8720-9bd7e08b6aa6}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK FB_TFProtocol
VAR_INPUT
sIpAddr : STRING;
sIpAddr : STRING(15);
udiPort : UDINT;
xConfirmAlarms : BOOL;
@@ -18,30 +18,27 @@ END_VAR
VAR
// Connection settings
_fbTcpConnection : FB_ClientServerConnection;
_sIpAddr : STRING;
_udiPort : UDINT;
_hSocket : T_HSOCKET;
_xConnect : BOOL := TRUE;
_xConnected : BOOL;
// Socket send
_fbSocketSend : FB_SocketSend;
_iCmdLength : INT;
_timSendTimeout : TIME := T#5S;
// Socket receive
_fbSocketReceive : FB_SocketReceive;
_timReceiveTimeout : TIME := T#5S;
_abReceivedBuffer : ARRAY[0..100] OF BYTE;
_asReceivedResponse : ARRAY[0..9] OF STRING;
_uiBufferPos : UINT;
_udiCounterReceive : UDINT;
_abyReceivedBuffer : ARRAY[0..100] OF BYTE;
_sReceivedResponse : STRING(255);
_udiResponseLength : UDINT;
_udiReceivedBytes : UDINT;
_timPollingTime : TIME := T#1S;
_tonPollTimer : TON;
_xEnableReceiveTimeout : BOOL;
_timReceiveTimeoutTime : TIME := T#5S;
_tonReceiveTimeout : TON;
_uiLastReceivedResponseId : UINT := 0;
// Receive state machine
_iStateReceive : INT;
@@ -49,16 +46,15 @@ VAR
// Command data
_uiCmdId : UINT := 1;
_sCmd : STRING(255);
_sAck : STRING(255);
_sAck : STRING(11);
// Main state machine
_iState : INT := 0;
_xRunSMAgain : BOOL;
_diCounter : DINT;
_xSendCmd : BOOL;
_uiRetries : UINT;
_xCmdReceived : BOOL;
_iPlaceCmdSeperator : INT;
_sTmp : STRING;
_xReceivedResponseOk : BOOL;
_uiReceivedCommandId : UINT;
@@ -67,7 +63,9 @@ VAR
_xError : BOOL;
END_VAR
VAR CONSTANT
{attribute 'naming' := 'off'}
MAX_RETRIES : UINT := 3;
{attribute 'naming' := 'on'}
END_VAR
]]></Declaration>
<Implementation>
@@ -101,17 +99,19 @@ CASE _iStateReceive OF
// Call receive
10:
{analysis -64}
_fbSocketReceive(
sSrvNetId:= '',
hSocket:= _hSocket,
cbLen:= SIZEOF(_abReceivedBuffer) - _udiReceivedBytes,
pDest:= ADR(_abReceivedBuffer) + _udiReceivedBytes,
cbLen:= SIZEOF(_abyReceivedBuffer) - _udiReceivedBytes,
pDest:= ADR(_abyReceivedBuffer) + _udiReceivedBytes,
bExecute:= TRUE,
tTimeout:= _timReceiveTimeout,
bBusy=> ,
bError=> ,
nErrId=> ,
nRecBytes=> );
{analysis +64}
IF (NOT _fbSocketReceive.bBusy) AND (NOT _fbSocketReceive.bError) THEN
_fbSocketReceive(bExecute := FALSE);
@@ -149,374 +149,197 @@ CASE _iStateReceive OF
// Check received data
20:
// Check if received command is complete
MEMCPY(destAddr := ADR(_asReceivedResponse[_uiBufferPos]), srcAddr := ADR(_abReceivedBuffer), n := _udiReceivedBytes);
_uiBufferPos := _uiBufferPos + 1;
IF _uiBufferPos > 9 THEN
_uiBufferPos := 0;
END_IF
MEMCPY(destAddr := ADR(_sReceivedResponse), srcAddr := ADR(_abyReceivedBuffer), n := _udiReceivedBytes);
_udiResponseLength := _udiReceivedBytes;
_udiReceivedBytes := 0;
_xCmdReceived := TRUE;
// Go back to polling wait state
_iStateReceive := 15;
ELSE
;
END_CASE
// CASE _iState OF
// // Wait for active connection to tray feeder
// 0:
// IF _xConnected THEN
// _iState := 10;
// END_IF
//
// // Connected and idle
// 10:
// // Got to disconnected state if connection is lost
// IF (NOT _xConnected) THEN
// _iState := 0;
// END_IF
//
// IF _xSendCmd THEN
// _xSendCmd := FALSE;
// _xBusy := TRUE;
// _iState := 20;
// END_IF
//
// // Check if we received a response without sending a command
// IF _xCmdReceived THEN
// _xCmdReceived := FALSE;
// _iState := 50;
// END_IF
//
// // Send command
// 20:
// _fbSocketSend(
// sSrvNetId:= '',
// hSocket:= _hSocket,
// cbLen:= SIZEOF(_sCmd),
// pSrc:= ADR(_sCmd),
// bExecute:= TRUE,
// tTimeout:= _timSendTimeout,
// bBusy=> ,
// bError=> ,
// nErrId=> );
//
// IF (NOT _fbSocketSend.bBusy) AND (NOT _fbSocketSend.bError) THEN
// _fbSocketSend(bExecute := FALSE);
// _xEnableReceiveTimeout := TRUE;
// _iState := 30;
// END_IF
//
// IF _fbSocketSend.bError THEN
// _fbSocketSend(bExecute := FALSE);
// _iState := 90;
// END_IF
//
// // Wait for response
// 30:
// // Received a response
// IF _xCmdReceived THEN
// _xCmdReceived := FALSE;
// _xEnableReceiveTimeout := FALSE;
// _uiRetries := 0;
// _iState := 40;
// END_IF
//
// // Didnt receive command ack in time
// // So resend the command if max retries are not reached
// IF _tonReceiveTimeout.Q THEN
// _xEnableReceiveTimeout := FALSE;
// _uiRetries := _uiRetries + 1;
//
// // Check if we reached the max number of retries
// IF _uiRetries > MAX_RETRIES THEN
// _iState := 90;
// ELSE
// // Retry by sendind command again
// _iState := 20;
// END_IF
// END_IF
//
// // Check response
// 40:
// // Check for cmd id
// IF (TO_STRING(_sReceivedResponse[0]) <> UINT_TO_STRING(_uiCmdId)) THEN
// // Wrong command id received
// _iState := 900;
// END_IF
//
// // For for response indicator
// IF _sReceivedResponse[1] <> F_ToASC('<') THEN
// // Wrong response indicator
// _iState := 901;
// END_IF
//
// // Check for correct cmd
// _xReceivedResponseOk := TRUE;
// FOR _diCounter := 2 TO 10 DO
// IF _sReceivedResponse[_diCounter] <> _sCmd[_diCounter] THEN
// _xReceivedResponseOk := FALSE;
// EXIT;
// END_IF
// END_FOR
//
// IF _xReceivedResponseOk THEN
// _xBusy := FALSE;
// _iState := 10;
// ELSE
// _iState := 902;
// END_IF
//
// 50:
// // Get command id
// _uiReceivedCommandId := _sReceivedResponse[0] - 16#30;
//
// // Check if it is a new command
// IF (_uiReceivedCommandId <> _uiLastReceivedResponseId) OR (_uiReceivedCommandId = 0) THEN
// _uiLastReceivedResponseId := _uiReceivedCommandId;
// xNewResponseReady := FALSE;
//
// // Send acknowledgement
// _iState := 60;
// END_IF
//
// // Prepare ack response
// 60:
// FOR _diCounter := 0 TO 10 DO
// _sAck[_diCounter] := _sReceivedResponse[_diCounter];
// END_FOR
// _sAck[11] := 0;
//
// IF _sReceivedResponse[1] = F_ToAsc('>') THEN
// _iState := 70;
// ELSE
// _iState := 10;
// END_IF
//
//
// // Send ack response
// 70:
// _fbSocketSend(
// sSrvNetId:= '',
// hSocket:= _hSocket,
// cbLen:= SIZEOF(_sAck),
// pSrc:= ADR(_sAck),
// bExecute:= TRUE,
// tTimeout:= _timSendTimeout,
// bBusy=> ,
// bError=> ,
// nErrId=> );
//
// IF (NOT _fbSocketSend.bBusy) AND (NOT _fbSocketSend.bError) THEN
// _fbSocketSend(bExecute := FALSE);
// _xEnableReceiveTimeout := TRUE;
// _xBusy := FALSE;
// _iState := 10;
// END_IF
//
// IF _fbSocketSend.bError THEN
// _fbSocketSend(bExecute := FALSE);
// _iState := 90;
// END_IF
//
// // Error
// 90:
// _xError := TRUE;
// _xBusy := FALSE;
//
// IF xConfirmAlarms THEN
// _xError := FALSE;
// _uiRetries := 0;
// _iState := 0;
// END_IF
// END_CASE
REPEAT
_xRunSMAgain := FALSE;
CASE _iState OF
// Wait for active connection to tray feeder
0:
IF _xConnected THEN
_iState := 10;
END_IF
// Connected and idle
10:
// Got to disconnected state if connection is lost
IF (NOT _xConnected) THEN
_iState := 0;
END_IF
IF _xSendCmd THEN
_xSendCmd := FALSE;
_xBusy := TRUE;
_iState := 20;
END_IF
// Check if we received a response without sending a command
IF _xCmdReceived THEN
_xCmdReceived := FALSE;
_xRunSMAgain := TRUE;
_iState := 50;
END_IF
// Send command
20:
_fbSocketSend(
sSrvNetId:= '',
hSocket:= _hSocket,
cbLen:= INT_TO_UDINT(_iCmdLength),
pSrc:= ADR(_sCmd),
bExecute:= TRUE,
tTimeout:= _timSendTimeout,
bBusy=> ,
bError=> ,
nErrId=> );
IF (NOT _fbSocketSend.bBusy) AND (NOT _fbSocketSend.bError) THEN
_fbSocketSend(bExecute := FALSE);
_xEnableReceiveTimeout := TRUE;
_iState := 30;
END_IF
IF _fbSocketSend.bError THEN
_fbSocketSend(bExecute := FALSE);
_iState := 90;
END_IF
// Wait for response
30:
// Received a response
IF _xCmdReceived THEN
_xCmdReceived := FALSE;
_xEnableReceiveTimeout := FALSE;
_uiRetries := 0;
_xRunSMAgain := TRUE;
_iState := 40;
END_IF
// Didnt receive command ack in time
// So resend the command if max retries are not reached
IF _tonReceiveTimeout.Q THEN
_xEnableReceiveTimeout := FALSE;
_uiRetries := _uiRetries + 1;
// Check if we reached the max number of retries
IF _uiRetries > MAX_RETRIES THEN
_iState := 90;
ELSE
// Retry by sendind command again
_iState := 20;
END_IF
END_IF
// Check response
40:
// Check for cmd id
IF (_sReceivedResponse[0] <> UINT_TO_BYTE(_uiCmdId+16#30)) THEN
// Wrong command id received
_iState := 900;
END_IF
// For for response indicator
IF _sReceivedResponse[1] <> F_ToASC('<') THEN
// Wrong response indicator
_iState := 901;
END_IF
// Check for correct cmd
_xReceivedResponseOk := TRUE;
FOR _diCounter := 2 TO 10 DO
IF _sReceivedResponse[_diCounter] <> _sCmd[_diCounter] THEN
_xReceivedResponseOk := FALSE;
EXIT;
END_IF
END_FOR
IF _xReceivedResponseOk THEN
_xBusy := FALSE;
_iState := 10;
ELSE
_iState := 902;
END_IF
// Got command from tray feeder which needs to be acknowledged
50:
// Copy first 11 bytes
FOR _diCounter := 0 TO 10 DO
_sAck[_diCounter] := _sReceivedResponse[_diCounter];
END_FOR
// Switch command indicator to ack indicator
_sAck[1] := F_ToASC('<');
// Check if it is something we need to acknowledge
IF _sReceivedResponse[1] = F_ToASC('>') THEN
// Get command id
_uiReceivedCommandId := _sReceivedResponse[0] - 16#30;
_xRunSMAgain := TRUE;
_iState := 60;
ELSE
_xRunSMAgain := TRUE;
_iState := 10;
END_IF
// Send ack response
60:
// Ack is always 11 bytes long (11 characters)
_fbSocketSend(
sSrvNetId:= '',
hSocket:= _hSocket,
cbLen:= 11,
pSrc:= ADR(_sAck),
bExecute:= TRUE,
tTimeout:= _timSendTimeout,
bBusy=> ,
bError=> ,
nErrId=> );
IF (NOT _fbSocketSend.bBusy) AND (NOT _fbSocketSend.bError) THEN
_fbSocketSend(bExecute := FALSE);
_xEnableReceiveTimeout := TRUE;
_xBusy := FALSE;
_iState := 10;
END_IF
IF _fbSocketSend.bError THEN
_fbSocketSend(bExecute := FALSE);
_iState := 90;
END_IF
// Error
90:
_xError := TRUE;
_xBusy := FALSE;
IF xConfirmAlarms THEN
_xError := FALSE;
_uiRetries := 0;
_iState := 0;
END_IF
ELSE
;
END_CASE
UNTIL (NOT _xRunSMAgain)
END_REPEAT
// Copy output buffers to outputs
xConnected := _xConnected;
xBusy := _xBusy;
xError := _xError;]]></ST>
</Implementation>
<Method Name="M_Debug" Id="{24e700f8-310e-46c8-98c0-8a10312afeda}">
<Declaration><![CDATA[METHOD M_Debug
VAR_INPUT
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[// CASE _iState OF
// // Wait for active connection to tray feeder
// 0:
// IF _xConnected THEN
// _iState := 10;
// END_IF
//
// // Connected and idle
// 10:
// // Got to disconnected state if connection is lost
// IF (NOT _xConnected) THEN
// _iState := 0;
// END_IF
//
// IF _xSendCmd THEN
// _xSendCmd := FALSE;
// _xBusy := TRUE;
// _iState := 20;
// END_IF
//
// // Send command
// 20:
// _fbSocketSend(
// sSrvNetId:= '',
// hSocket:= _hSocket,
// cbLen:= SIZEOF(_sCmd),
// pSrc:= ADR(_sCmd),
// bExecute:= TRUE,
// tTimeout:= _timSendTimeout,
// bBusy=> ,
// bError=> ,
// nErrId=> );
//
// IF (NOT _fbSocketSend.bBusy) AND (NOT _fbSocketSend.bError) THEN
// _fbSocketSend(bExecute := FALSE);
// _xEnableReceiveTimeout := TRUE;
// _iState := 30;
// END_IF
//
// IF _fbSocketSend.bError THEN
// _fbSocketSend(bExecute := FALSE);
// _iState := 90;
// END_IF
//
// // Wait for response
// 30:
// // Received a response
// IF _xCmdReceived THEN
// _xCmdReceived := FALSE;
// _xEnableReceiveTimeout := FALSE;
// _uiRetries := 0;
// _iState := 40;
// END_IF
//
// // Didnt receive command ack in time
// // So resend the command if max retries are not reached
// IF _tonReceiveTimeout.Q THEN
// _xEnableReceiveTimeout := FALSE;
// _uiRetries := _uiRetries + 1;
//
// // Check if we reached the max number of retries
// IF _uiRetries > MAX_RETRIES THEN
// _iState := 90;
// ELSE
// // Retry by sendind command again
// _iState := 20;
// END_IF
// END_IF
//
// // Check response
// 40:
// // Check for cmd id
// IF (TO_STRING(_asReceivedResponse[0]) <> UINT_TO_STRING(_uiCmdId)) THEN
// // Wrong command id received
// _iState := 900;
// END_IF
//
// // For for response indicator
// IF _asReceivedResponse[1] <> F_ToASC('<') THEN
// // Wrong response indicator
// _iState := 901;
// END_IF
//
// // Check for correct cmd
// _xReceivedResponseOk := TRUE;
// FOR _diCounter := 2 TO 10 DO
// IF _asReceivedResponse[_diCounter] <> _sCmd[_diCounter] THEN
// _xReceivedResponseOk := FALSE;
// EXIT;
// END_IF
// END_FOR
//
// IF _xReceivedResponseOk THEN
// _xBusy := FALSE;
// _iState := 10;
// ELSE
// _iState := 902;
// END_IF
//
// 50:
// // Get command id
// _uiReceivedCommandId := _asReceivedResponse[0] - 16#30;
//
// // Check if it is a new command
// IF (_uiReceivedCommandId <> _uiLastReceivedResponseId) OR (_uiReceivedCommandId = 0) THEN
// _uiLastReceivedResponseId := _uiReceivedCommandId;
// xNewResponseReady := FALSE;
//
// // Send acknowledgement
// _iState := 60;
// END_IF
//
// // Prepare ack response
// 60:
// FOR _diCounter := 0 TO 10 DO
// _sAck[_diCounter] := _asReceivedResponse[_diCounter];
// END_FOR
// _sAck[11] := 0;
//
// IF _asReceivedResponse[1] = F_ToAsc('>') THEN
// _iState := 70;
// ELSE
// _iState := 10;
// END_IF
//
//
// // Send ack response
// 70:
// _fbSocketSend(
// sSrvNetId:= '',
// hSocket:= _hSocket,
// cbLen:= SIZEOF(_sAck),
// pSrc:= ADR(_sAck),
// bExecute:= TRUE,
// tTimeout:= _timSendTimeout,
// bBusy=> ,
// bError=> ,
// nErrId=> );
//
// IF (NOT _fbSocketSend.bBusy) AND (NOT _fbSocketSend.bError) THEN
// _fbSocketSend(bExecute := FALSE);
// _xEnableReceiveTimeout := TRUE;
// _xBusy := FALSE;
// _iState := 10;
// END_IF
//
// IF _fbSocketSend.bError THEN
// _fbSocketSend(bExecute := FALSE);
// _iState := 90;
// END_IF
//
// // Error
// 90:
// _xError := TRUE;
// _xBusy := FALSE;
//
// IF xConfirmAlarms THEN
// _xError := FALSE;
// _uiRetries := 0;
// _iState := 0;
// END_IF
// END_CASE]]></ST>
</Implementation>
</Method>
<Method Name="M_GetResponse" Id="{48f8719d-7ed0-4fb1-824a-0bf475fcfc2c}">
<Declaration><![CDATA[METHOD M_GetResponse : STRING
VAR
_sTmp : STRING(255);
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[xNewResponseReady := FALSE;
//MEMCPY(destAddr := ADR(_sTmp), ADR(_asReceivedResponse) + 2, n := INT_TO_UDINT(LEN(_asReceivedResponse) - 2));
M_GetResponse := _sTmp;]]></ST>
</Implementation>
</Method>
<Method Name="M_SendCmd" Id="{d00a8c1a-c183-4659-9c9e-be5b30566b7c}">
<Declaration><![CDATA[METHOD M_SendCmd : BOOL
VAR_INPUT
@@ -541,6 +364,7 @@ END_IF
// Create command with id
_sCmd := CONCAT(UINT_TO_STRING(_uiCmdId), '>');
_sCmd := CONCAT(_sCmd, sCmd);
_iCmdLength := LEN(sCmd);
// start sendind command state machine
_xSendCmd := TRUE;

View File

@@ -8,14 +8,14 @@ END_VAR
VAR_OUTPUT
END_VAR
VAR
_sIpAddr : STRING;
_sIpAddr : STRING(15);
_udiPort : UDINT;
_fbProtocolHandler : FB_TFProtocol;
_xTest : BOOL;
_xSendResult : BOOL;
_sCmd : STRING := 'STAT-FEED';
_sCmd : STRING := 'STAT-FEED:';
END_VAR
]]></Declaration>
<Implementation>
@@ -50,7 +50,7 @@ VAR_INPUT
bInitRetains: BOOL; // TRUE: Die Retain-Variablen werden initialisiert (Reset warm / Reset kalt)
bInCopyCode: BOOL; // TRUE: Die Instanz wird danach in den Kopiercode kopiert (Online-Change)
sIPAddr : STRING;
sIPAddr : STRING(15);
udiPort : UDINT;
END_VAR]]></Declaration>
<Implementation>