ORGANIZATION_BLOCK "LCom_Example"
TITLE = "Main Program Sweep (Cycle)"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1

BEGIN
	(* REGION CONFIGURATION
	    // Set configuration values in first call of FB
	    IF "LCom_ExampleComData".firstCycle THEN
	        //-----------------------------------------------------------
	        // Connection parameter
	        //-----------------------------------------------------------
	        // hardware identifier of the local interface, PLC tags--Default tag table--System constant default value: PROFINET_interface_1 = 64
	        "LCom_Communication_DB".configuration.connection.interfaceID := 64;
	        //unique connection identifier (W#16#0001...W#16#0FFF)
	        "LCom_Communication_DB".configuration.connection.connectionID := 16#1F4;
	        //used protocol: 1=TCP protocol, 2=LCom protocol
	        "LCom_Communication_DB".configuration.connection.comService := "LCOM_TCP_CONNECTION";//"LCOM_TCP_CONNECTION";
	        //FALSE: passive (server), TRUE: active connection establishment (client)
	        "LCom_Communication_DB".configuration.connection.isClient := FALSE;//FALSE;;
	        //TRUE: accept requests from all partners, FALSE: only specified partner is allowed in case of server
	        "LCom_Communication_DB".configuration.connection.acceptUnknownPartner := TRUE;
	        //local port number (possible values: 1...49151; caution: some may be reserved for internal use; recommended values: 2000...5000)
	        "LCom_Communication_DB".configuration.connection.localPort := 3456;
	        //partner port number, use only in case of client or specified partner (possible values: 1...49151; recommended values: 2000...5000)
	        "LCom_Communication_DB".configuration.connection.partnerPort := 3456;
	        //partner IP address xxx.xxx.xxx.xxx = [1].[2].[3].[4], use only in case of client or specified partner
	        "LCom_Communication_DB".configuration.connection.partnerIP.ADDR[1] := 192;
	        "LCom_Communication_DB".configuration.connection.partnerIP.ADDR[2] := 168;
	        "LCom_Communication_DB".configuration.connection.partnerIP.ADDR[3] := 1;
	        "LCom_Communication_DB".configuration.connection.partnerIP.ADDR[4] := 244;
	        //life sign cycle time if no other data exchange is active (T#1ms...T#24d20h31m23s647ms); only valid with LCom protocol
	        "LCom_Communication_DB".configuration.connection.lifeSignCycleTime := T#1s;
	        
	        //-----------------------------------------------------------
	        // QDN support since LCom_Communication version 2.1.0 !
	        // additionally a DNS server must be configured in PLC
	        //-----------------------------------------------------------
	        // value: 1 = IPv4, 10 = QDN (qualified domain name)
	        "LCom_Communication_DB".configuration.connection.connectType := "LCOM_CON_TYPE_IPV4"; //LCOM_CON_TYPE_QDN;
	        //define interfaceID and partner domain name with use of QDN
	        IF "LCom_Communication_DB".configuration.connection.connectType ="LCOM_CON_TYPE_QDN" THEN
	            // set to "0" for integrated interface; "x" for used CP;
	            "LCom_Communication_DB".configuration.connection.interfaceID := 0;
	            // Fully qualified domain name of the partner end point, which must finish with "."
	            "LCom_Communication_DB".configuration.connection.partnerQDN := 'lcomserver.lan.';
	        END_IF;
	        //-----------------------------------------------------------
	        
	        //-----------------------------------------------------------
	        // Sender configuration
	        //-----------------------------------------------------------
	        //time interval for cyclic communication (T#1ms...T#24d20h31m23s647ms)
	        "LCom_Communication_DB".configuration.sender.cycleTime := T#1s;
	        //timeout in case of missing confirmation (T#1ms...T#24d20h31m23s647ms); only valid with LCom protocol
	        "LCom_Communication_DB".configuration.sender.ackTimeout := T#1s;
	        
	        //-----------------------------------------------------------
	        // Configure time synchronization
	        //-----------------------------------------------------------
	        //TRUE: synchronize local time with received timestamps, FALSE: do not use received timestamps
	        "LCom_Communication_DB".configuration.timeSync.usePartnerTimestamps := FALSE;
	        //time synchronization send mode 0: inactive, 1: cyclic, 2: at time of day
	        "LCom_Communication_DB".configuration.timeSync.sendMode := "LCOM_CYCLE_TIME_SYNC";
	        //time interval for cyclic mode (T#1ms...T#24d20h31m23s647ms)
	        "LCom_Communication_DB".configuration.timeSync.cycleTime := T#1h;
	        //time synchronization at specified time of day (for sendMode = 2 only)
	        // "LCom_Communication_DB".configuration.timeSync.sendAtTimeOfDay := TOD#05:00:0.000; 
	        //-----------------------------------------------------------
	        
	        //-----------------------------------------------------------
	        // Initialize send buffer
	        //-----------------------------------------------------------
	        "LCom_ExampleComData".serializePos := 0;
	        "LCom_ExampleComData".serializeRetval := Serialize(SRC_VARIABLE := "LCom_ExampleComData".userSendData, DEST_ARRAY => "LCom_ExampleComBuffers".sendBuffer, POS := "LCom_ExampleComData".serializePos);
	        "LCom_ExampleComData".sendDataLength := DINT_TO_UDINT("LCom_ExampleComData".serializePos);
	        //----------------------------------------------------------
	        
	        "LCom_ExampleComData".comEnable := TRUE;
	        "LCom_ExampleComData".firstCycle := FALSE;
	    END_IF;
	END_REGION CONFIGURATION
	
	REGION INCREMENT_DATA_VALUES
	    // Increment user variables when FB has send data
	    IF NOT "LCom_ExampleComData".sending AND "LCom_ExampleComData".sendingOld THEN
	        "LCom_ExampleComData".userSendData.counter := "LCom_ExampleComData".userSendData.counter + 1;
	        "LCom_ExampleComData".userSendData.value := "LCom_ExampleComData".userSendData.value + 0.1;
	        
	        // Serialize data before sending
	        "LCom_ExampleComData".serializePos := 0;
	        // "LCom_ExampleComData".serializeRetval := Serialize(SRC_VARIABLE := "LCom_ExampleComData".userSendData, DEST_ARRAY => "LCom_ExampleComBuffers".sendBuffer, POS := "LCom_ExampleComData".serializePos);
	        // "LCom_ExampleComData".sendDataLength := DINT_TO_UDINT("LCom_ExampleComData".serializePos);
	      //  68 61 6C 6C 6F 0A
	        "LCom_ExampleComBuffers".sendBuffer[0] := 16#70;
	        "LCom_ExampleComBuffers".sendBuffer[1] := 16#65;
	        "LCom_ExampleComBuffers".sendBuffer[2] := 16#6e;
	        "LCom_ExampleComBuffers".sendBuffer[3] := 16#69;
	        "LCom_ExampleComBuffers".sendBuffer[4] := 16#73;
	        "LCom_ExampleComData".sendDataLength := 5;
	    END_IF;
	    "LCom_ExampleComData".sendingOld := "LCom_Communication_DB".sending;
	END_REGION INCREMENT_DATA_VALUES
	
	
	REGION CALL_LCOM_FB
	    // Call Communication FB
	    // Data is sent cyclic, when function block is connected
	    "LCom_Communication_DB"(enable := "LCom_ExampleComData".comEnable,
	                            send := "LCom_ExampleComData".connected,
	                            sendDataLength := "LCom_ExampleComData".sendDataLength,
	                            connected => "LCom_ExampleComData".connected,
	                            sending => "LCom_ExampleComData".sending,
	                            dataReceived => "LCom_ExampleComData".dataReceived,
	                            receivedDataLength => "LCom_ExampleComData".receivedDataLength,
	                            busy => "LCom_ExampleComData".comBusy,
	                            error => "LCom_ExampleComData".comError,
	                            status => "LCom_ExampleComData".comStatus,
	                            sendBuffer := "LCom_ExampleComBuffers".sendBuffer,
	                            receiveBuffer := "LCom_ExampleComBuffers".receiveBuffer,
	                            diagnostics := "LCom_ExampleComData".diagnosticData);
	END_REGION CALL_LCOM_FB
	
	REGION ERROR_HANDLING
	    // Error Handling
	    IF "LCom_ExampleComData".comError AND NOT "LCom_ExampleComData".comBusy THEN
	        // User cleared error detected. New rising edge on enable input is necessary to clear the error.
	        // E.g. a wrong configuration value can be a user cleared error
	        "LCom_ExampleComData".comEnable := FALSE;
	        
	    ELSIF "LCom_ExampleComData".comError THEN
	        // Autmatically cleared error detected. The FB tries to solve the problem on it's own.
	        // E.g. if the connection is lost, the FB tries a reconnect to the partner
	        ;
	        
	    ELSE
	        ; // No error detected
	    END_IF;
	END_REGION ERROR_HANDLING
	
	REGION DESERIALIZE
	    // Deserialize data after receiving
	    IF "LCom_ExampleComData".dataReceived THEN
	        "LCom_ExampleComData".deserializePos := 0;
	        "LCom_ExampleComData".deserializeRetval := Deserialize(SRC_ARRAY := "LCom_ExampleComBuffers".receiveBuffer, DEST_VARIABLE => "LCom_ExampleComData".userReceiveData, POS := "LCom_ExampleComData".deserializePos);
	    END_IF;
	END_REGION DESERIALIZE
	*)
END_ORGANIZATION_BLOCK

