﻿<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://www.plcopen.org/xml/tc6_0200">
  <fileHeader companyName="" productName="IndraLogic" productVersion="indralogic" creationDateTime="2023-06-22T15:56:04.1030146" />
  <contentHeader name="IndraLogic.project" modificationDateTime="2023-06-22T15:52:55.9148375">
    <coordinateInfo>
      <fbd>
        <scaling x="1" y="1" />
      </fbd>
      <ld>
        <scaling x="1" y="1" />
      </ld>
      <sfc>
        <scaling x="1" y="1" />
      </sfc>
    </coordinateInfo>
    <addData>
      <data name="http://www.3s-software.com/plcopenxml/projectinformation" handleUnknown="implementation">
        <ProjectInformation />
      </data>
    </addData>
  </contentHeader>
  <types>
    <dataTypes>
      <dataType name="N4K_LOG_BUFFER">
        <baseType>
          <struct>
            <variable name="iSize">
              <type>
                <INT />
              </type>
            </variable>
            <variable name="strBuffer">
              <type>
                <string length="usiMAX_LOG_STRING_LEN" />
              </type>
            </variable>
          </struct>
        </baseType>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>54c410e8-1394-47d6-9b2d-09c1d6934ac4</ObjectId>
          </data>
        </addData>
      </dataType>
      <dataType name="N4K_MESSAGE">
        <baseType>
          <struct>
            <variable name="pbySem">
              <type>
                <derived name="CXA_SocketComm.SysTypes.RTS_IEC_HANDLE" />
              </type>
              <initialValue>
                <simpleValue value="CXA_SocketComm.SysTypes.RTS_INVALID_HANDLE" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Semaphore used for thread safe access </xhtml>
              </documentation>
            </variable>
            <variable name="bWaitForReply">
              <type>
                <BOOL />
              </type>
              <initialValue>
                <simpleValue value="FALSE" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Flag indicating this message is in use by a N4K_Call </xhtml>
              </documentation>
            </variable>
            <variable name="pstRecvBuffer">
              <type>
                <pointer>
                  <baseType>
                    <derived name="N4K_REPLY" />
                  </baseType>
                </pointer>
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
            </variable>
          </struct>
        </baseType>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>e7f93e9d-b72b-413c-aef0-be3d910df45b</ObjectId>
          </data>
        </addData>
      </dataType>
      <dataType name="N4K_REPLY">
        <baseType>
          <struct>
            <variable name="stHeader">
              <type>
                <derived name="N4K_REPLY_HEADER" />
              </type>
            </variable>
            <variable name="arData">
              <type>
                <array>
                  <dimension lower="0" upper="(uiMAX_SOCKET_REPLY_DATA_SIZE - 1)" />
                  <baseType>
                    <USINT />
                  </baseType>
                </array>
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> reply data bytes </xhtml>
              </documentation>
            </variable>
          </struct>
        </baseType>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
            <Attributes>
              <Attribute Name="pack_mode" Value="8" />
            </Attributes>
          </data>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>8350c0ad-55b7-45bf-a02d-fad6d2ebc37e</ObjectId>
          </data>
        </addData>
      </dataType>
      <dataType name="N4K_REPLY_HEADER">
        <baseType>
          <struct>
            <variable name="uiLength">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> total reply length </xhtml>
              </documentation>
            </variable>
            <variable name="uiMsgId">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> unique message ID </xhtml>
              </documentation>
            </variable>
            <variable name="usiStatus">
              <type>
                <USINT />
              </type>
              <initialValue>
                <simpleValue value="INT_TO_USINT(N4K_STATUS_NONE)" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> success flag of the call </xhtml>
              </documentation>
            </variable>
            <variable name="arPadding">
              <type>
                <array>
                  <dimension lower="0" upper="2" />
                  <baseType>
                    <USINT />
                  </baseType>
                </array>
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> padding to enforce header size to 16 bytes </xhtml>
              </documentation>
            </variable>
          </struct>
        </baseType>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
            <Attributes>
              <Attribute Name="pack_mode" Value="8" />
            </Attributes>
          </data>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>bd93192c-d0d9-46bd-bc2e-98490d717252</ObjectId>
          </data>
        </addData>
      </dataType>
      <dataType name="N4K_REQUEST">
        <baseType>
          <struct>
            <variable name="stHeader">
              <type>
                <derived name="N4K_REQUEST_HEADER" />
              </type>
            </variable>
            <variable name="arData">
              <type>
                <array>
                  <dimension lower="0" upper="(uiMAX_SOCKET_REQUEST_DATA_SIZE - 1)" />
                  <baseType>
                    <USINT />
                  </baseType>
                </array>
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> zero terminated function name followed by request data bytes </xhtml>
              </documentation>
            </variable>
          </struct>
        </baseType>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
            <Attributes>
              <Attribute Name="pack_mode" Value="8" />
            </Attributes>
          </data>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>4da207f7-eb0a-4183-bbad-ba0c918d7cb8</ObjectId>
          </data>
        </addData>
      </dataType>
      <dataType name="N4K_REQUEST_HEADER">
        <baseType>
          <struct>
            <variable name="uiLength">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> total request length </xhtml>
              </documentation>
            </variable>
            <variable name="uiMsgId">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> unique message ID </xhtml>
              </documentation>
            </variable>
            <variable name="uiSizeIn">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> number of request data bytes </xhtml>
              </documentation>
            </variable>
            <variable name="uiSizeOut">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> expected number of reply data bytes </xhtml>
              </documentation>
            </variable>
          </struct>
        </baseType>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
            <Attributes>
              <Attribute Name="pack_mode" Value="8" />
            </Attributes>
          </data>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>df547b05-5210-46d9-9a11-ea6293426b2f</ObjectId>
          </data>
        </addData>
      </dataType>
      <dataType name="N4K_STATUS">
        <baseType>
          <enum>
            <values>
              <value name="N4K_STATUS_NONE" />
              <value name="N4K_STATUS_PROCESSING" />
              <value name="N4K_STATUS_READY" />
              <value name="N4K_STATUS_ERROR" />
              <value name="N4K_STATUS_TIMEOUT" />
              <value name="N4K_STATUS_EXCEPTION" />
              <value name="N4K_STATUS_USERTERMINATED" />
              <value name="N4K_STATUS_TOO_MANY_ACTIVE" />
              <value name="N4K_STATUS_FAILED" />
              <value name="N4K_STATUS_UNKNOWN_REQUEST" />
              <value name="N4K_STATUS_INVALID_REQUEST" />
              <value name="N4K_STATUS_NOT_INITIALIZED" />
              <value name="N4K_STATUS_INVALID_REPLY" />
              <value name="N4K_STATUS_INVALID_INPUT_DATA" />
              <value name="N4K_STATUS_OUT_OF_RESOURCES" />
            </values>
          </enum>
        </baseType>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/enumvaluedocumentation" handleUnknown="implementation">
            <EnumValueDocumentation>
              <EnumValue>
                <Name>N4K_STATUS_TOO_MANY_ACTIVE</Name>
                <Documentation>
                  <xhtml xmlns="http://www.w3.org/1999/xhtml">Too many active N4K_Call function blocks</xhtml>
                </Documentation>
              </EnumValue>
              <EnumValue>
                <Name>N4K_STATUS_NOT_INITIALIZED</Name>
                <Documentation>
                  <xhtml xmlns="http://www.w3.org/1999/xhtml">The N4K_Communication function block is not (yet) initialized/operational.</xhtml>
                </Documentation>
              </EnumValue>
              <EnumValue>
                <Name>N4K_STATUS_INVALID_REPLY</Name>
                <Documentation>
                  <xhtml xmlns="http://www.w3.org/1999/xhtml">The format of the reply message is not as expected</xhtml>
                </Documentation>
              </EnumValue>
            </EnumValueDocumentation>
          </data>
          <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
            <Attributes>
              <Attribute Name="to_string" Value="" />
            </Attributes>
          </data>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>48f61eb1-4d20-4090-bad9-d607efcc5aac</ObjectId>
          </data>
        </addData>
      </dataType>
    </dataTypes>
    <pous>
      <pou name="N4K_Call" pouType="functionBlock">
        <interface>
          <inputVars>
            <variable name="Execute">
              <type>
                <BOOL />
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Execute this function block </xhtml>
              </documentation>
            </variable>
            <variable name="FunctionName">
              <type>
                <string length="80" />
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Name of the Cm socket server function to run </xhtml>
              </documentation>
            </variable>
            <variable name="Timeout">
              <type>
                <UINT />
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> The function places the message on the request queue, within the specified timeout </xhtml>
              </documentation>
            </variable>
            <variable name="SocketServerIndex">
              <type>
                <UINT />
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Cm socket server Index, for multiple subsystems in a network </xhtml>
              </documentation>
            </variable>
            <variable name="InputBuffer">
              <type>
                <pointer>
                  <baseType>
                    <USINT />
                  </baseType>
                </pointer>
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Address of input data buffer </xhtml>
              </documentation>
            </variable>
            <variable name="SizeIn">
              <type>
                <UINT />
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Size of input data buffer </xhtml>
              </documentation>
            </variable>
            <variable name="OutputBuffer">
              <type>
                <pointer>
                  <baseType>
                    <USINT />
                  </baseType>
                </pointer>
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Address of output data buffer </xhtml>
              </documentation>
            </variable>
            <variable name="SizeOut">
              <type>
                <UINT />
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Size of output data buffer </xhtml>
              </documentation>
            </variable>
          </inputVars>
          <outputVars>
            <variable name="Done">
              <type>
                <BOOL />
              </type>
              <initialValue>
                <simpleValue value="FALSE" />
              </initialValue>
            </variable>
            <variable name="Active">
              <type>
                <BOOL />
              </type>
              <initialValue>
                <simpleValue value="FALSE" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Client should call this function block as long as Active is TRUE </xhtml>
              </documentation>
            </variable>
            <variable name="Error">
              <type>
                <BOOL />
              </type>
              <initialValue>
                <simpleValue value="FALSE" />
              </initialValue>
            </variable>
            <variable name="ErrorID">
              <type>
                <derived name="N4K_STATUS" />
              </type>
              <initialValue>
                <simpleValue value="N4K_STATUS_NONE" />
              </initialValue>
            </variable>
            <variable name="Timedout">
              <type>
                <BOOL />
              </type>
              <initialValue>
                <simpleValue value="FALSE" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Flag indicating the function timed out. Client must call this function block until the function results in Error or Done. </xhtml>
              </documentation>
            </variable>
          </outputVars>
          <localVars>
            <variable name="strFunctionName">
              <type>
                <string length="80" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="pusiAddressIn">
              <type>
                <pointer>
                  <baseType>
                    <USINT />
                  </baseType>
                </pointer>
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Address of input data buffer </xhtml>
              </documentation>
            </variable>
            <variable name="uiSizeIn">
              <type>
                <UINT />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Size of input data buffer </xhtml>
              </documentation>
            </variable>
            <variable name="pusiAddressOut">
              <type>
                <pointer>
                  <baseType>
                    <USINT />
                  </baseType>
                </pointer>
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Address of output data buffer </xhtml>
              </documentation>
            </variable>
            <variable name="uiSizeOut">
              <type>
                <UINT />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Size of output data buffer </xhtml>
              </documentation>
            </variable>
            <variable name="tonTimerReqBuf">
              <type>
                <derived name="TON" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="uiMessageBufferIndex">
              <type>
                <UINT />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> index to the claimed message buffer </xhtml>
              </documentation>
            </variable>
            <variable name="iRequestBufferIndex">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="-1" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> index to the claimed request buffer </xhtml>
              </documentation>
            </variable>
            <variable name="bLocalError">
              <type>
                <BOOL />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="enLocalErrorID">
              <type>
                <derived name="N4K_STATUS" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="bfound">
              <type>
                <BOOL />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="pstrBuffer">
              <type>
                <pointer>
                  <baseType>
                    <string />
                  </baseType>
                </pointer>
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> String pointer to the buffer </xhtml>
              </documentation>
            </variable>
            <variable name="pusiBuffer">
              <type>
                <pointer>
                  <baseType>
                    <USINT />
                  </baseType>
                </pointer>
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> data pointer to buffer </xhtml>
              </documentation>
            </variable>
            <variable name="uiLength">
              <type>
                <UINT />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="udiResult">
              <type>
                <derived name="CXA_SocketComm.SysTypes.RTS_IEC_RESULT" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
          </localVars>
          <addData>
            <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
              <Attributes>
                <Attribute Name="hide_all_locals" Value="" />
              </Attributes>
            </data>
          </addData>
        </interface>
        <body>
          <ST>
            <xhtml xmlns="http://www.w3.org/1999/xhtml">(* First check if the communication is initialized/operational *)
IF Execute AND NOT bN4kInitialized_gb THEN
    IF NOT Error THEN
        fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: N4K Communication is not operational (any more)');
        Error := TRUE;
        ErrorID := N4K_STATUS_NOT_INITIALIZED;
        Active := FALSE;
        uiMessageBufferIndex := uiMSG_BUFFER_INDEX_EMPTY;
    END_IF;
    RETURN;
(* If the function block is active; continue processing until the reply is received. *)
ELSIF Execute AND NOT Active AND NOT Done AND NOT Error THEN
    Active := TRUE;
    bLocalError := FALSE;
    enLocalErrorID := N4K_STATUS_NONE;
    uiMessageBufferIndex := uiMSG_BUFFER_INDEX_EMPTY;
    Timedout := FALSE;

    (* Check if the request data fits in the request buffer *)
    IF (LEN(FunctionName) + 1 + UINT_TO_INT(SizeIn)) &gt; UINT_TO_INT(SIZEOF(arN4kRequestBuffers_gb[0].arData)) THEN
        fbN4kLogMsg_gb.AddMessage(Msg := CONCAT('Too much data; the request does not fit in the send buffer; ', FunctionName));
        bLocalError := TRUE;
        enLocalErrorID := N4K_STATUS_INVALID_INPUT_DATA;
        RETURN;
    END_IF

    (* Check if the request data fits in the response buffer *)
    IF SizeOut &gt; uiMAX_SOCKET_REPLY_DATA_SIZE THEN
        fbN4kLogMsg_gb.AddMessage(Msg := CONCAT('Too much data; the response will not fit in the response buffer; ', FunctionName));
        bLocalError := TRUE;
        enLocalErrorID := N4K_STATUS_INVALID_INPUT_DATA;
        RETURN;
    END_IF

    (* Claim a free message ID *)
    bfound := FALSE;
    uiMessageBufferIndex := 0;
    WHILE (uiMessageBufferIndex &lt;= 250) AND NOT bfound DO
        udiResult := SysSemTry(arN4kMessages_gb[uiMessageBufferIndex].pbySem);
        IF (NOT arN4kMessages_gb[uiMessageBufferIndex].bWaitForReply) AND (udiResult = CXA_SocketComm.CmpErrors.Errors.ERR_OK)  THEN  (*Don't stress the semaphore access*)
            arN4kMessages_gb[uiMessageBufferIndex].bWaitForReply := TRUE;
            SysSemLeave(arN4kMessages_gb[uiMessageBufferIndex].pbySem);
            bfound := TRUE;
        ELSE
            uiMessageBufferIndex := uiMessageBufferIndex + 1;
        END_IF
    END_WHILE

    IF NOT bfound THEN
        fbN4kLogMsg_gb.AddMessage(Msg := CONCAT('ERROR: No more free message IDs; ', FunctionName));
        bLocalError := TRUE;
        enLocalErrorID := N4K_STATUS_OUT_OF_RESOURCES;
        uiMessageBufferIndex := uiMSG_BUFFER_INDEX_EMPTY;
        RETURN;
    END_IF

    (* Store the input data in local variables *)
    strFunctionName := FunctionName;
    pusiAddressIn := InputBuffer;
    uiSizeIn := SizeIn;
    pusiAddressOut := OutputBuffer;
    uiSizeOut := SizeOut;
    tonTimerReqBuf(IN := FALSE, PT := UINT_TO_TIME(Timeout));
    iRequestBufferIndex := -1;
ELSIF Active THEN
    IF bLocalError THEN
        Error := TRUE;
        ErrorID := enLocalErrorID;
        Active := FALSE;
        RETURN;
    END_IF

    tonTimerReqBuf(IN := TRUE);
    IF tonTimerReqBuf.Q THEN
        Timedout := TRUE;
    END_IF

    IF iRequestBufferIndex &lt; 0 THEN     (* No request send yet *)
        IF Timedout THEN
            fbN4kLogMsg_gb.AddMessage(Msg := CONCAT('ERROR: Failed to send the request within the specified timeout; ', FunctionName));
            Error := TRUE;
            ErrorID := N4K_STATUS_TIMEOUT;
            Active := FALSE;
            RETURN;
        END_IF

        (* Claim a request buffer *)
        udiResult := SysSemTry(pbyN4kRequestBufferIndexSem_gb);
        IF udiResult = CXA_SocketComm.CmpErrors.Errors.ERR_OK THEN
            IF arN4kRequestBuffers_gb[usiN4kNextRequestBuffer_gb].stHeader.uiLength = 0 THEN
                iRequestBufferIndex := usiN4kNextRequestBuffer_gb;
                usiN4kNextRequestBuffer_gb := UINT_TO_USINT((usiN4kNextRequestBuffer_gb + 1) MOD uiN4kNumBuffers_gb);
                SysSemLeave(pbyN4kRequestBufferIndexSem_gb);
            ELSE
                SysSemLeave(pbyN4kRequestBufferIndexSem_gb);
                fbN4kLogMsg_gb.AddMessage(Msg := 'No more free request buffers');
                Error := TRUE;
                ErrorID := N4K_STATUS_OUT_OF_RESOURCES;
                Active := FALSE;
                RETURN;
            END_IF
        ELSE
            (* Failed to claim a request buffer; try to claim the buffer the next PLC-cycle *)
            fbN4kLogMsg_gb.AddMessage(Msg := 'DBG: SEMAPHORE IN ACTION... Failed to claim pbyN4kRequestBufferIndexSem_gb');
            RETURN;
        END_IF

        (* copy the request to the request buffer *)
        arN4kRequestBuffers_gb[iRequestBufferIndex].stHeader.uiMsgId := uiMessageBufferIndex;
        arN4kRequestBuffers_gb[iRequestBufferIndex].stHeader.uiSizeIn := uiSizeIn;
        arN4kRequestBuffers_gb[iRequestBufferIndex].stHeader.uiSizeOut := uiSizeOut;
        (* Copy function name *)
        pstrBuffer := ADR(arN4kRequestBuffers_gb[iRequestBufferIndex].arData);
        pstrBuffer^ := strFunctionName;      (* Copy function name (incl. zero terminator) *)
        (* Copy input data to the sendBuffer directly after the function name *)
        uiLength := INT_TO_UINT(LEN(strFunctionName)) + 1;
        pusiBuffer := pstrBuffer + UINT_TO_DWORD(uiLength);
        uiLength := uiLength + uiSizeIn + SIZEOF(arN4kRequestBuffers_gb[iRequestBufferIndex].stHeader);
        WHILE uiSizeIn &gt; 0 DO
            pusiBuffer^ := pusiAddressIn^;
            pusiBuffer := pusiBuffer + 1;
            pusiAddressIn := pusiAddressIn + 1;
            uiSizeIn := uiSizeIn - 1;
        END_WHILE
        (* Setting the uiLength field transfers ownership of the request buffer to the N4K_Communication program *)
        arN4kRequestBuffers_gb[iRequestBufferIndex].stHeader.uiLength := uiLength;

    ELSE    (* Request is sent, wait for a reply *)
        IF NOT arN4kMessages_gb[uiMessageBufferIndex].bWaitForReply THEN
            (* N4K PLC software stack failure (serious bug) *)
            fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: A reply was requested for an inactive MsgId');
            Error := TRUE;
            ErrorID := N4K_STATUS_ERROR;
            Active := FALSE;
            RETURN;
        END_IF

        IF arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer &lt;&gt; 0 THEN
            (* reply received *)
            IF arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer^.stHeader.uiMsgId &lt;&gt; uiMessageBufferIndex OR
               arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer^.stHeader.usiStatus &lt;= N4K_STATUS_PROCESSING THEN
                (* N4K PLC software stack failure (serious bug) *)
                fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: N4K PLC Software stack failure');
                Error := TRUE;
                ErrorID := N4K_STATUS_ERROR;
                Active := FALSE;
                RETURN;
            END_IF

            IF arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer^.stHeader.usiStatus = N4K_STATUS_READY THEN
                (* Function executed successfully *)
                uiLength := arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer^.stHeader.uiLength -
                            SIZEOF(arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer^.stHeader); (* strip header from received data buffer *)
                IF uiLength = uiSizeOut THEN
                    (* Copy the data from the receive buffer to OutputBuffer *)
                    pusiBuffer := ADR(arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer^.arData);
                    WHILE (uiLength &gt; 0) DO
                        pusiAddressOut^ := pusiBuffer^;
                        uiLength := uiLength - 1;
                        pusiBuffer := pusiBuffer + 1;
                        pusiAddressOut := pusiAddressOut + 1;
                    END_WHILE
                    Done := TRUE;
                ELSE
                    fbN4kLogMsg_gb.AddMessage(Msg := CONCAT('ERROR: received wrong nr of bytes; ', FunctionName));
                    Error := TRUE;
                    ErrorID := N4K_STATUS_INVALID_REPLY;
                END_IF
            ELSE
                (* The Cm socket server failed to properly execute the function *)
                fbN4kLogMsg_gb.AddMessage(Msg := CONCAT('ERROR: The Cm socket server failed to execute the function; ', FunctionName));
                Error := TRUE;
                ErrorID := arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer^.stHeader.usiStatus;
            END_IF

            Active := FALSE;
            (* Release the receive buffer *)
            arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer^.stHeader.uiLength := 0;   (* Mark the receive buffer as free buffer *)
            (* Release the Message ID *)
            arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer := 0;
            arN4kMessages_gb[uiMessageBufferIndex].bWaitForReply := FALSE;                  (* Mark the msgId as free *)
            uiMessageBufferIndex := uiMSG_BUFFER_INDEX_EMPTY;
        END_IF
    END_IF
(* Stop executing command. *)
ELSIF NOT Execute AND (Done OR Error) THEN
    IF uiMessageBufferIndex &lt;&gt; uiMSG_BUFFER_INDEX_EMPTY THEN
        IF arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer &lt;&gt; 0 THEN
            arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer^.stHeader.uiLength := 0;   (* Mark the receive buffer as free buffer *)
            arN4kMessages_gb[uiMessageBufferIndex].pstRecvBuffer := 0;
        END_IF
        arN4kMessages_gb[uiMessageBufferIndex].bWaitForReply := FALSE;                      (* Mark the msgId as free *)
        uiMessageBufferIndex := uiMSG_BUFFER_INDEX_EMPTY;
    END_IF
    Done := FALSE;
    Error := FALSE;
END_IF</xhtml>
          </ST>
        </body>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>6158f4d9-4f32-4e01-b4e7-0b490de2015d</ObjectId>
          </data>
        </addData>
      </pou>
      <pou name="N4K_Communication" pouType="functionBlock">
        <interface>
          <inputVars>
            <variable name="Enable">
              <type>
                <BOOL />
              </type>
              <initialValue>
                <simpleValue value="FALSE" />
              </initialValue>
            </variable>
            <variable name="IpAddress">
              <type>
                <string />
              </type>
              <initialValue>
                <simpleValue value="'192.168.0.1'" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> IP Address of the Cm socket server </xhtml>
              </documentation>
            </variable>
            <variable name="Port">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="4242" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Cm socket server port </xhtml>
              </documentation>
            </variable>
            <variable name="RetryMax">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="3" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Max nr. of retries (30sec) to connect with the Cm socket server </xhtml>
              </documentation>
            </variable>
          </inputVars>
          <outputVars>
            <variable name="InOperation">
              <type>
                <BOOL />
              </type>
            </variable>
            <variable name="Error">
              <type>
                <BOOL />
              </type>
            </variable>
          </outputVars>
          <localVars>
            <variable name="stAddress">
              <type>
                <derived name="CXA_SocketComm.SysSocket.SysSocket_Interfaces.SOCKADDRESS" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="pbySocket">
              <type>
                <derived name="CXA_SocketComm.SysTypes.RTS_IEC_HANDLE" />
              </type>
              <initialValue>
                <simpleValue value="CXA_SocketComm.SysTypes.RTS_INVALID_HANDLE" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="arReplyBuffers">
              <type>
                <array>
                  <dimension lower="0" upper="(usiMAX_NR_OF_PARALLEL_REQUESTS - 1)" />
                  <baseType>
                    <derived name="N4K_REPLY" />
                  </baseType>
                </array>
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="uiRecvBufIndex">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Index to the current receive buffer</xhtml>
              </documentation>
            </variable>
            <variable name="uiInputSize">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Number of bytes received in the current receive buffer</xhtml>
              </documentation>
            </variable>
            <variable name="uiSendIndex">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="uiPendingMessages">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Number of requests waiting for a reply</xhtml>
              </documentation>
            </variable>
            <variable name="udiResult">
              <type>
                <derived name="CXA_SocketComm.SysTypes.RTS_IEC_RESULT" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="stFdRead">
              <type>
                <derived name="CXA_SocketComm.SysSocket.SysSocket_Interfaces.SOCKET_FD_SET" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Variables used by SysSockSelect </xhtml>
              </documentation>
            </variable>
            <variable name="stTvTimeout">
              <type>
                <derived name="CXA_SocketComm.SysSocket.SysSocket_Interfaces.SOCKET_TIMEVAL" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="diReady">
              <type>
                <DINT />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="uiIndex">
              <type>
                <UINT />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Tmp variables</xhtml>
              </documentation>
            </variable>
            <variable name="diSize">
              <type>
                <DINT />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="usiLoopCount">
              <type>
                <USINT />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Max nr of recv / send actions per PLC cycle </xhtml>
              </documentation>
            </variable>
            <variable name="uiExpectedInputLength">
              <type>
                <UINT />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="diSockOptionNoDelay">
              <type>
                <DINT />
              </type>
              <initialValue>
                <simpleValue value="-1" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Disable nagle algorithm, so use TCP_NODELAY</xhtml>
              </documentation>
            </variable>
            <variable name="fbIL_TCPConnectionAsync">
              <type>
                <derived name="IL_TCPConnectionAsync" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> FB which creates and closes either a server or client </xhtml>
              </documentation>
            </variable>
            <variable name="bCommAsyncOK">
              <type>
                <BOOL />
              </type>
              <initialValue>
                <simpleValue value="FALSE" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Flag for PLC and Cm socket server communication is alive </xhtml>
              </documentation>
            </variable>
            <variable name="iStep">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="STEP_INITIAL" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> CASE step </xhtml>
              </documentation>
            </variable>
            <variable name="iRetry">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> nr. of retries </xhtml>
              </documentation>
            </variable>
            <variable name="tRetryDelay">
              <type>
                <TIME />
              </type>
              <initialValue>
                <simpleValue value="TIME#30s0ms" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Delay time before a retry will be executed </xhtml>
              </documentation>
            </variable>
            <variable name="tonRetry">
              <type>
                <derived name="TON" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Time On Delay for retry a connection </xhtml>
              </documentation>
            </variable>
          </localVars>
          <localVars constant="true">
            <variable name="STEP_INITIAL">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Reset FB's </xhtml>
              </documentation>
            </variable>
            <variable name="STEP_ASYNC_CONNECT">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="10" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Open an asynchronous connection to the Cm socket server </xhtml>
              </documentation>
            </variable>
            <variable name="STEP_ASYNC_DISCONNECT">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="20" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Close the asychronous connection to the Cm socket server </xhtml>
              </documentation>
            </variable>
            <variable name="STEP_RETRY">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="30" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Count the amount of retries </xhtml>
              </documentation>
            </variable>
            <variable name="STEP_END">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="40" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Asychronous connection to the Cm socket server was succesfull </xhtml>
              </documentation>
            </variable>
            <variable name="STEP_ERROR">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="-1" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> An error occurred </xhtml>
              </documentation>
            </variable>
          </localVars>
          <addData>
            <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
              <Attributes>
                <Attribute Name="hide_all_locals" Value="" />
              </Attributes>
            </data>
          </addData>
        </interface>
        <actions>
          <action name="CheckCommunication">
            <body>
              <ST>
                <xhtml xmlns="http://www.w3.org/1999/xhtml">(*
Checks the communication with the Cm socket server, using async communication.

    States: | PLC | PC/Node | Cm socket server | Result                                   |
 -----------------------------------------------------------------------------------------
            | ON  | OFF     | OFF              | AsyncConnect ERROR --&gt; Retries --&gt; Error |
            | ON  | ON      | OFF              | AsyncConnect ERROR --&gt; Retries --&gt; Error |
            | ON  | ON      | ON               | AsyncConnect OK                          |

    Notes: fbN4kLogMsg_gb.AddMessage only works when Cm socket server has started!!!
*)
CASE iStep OF
    STEP_INITIAL:
        fbIL_TCPConnectionAsync(Enable := FALSE);
        tonRetry(IN := FALSE);
        iStep := STEP_ASYNC_CONNECT;
        
    STEP_ASYNC_CONNECT:
        fbIL_TCPConnectionAsync(Enable := TRUE, Server := FALSE, Address := IpAddress, Port := TO_UINT(Port));
        tonRetry(IN := TRUE, PT := tRetryDelay);

        IF fbIL_TCPConnectionAsync.Done THEN
            fbN4kLogMsg_gb.AddMessage(Msg := 'PLC --&gt; Cm socket server asynchronous TCP connection established');
            tonRetry(IN := FALSE);
            iStep := STEP_ASYNC_DISCONNECT;

        ELSIF tonRetry.Q THEN
            istep := STEP_RETRY;

        ELSIF fbIL_TCPConnectionAsync.Error THEN
            istep := STEP_ERROR;
        END_IF

    STEP_ASYNC_DISCONNECT:
        fbIL_TCPConnectionAsync(Enable := FALSE);
        fbN4kLogMsg_gb.AddMessage(Msg := 'PLC --&gt; Cm socket server asynchronous TCP connection closed');
        iStep := STEP_END;

    STEP_RETRY:
        iRetry := iRetry + 1;

        IF iRetry &lt; RetryMax THEN
            istep := STEP_INITIAL;
        ELSE
            istep := STEP_ERROR;
        END_IF

    STEP_END:
        bCommAsyncOK := TRUE;

    STEP_ERROR:
        Error := TRUE;
END_CASE</xhtml>
              </ST>
            </body>
            <addData>
              <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
                <ObjectId>769c53a9-a8c9-4568-907b-854966aeb199</ObjectId>
              </data>
            </addData>
          </action>
        </actions>
        <body>
          <ST>
            <xhtml xmlns="http://www.w3.org/1999/xhtml">(*At start-up or after an error: First check PC communication with Asynchronous Function*)
IF Enable AND NOT bCommAsyncOK AND NOT Error THEN
    CheckCommunication();

ELSIF Enable AND bCommAsyncOK AND NOT Error THEN
    IF NOT InOperation THEN (*Initialize the connection to Cm socket server*)
        IF NOT bN4kInitialized_gb THEN  (*Initialize global N4k buffers just once*)
            (*Initialize buffer sizes*)
            uiN4kBufferSize_gb := SIZEOF(arN4kRequestBuffers_gb[0]);
            uiN4kNumBuffers_gb := DINT_TO_UINT(SIZEOF(arN4kRequestBuffers_gb) / uiN4kBufferSize_gb);

            (*Initialize buffers*)
            FOR uiIndex := 0 TO uiN4kNumbuffers_gb - 1 DO
                arN4kRequestBuffers_gb[uiIndex].stHeader.uiLength := 0;
                arReplyBuffers[uiIndex].stHeader.uiLength := 0;
            END_FOR
            pbyN4kRequestBufferIndexSem_gb := SysSemCreate(ADR(udiResult));
            IF udiResult &lt;&gt; CXA_SocketComm.CmpErrors.Errors.ERR_OK THEN
                fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: SysSemCreate failed');
                Error := TRUE;
                RETURN;
            END_IF

            (*Initialize msgIds*)
            FOR uiIndex := 0 TO 255 DO
                arN4kMessages_gb[uiIndex].pbySem := SysSemCreate(ADR(udiResult));
                IF udiResult &lt;&gt; CXA_SocketComm.CmpErrors.Errors.ERR_OK THEN
                    fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: SysSemCreate failed');
                    Error := TRUE;
                    RETURN;
                END_IF
                arN4kMessages_gb[uiIndex].bWaitForReply := FALSE;
                arN4kMessages_gb[uiIndex].pstRecvBuffer := 0;
            END_FOR

            bN4kInitialized_gb := TRUE;
        END_IF

        (*Create Cm socket server socket address*)
        stAddress.sin_family := CXA_SocketComm.SysSocket.SysSocket_Interfaces.SOCKET_AF_INET;
        udiResult := CXA_SocketComm.SysSocket.SysSocket_Implementation.SysSockInetAddr(IpAddress, ADR(stAddress.sin_addr.ulAddr));
        IF udiResult = CXA_SocketComm.CmpErrors.Errors.ERR_OK THEN
            IF Port &gt;= 1024 THEN
                stAddress.sin_port := CXA_SocketComm.SysSocket.SysSocket_Implementation.SysSockHtons(DINT_TO_WORD(Port));
                (*Create a socket*)
                pbySocket := CXA_SocketComm.SysSocket.SysSocket_Implementation.SysSockCreate(CXA_SocketComm.SysSocket.SysSocket_Interfaces.SOCKET_AF_INET,
                                                                                             CXA_SocketComm.SysSocket.SysSocket_Interfaces.SOCKET_STREAM,
                                                                                             0,
                                                                                             ADR(udiResult));
                IF udiResult = CXA_SocketComm.CmpErrors.Errors.ERR_OK THEN
                    (*Set TCP_NODELAY option*)
                    udiResult := CXA_SocketComm.SysSocket.SysSocket_Implementation.SysSockSetOption(pbySocket,
                                                                                                    CXA_SocketComm.SysSocket.SysSocket_Interfaces.SOCKET_IPPROTO_TCP,
                                                                                                    CXA_SocketComm.SysSocket.SysSocket_Interfaces.SOCKET_TCP_NODELAY,
                                                                                                    ADR(diSockOptionNoDelay),
                                                                                                    SIZEOF(diSockOptionNoDelay));
                    IF udiResult = CXA_SocketComm.CmpErrors.Errors.ERR_OK THEN
                        (*Connect to the Cm socket server*)
                        udiResult := CXA_SocketComm.SysSocket.SysSocket_Implementation.SysSockConnect(pbySocket,
                                                                                                      ADR(stAddress),
                                                                                                      SIZEOF(stAddress));
                        IF udiResult = CXA_SocketComm.CmpErrors.Errors.ERR_OK THEN
                            InOperation := TRUE;
                            fbN4kLogMsg_gb.AddMessage(Msg := 'PLC --&gt; Cm socket server synchronous TCP connection established.');
                            (* initialize variables for SysSockSelect *)
                            stTvTimeout.tv_sec := 0;
                            stTvTimeout.tv_usec := 0;
                            (* initialize variables for receive buffer management *)
                            uiExpectedInputLength := 0;
                            uiInputSize := uiExpectedInputLength;
                            uiRecvBufIndex := 0;
                        ELSE
                            fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: Failed to connect to Cm socket server');
                            Error := TRUE;
                        END_IF
                    ELSE
                        fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: Failed to set socket option');
                        Error := TRUE;
                    END_IF
                ELSE
                    fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: Failed to create the TCP socket');
                    Error := TRUE;
                END_IF
            ELSE
                fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: Invalid port address; port number should be &gt;= 1024');
                Error := TRUE;
            END_IF
        ELSE
            fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: Invalid IP address');
            Error := TRUE;
        END_IF
    ELSE    (* Pump the message queues *)
        (* Receive pending data *)
        usiLoopCount := 2*usiMAX_NR_MSGS_PER_CYCLE;  (* Try to recv usiMAX_NR_MSGS_PER_CYCLE messages (messages are received in 2 phases; header and data) *)
        WHILE usiLoopCount &gt; 0 DO
            (* Check if there is data ready to receive *)
            stFdRead.fd_count := 1;
            stFdRead.fd_array[0] := pbySocket;
            udiResult := CXA_SocketComm.SysSocket.SysSocket_Implementation.SysSockSelect(diWidth := CXA_SocketComm.SysSocket.SysSocket_Interfaces.SOCKET_FD_SETSIZE,
                                                                                         pfdRead := ADR(stFdRead),
                                                                                         pfdWrite := 0,
                                                                                         pfdExcept := 0,
                                                                                         ptvTimeout := ADR(stTvTimeout),
                                                                                         pdiReady := ADR(diReady));
            IF udiResult = CXA_SocketComm.CmpErrors.Errors.ERR_OK THEN
                IF diReady &gt; 0 THEN
                    IF uiExpectedInputLength = uiInputSize THEN
                        (* Previous message was completely received; claim a new receive buffer. *)
                        uiIndex := uiRecvBufIndex;
                        REPEAT
                            (* Find an empty receive buffer. *)
                            uiRecvBufIndex := (uiRecvBufIndex + 1) MOD uiN4kNumBuffers_gb;
                        UNTIL
                            (arReplyBuffers[uiRecvBufIndex].stHeader.uiLength = 0) OR
                            (uiIndex = uiRecvBufIndex)
                        END_REPEAT;

                        IF uiIndex = uiRecvBufIndex THEN
                            (* There are more than uiN4kNumBuffers_gb messages pending; do not send new requests. *)
                            fbN4kLogMsg_gb.AddMessage(Msg := 'WARNING: No more free receive buffers');
                            RETURN;
                        END_IF
                        uiExpectedInputLength := SIZEOF(arReplyBuffers[uiRecvBufIndex].stHeader);
                        uiInputSize := 0;
                    END_IF

                    (* Receive the data *)
                    diSize := TO_DINT(CXA_SocketComm.SysSocket.SysSocket_Implementation.SysSockRecv(hSocket := pbySocket,
                                      pbyBuffer := ADR(arReplyBuffers[uiRecvBufIndex]) + uiInputSize,
                                      diBufferSize := uiExpectedInputLength - uiInputSize,
                                      diFlags := 0,
                                      ADR(udiResult)));

                    IF udiResult &lt;&gt; CXA_SocketComm.CmpErrors.Errors.ERR_OK OR diSize = 0 THEN
                        fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: The Cm socket server closed the connection');
                        Error := TRUE;
                        RETURN;
                    ELSE
                        uiInputSize := uiInputSize + TO_UINT(diSize);
                        IF uiInputSize &gt;= SIZEOF(arReplyBuffers[uiRecvBufIndex].stHeader.uiLength) THEN
                            uiExpectedInputLength := arReplyBuffers[uiRecvBufIndex].stHeader.uiLength;
                            IF uiExpectedInputLength &gt; SIZEOF(arReplyBuffers[uiRecvBufIndex]) THEN
                                fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: The received reply does not fit in the reply buffer');
                                Error := TRUE;
                                RETURN;
                            ELSIF uiExpectedInputLength &lt; SIZEOF(arReplyBuffers[uiRecvBufIndex].stHeader) THEN
                                fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: The reply contains less data than the reply header.');
                                Error := TRUE;
                                RETURN;
                            END_IF

                            IF uiInputSize = uiExpectedInputLength THEN
                                (*Message is completely received*)
                                IF arReplyBuffers[uiRecvBufIndex].stHeader.uiMsgId = usiALIVE_MSG THEN
                                    (*Special handling for this ALIVE message*)
                                    (*This is a dummy message that is used as a "pacemaker" for the socket communication*)
                                    arReplyBuffers[uiRecvBufIndex].stHeader.uiLength := 0;
                                ELSIF arN4kMessages_gb[arReplyBuffers[uiRecvBufIndex].stHeader.uiMsgId].bWaitForReply THEN
                                    arN4kMessages_gb[arReplyBuffers[uiRecvBufIndex].stHeader.uiMsgId].pstRecvBuffer := ADR(arReplyBuffers[uiRecvBufIndex]);
                                    uiPendingMessages := uiPendingMessages - 1;
                                ELSE
                                    fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: Received a message that was not expected');
                                    Error := TRUE;
                                    RETURN;
                                END_IF
                            END_IF
                        END_IF
                    END_IF
                    usiLoopCount := usiLoopCount - 1;
                ELSE
                    usiLoopCount := 0;  (* There is no data ready to receive; stop the loop *)
                END_IF
            ELSE
                usiLoopCount := 0;  (* There is no data ready to receive; stop the loop *)
            END_IF
        END_WHILE

        (* Send pending requests *)
        usiLoopCount := usiMAX_NR_MSGS_PER_CYCLE;  (* Try to send max usiMAX_NR_MSGS_PER_CYCLE messages *)
        WHILE usiLoopCount &gt; 0 AND arN4kRequestBuffers_gb[uiSendIndex].stHeader.uiLength &gt; 0 DO

            diSize := TO_DINT(CXA_SocketComm.SysSocket.SysSocket_Implementation.SysSockSend(hSocket := pbySocket,
                              pbyBuffer := ADR(arN4kRequestBuffers_gb[uiSendIndex]),
                              diBufferSize := arN4kRequestBuffers_gb[uiSendIndex].stHeader.uiLength,
                              diFlags := 0,
                              ADR(udiResult)));
            IF udiResult = CXA_SocketComm.CmpErrors.Errors.ERR_OK THEN
                IF diSize = arN4kRequestBuffers_gb[uiSendIndex].stHeader.uiLength THEN
                    uiPendingMessages := uiPendingMessages + 1;
                    arN4kRequestBuffers_gb[uiSendIndex].stHeader.uiLength := 0; (* mark send buffer ready for reuse *)
                    uiSendIndex := (uiSendIndex + 1) MOD uiN4kNumBuffers_gb;
                ELSE
                    fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: Could not send all required data');
                    Error := TRUE;
                    RETURN;
                END_IF
            ELSE
                fbN4kLogMsg_gb.AddMessage(Msg := 'ERROR: SysSockSend failed');
                Error := TRUE;
                RETURN;
            END_IF

            usiLoopCount := usiLoopCount - 1;
        END_WHILE
    END_IF

ELSIF NOT Enable THEN
    Error := FALSE;
END_IF

IF Error OR NOT Enable THEN
    bCommAsyncOK := FALSE;
    iStep := STEP_INITIAL;
    iRetry := 0;
    InOperation := FALSE;
    IF bN4kInitialized_gb THEN
        bN4kInitialized_gb := FALSE;
        SysSemDelete(pbyN4kRequestBufferIndexSem_gb);

        FOR uiIndex := 0 TO 255 DO
            SysSemDelete(arN4kMessages_gb[uiIndex].pbySem);
        END_FOR
    END_IF
    IF pbySocket &lt;&gt; CXA_SocketComm.SysTypes.RTS_INVALID_HANDLE THEN
        CXA_SocketComm.SysSocket.SysSocket_Implementation.SysSockShutdown(hSocket := pbySocket, dihow := 2);
        CXA_SocketComm.SysSocket.SysSocket_Implementation.SysSockClose(hSocket := pbySocket);
        pbySocket := CXA_SocketComm.SysTypes.RTS_INVALID_HANDLE;
    END_IF
END_IF</xhtml>
          </ST>
        </body>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>124db718-c04f-46bb-aec2-a5aaffc2281b</ObjectId>
          </data>
        </addData>
      </pou>
      <pou name="N4K_Logger" pouType="functionBlock">
        <interface>
          <inputVars>
            <variable name="IpAddress">
              <type>
                <string />
              </type>
              <initialValue>
                <simpleValue value="'192.168.0.1'" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> IP Address of the Cm socket server </xhtml>
              </documentation>
            </variable>
            <variable name="Port">
              <type>
                <UINT />
              </type>
              <initialValue>
                <simpleValue value="4242" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> Logging port </xhtml>
              </documentation>
            </variable>
            <variable name="Msg">
              <type>
                <string length="usiMAX_LOG_STRING_LEN" />
              </type>
            </variable>
          </inputVars>
          <outputVars>
            <variable name="Status">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="1" />
              </initialValue>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> 0: close UDP socket; 1: create UDP socket; 2: send data packages; 3: log buffer overflow detected; -1: socket creation failed; </xhtml>
              </documentation>
            </variable>
          </outputVars>
          <localVars>
            <variable name="usiIn">
              <type>
                <USINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="usiOut">
              <type>
                <USINT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="arMessages">
              <type>
                <array>
                  <dimension lower="0" upper="19" />
                  <baseType>
                    <derived name="N4K_LOG_BUFFER" />
                  </baseType>
                </array>
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="bServer">
              <type>
                <BOOL />
              </type>
              <initialValue>
                <simpleValue value="FALSE" />
              </initialValue>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="fbSocket">
              <type>
                <derived name="CXA_SocketComm.IL_UDPSocketAsync" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
            <variable name="fbSendTo">
              <type>
                <derived name="CXA_SocketComm.IL_UDPSendtoAsync" />
              </type>
              <addData>
                <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
                  <Attributes>
                    <Attribute Name="hide" Value="" />
                  </Attributes>
                </data>
              </addData>
            </variable>
          </localVars>
          <addData>
            <data name="http://www.3s-software.com/plcopenxml/attributes" handleUnknown="implementation">
              <Attributes>
                <Attribute Name="hide_all_locals" Value="" />
              </Attributes>
            </data>
          </addData>
        </interface>
        <actions>
          <action name="Reset">
            <body>
              <ST>
                <xhtml xmlns="http://www.w3.org/1999/xhtml">(*
This method closes the UDP logging socket and clears the log buffers.
This method is intended to automatically test logging functionality.
Note: This could also be used to dynamically switch to another logging Cm socket server.
*)
Status := 0;
usiIn := 0;
usiOut := 0;</xhtml>
              </ST>
            </body>
            <addData>
              <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
                <ObjectId>f5a988eb-6c14-4dc1-b72e-d03e34233a0d</ObjectId>
              </data>
            </addData>
          </action>
          <action name="AddMessage">
            <body>
              <ST>
                <xhtml xmlns="http://www.w3.org/1999/xhtml">(*
Add a message to the internal log buffer.
When there are no internal log buffers free:
 - The message will be rejected and
 - Status of N4K_Logger will be set to 3.
*)
IF ((usiIn + 1) MOD 20) &lt;&gt; usiOut THEN    (* don't overwrite old log messages *)
    arMessages[usiIn].strBuffer := Msg;
    arMessages[usiIn].iSize := LEN(Msg);
    usiIn := (usiIn + 1) MOD 20;
ELSIF Status = 2 THEN
    (* logbuffer overflow *)
    Status := 3;
END_IF</xhtml>
              </ST>
            </body>
            <addData>
              <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
                <ObjectId>6b21efde-46c6-485d-858a-5fdffdba2cef</ObjectId>
              </data>
            </addData>
          </action>
        </actions>
        <body>
          <ST>
            <xhtml xmlns="http://www.w3.org/1999/xhtml">CASE Status OF
0:
    fbSocket(Enable := FALSE,
             Server := bServer, Address := IpAddress, Port := Port );
    IF NOT fbSocket.InOperation THEN
        Status := 1;
    END_IF

1:
    fbSocket(Enable := TRUE,
             Server := bServer, Address := IpAddress, Port := Port );
    IF fbSocket.InOperation THEN
        Status := 2;
    END_IF
    IF fbSocket.Error THEN
        Status := -1;
    END_IF

2..3:
    fbSendTo(Execute := (usiIn &lt;&gt; usiOut) AND NOT fbSendTo.Done,
             Socket := fbSocket.Socket,
             Address := IpAddress, Port := Port,
             ValueAdr := ADR(arMessages[usiOut].strBuffer), NoOfBytes := INT_TO_UDINT(arMessages[usiOut].iSize) );
    IF fbSendTo.Done THEN
        usiOut := (usiOut + 1) MOD 20;
        Status := 2;   (* Clear the overflow flag *)
    END_IF

END_CASE</xhtml>
          </ST>
        </body>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>ca02a5bf-b1b1-4091-aaeb-23b0744c921b</ObjectId>
          </data>
        </addData>
      </pou>
    </pous>
  </types>
  <instances>
    <configurations />
  </instances>
  <addData>
    <data name="http://www.3s-software.com/plcopenxml/globalvars" handleUnknown="implementation">
      <globalVars name="Global_N4K_Variables" constant="true">
        <variable name="usiMAX_NR_OF_PARALLEL_REQUESTS">
          <type>
            <USINT />
          </type>
          <initialValue>
            <simpleValue value="128" />
          </initialValue>
          <documentation>
            <xhtml xmlns="http://www.w3.org/1999/xhtml"> Nr of requests that can be handled simultaneously </xhtml>
          </documentation>
        </variable>
        <variable name="usiALIVE_MSG">
          <type>
            <USINT />
          </type>
          <initialValue>
            <simpleValue value="255" />
          </initialValue>
        </variable>
        <variable name="usiMAX_NR_MSGS_PER_CYCLE">
          <type>
            <USINT />
          </type>
          <initialValue>
            <simpleValue value="3" />
          </initialValue>
          <documentation>
            <xhtml xmlns="http://www.w3.org/1999/xhtml"> nr of messages to process during 1 communication-cycle </xhtml>
          </documentation>
        </variable>
        <variable name="usiMAX_LOG_STRING_LEN">
          <type>
            <USINT />
          </type>
          <initialValue>
            <simpleValue value="255" />
          </initialValue>
          <documentation>
            <xhtml xmlns="http://www.w3.org/1999/xhtml"> max length of a log string </xhtml>
          </documentation>
        </variable>
        <variable name="uiMSG_BUFFER_INDEX_EMPTY">
          <type>
            <UINT />
          </type>
          <initialValue>
            <simpleValue value="256" />
          </initialValue>
        </variable>
        <variable name="uiMAX_SOCKET_REQUEST_DATA_SIZE">
          <type>
            <UINT />
          </type>
          <initialValue>
            <simpleValue value="2048" />
          </initialValue>
          <documentation>
            <xhtml xmlns="http://www.w3.org/1999/xhtml"> Keep this size the same as the define MAX_SOCKET_REQUEST_DATA_SIZE in the sw. </xhtml>
          </documentation>
        </variable>
        <variable name="uiMAX_SOCKET_REPLY_DATA_SIZE">
          <type>
            <UINT />
          </type>
          <initialValue>
            <simpleValue value="6144" />
          </initialValue>
          <documentation>
            <xhtml xmlns="http://www.w3.org/1999/xhtml"> Keep this size the same as the define MAX_SOCKET_REPLY_DATA_SIZE in the sw. </xhtml>
          </documentation>
        </variable>
        <variable name="bN4kInitialized_gb">
          <type>
            <BOOL />
          </type>
          <initialValue>
            <simpleValue value="FALSE" />
          </initialValue>
        </variable>
        <variable name="arN4kMessages_gb">
          <type>
            <array>
              <dimension lower="0" upper="255" />
              <baseType>
                <derived name="N4K_MESSAGE" />
              </baseType>
            </array>
          </type>
        </variable>
        <variable name="arN4kRequestBuffers_gb">
          <type>
            <array>
              <dimension lower="0" upper="(usiMAX_NR_OF_PARALLEL_REQUESTS - 1)" />
              <baseType>
                <derived name="N4K_REQUEST" />
              </baseType>
            </array>
          </type>
        </variable>
        <variable name="pbyN4kRequestBufferIndexSem_gb">
          <type>
            <derived name="CXA_SocketComm.SysTypes.RTS_IEC_HANDLE" />
          </type>
          <initialValue>
            <simpleValue value="CXA_SocketComm.SysTypes.RTS_INVALID_HANDLE" />
          </initialValue>
        </variable>
        <variable name="usiN4kNextRequestBuffer_gb">
          <type>
            <USINT />
          </type>
          <initialValue>
            <simpleValue value="0" />
          </initialValue>
          <documentation>
            <xhtml xmlns="http://www.w3.org/1999/xhtml">Index to the next free send buffer</xhtml>
          </documentation>
        </variable>
        <variable name="uiN4kBufferSize_gb">
          <type>
            <UINT />
          </type>
          <documentation>
            <xhtml xmlns="http://www.w3.org/1999/xhtml"> := SIZEOF(arN4kRequestBuffers_gb[0]);</xhtml>
          </documentation>
        </variable>
        <variable name="uiN4kNumBuffers_gb">
          <type>
            <UINT />
          </type>
          <documentation>
            <xhtml xmlns="http://www.w3.org/1999/xhtml"> := SIZEOF(arN4kRequestBuffers_gb) / uiN4kBufferSize_gb;</xhtml>
          </documentation>
        </variable>
        <variable name="fbN4kLogMsg_gb">
          <type>
            <derived name="N4K_Logger" />
          </type>
        </variable>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>8f41a7cf-0864-49e5-8520-e084e32596a3</ObjectId>
          </data>
          <data name="http://www.3s-software.com/plcopenxml/mixedattrsvarlist" handleUnknown="implementation">
            <MixedAttrsVarList>
              <globalVars name="Global_N4K_Variables" constant="true">
                <variable name="usiMAX_NR_OF_PARALLEL_REQUESTS">
                  <type>
                    <USINT />
                  </type>
                  <initialValue>
                    <simpleValue value="128" />
                  </initialValue>
                  <documentation>
                    <xhtml xmlns="http://www.w3.org/1999/xhtml"> Nr of requests that can be handled simultaneously </xhtml>
                  </documentation>
                </variable>
                <variable name="usiALIVE_MSG">
                  <type>
                    <USINT />
                  </type>
                  <initialValue>
                    <simpleValue value="255" />
                  </initialValue>
                </variable>
                <variable name="usiMAX_NR_MSGS_PER_CYCLE">
                  <type>
                    <USINT />
                  </type>
                  <initialValue>
                    <simpleValue value="3" />
                  </initialValue>
                  <documentation>
                    <xhtml xmlns="http://www.w3.org/1999/xhtml"> nr of messages to process during 1 communication-cycle </xhtml>
                  </documentation>
                </variable>
                <variable name="usiMAX_LOG_STRING_LEN">
                  <type>
                    <USINT />
                  </type>
                  <initialValue>
                    <simpleValue value="255" />
                  </initialValue>
                  <documentation>
                    <xhtml xmlns="http://www.w3.org/1999/xhtml"> max length of a log string </xhtml>
                  </documentation>
                </variable>
                <variable name="uiMSG_BUFFER_INDEX_EMPTY">
                  <type>
                    <UINT />
                  </type>
                  <initialValue>
                    <simpleValue value="256" />
                  </initialValue>
                </variable>
                <variable name="uiMAX_SOCKET_REQUEST_DATA_SIZE">
                  <type>
                    <UINT />
                  </type>
                  <initialValue>
                    <simpleValue value="2048" />
                  </initialValue>
                  <documentation>
                    <xhtml xmlns="http://www.w3.org/1999/xhtml"> Keep this size the same as the define MAX_SOCKET_REQUEST_DATA_SIZE in the sw. </xhtml>
                  </documentation>
                </variable>
                <variable name="uiMAX_SOCKET_REPLY_DATA_SIZE">
                  <type>
                    <UINT />
                  </type>
                  <initialValue>
                    <simpleValue value="6144" />
                  </initialValue>
                  <documentation>
                    <xhtml xmlns="http://www.w3.org/1999/xhtml"> Keep this size the same as the define MAX_SOCKET_REPLY_DATA_SIZE in the sw. </xhtml>
                  </documentation>
                </variable>
              </globalVars>
              <globalVars name="Global_N4K_Variables">
                <variable name="bN4kInitialized_gb">
                  <type>
                    <BOOL />
                  </type>
                  <initialValue>
                    <simpleValue value="FALSE" />
                  </initialValue>
                </variable>
                <variable name="arN4kMessages_gb">
                  <type>
                    <array>
                      <dimension lower="0" upper="255" />
                      <baseType>
                        <derived name="N4K_MESSAGE" />
                      </baseType>
                    </array>
                  </type>
                </variable>
                <variable name="arN4kRequestBuffers_gb">
                  <type>
                    <array>
                      <dimension lower="0" upper="(usiMAX_NR_OF_PARALLEL_REQUESTS - 1)" />
                      <baseType>
                        <derived name="N4K_REQUEST" />
                      </baseType>
                    </array>
                  </type>
                </variable>
                <variable name="pbyN4kRequestBufferIndexSem_gb">
                  <type>
                    <derived name="CXA_SocketComm.SysTypes.RTS_IEC_HANDLE" />
                  </type>
                  <initialValue>
                    <simpleValue value="CXA_SocketComm.SysTypes.RTS_INVALID_HANDLE" />
                  </initialValue>
                </variable>
                <variable name="usiN4kNextRequestBuffer_gb">
                  <type>
                    <USINT />
                  </type>
                  <initialValue>
                    <simpleValue value="0" />
                  </initialValue>
                  <documentation>
                    <xhtml xmlns="http://www.w3.org/1999/xhtml">Index to the next free send buffer</xhtml>
                  </documentation>
                </variable>
                <variable name="uiN4kBufferSize_gb">
                  <type>
                    <UINT />
                  </type>
                  <documentation>
                    <xhtml xmlns="http://www.w3.org/1999/xhtml"> := SIZEOF(arN4kRequestBuffers_gb[0]);</xhtml>
                  </documentation>
                </variable>
                <variable name="uiN4kNumBuffers_gb">
                  <type>
                    <UINT />
                  </type>
                  <documentation>
                    <xhtml xmlns="http://www.w3.org/1999/xhtml"> := SIZEOF(arN4kRequestBuffers_gb) / uiN4kBufferSize_gb;</xhtml>
                  </documentation>
                </variable>
                <variable name="fbN4kLogMsg_gb">
                  <type>
                    <derived name="N4K_Logger" />
                  </type>
                </variable>
              </globalVars>
            </MixedAttrsVarList>
          </data>
        </addData>
      </globalVars>
    </data>
    <data name="http://www.3s-software.com/plcopenxml/projectstructure" handleUnknown="discard">
      <ProjectStructure>
        <Folder Name="Data types">
          <Folder Name="N4K_Support">
            <Object Name="N4K_STATUS" ObjectId="48f61eb1-4d20-4090-bad9-d607efcc5aac" />
            <Object Name="N4K_REQUEST_HEADER" ObjectId="df547b05-5210-46d9-9a11-ea6293426b2f" />
            <Object Name="N4K_REQUEST" ObjectId="4da207f7-eb0a-4183-bbad-ba0c918d7cb8" />
            <Object Name="N4K_REPLY_HEADER" ObjectId="bd93192c-d0d9-46bd-bc2e-98490d717252" />
            <Object Name="N4K_REPLY" ObjectId="8350c0ad-55b7-45bf-a02d-fad6d2ebc37e" />
            <Object Name="N4K_MESSAGE" ObjectId="e7f93e9d-b72b-413c-aef0-be3d910df45b" />
            <Object Name="N4K_LOG_BUFFER" ObjectId="54c410e8-1394-47d6-9b2d-09c1d6934ac4" />
          </Folder>
        </Folder>
        <Folder Name="Global Variables">
          <Object Name="Global_N4K_Variables" ObjectId="8f41a7cf-0864-49e5-8520-e084e32596a3" />
        </Folder>
        <Folder Name="POUs">
          <Folder Name="N4K">
            <Object Name="N4K_Logger" ObjectId="ca02a5bf-b1b1-4091-aaeb-23b0744c921b">
              <Object Name="Reset" ObjectId="f5a988eb-6c14-4dc1-b72e-d03e34233a0d" />
              <Object Name="AddMessage" ObjectId="6b21efde-46c6-485d-858a-5fdffdba2cef" />
            </Object>
            <Object Name="N4K_Communication" ObjectId="124db718-c04f-46bb-aec2-a5aaffc2281b">
              <Object Name="CheckCommunication" ObjectId="769c53a9-a8c9-4568-907b-854966aeb199" />
            </Object>
            <Object Name="N4K_Call" ObjectId="6158f4d9-4f32-4e01-b4e7-0b490de2015d" />
          </Folder>
        </Folder>
      </ProjectStructure>
    </data>
  </addData>
</project>