Search code examples
plcstructured-text

Issue in assigning priority to plc based motor


I want to assign a unique priority to each element in an array, but with the snippet below I'm getting the same priority m repeated for elements with the same working time values. How do I set a unique priority for each element?

// assign priority number according to working times
#m := 0;
FOR #m := 0 TO 10 DO
    FOR #l := 0 TO 10 DO
        IF #OrderedList[#m] = #WorkingTimes[#l]."Time" THEN
            #WorkingTimes[#l].Priority := #m;
        END_IF;
    END_FOR;
END_FOR;

Solution

  • Set some default priority then exit the inner FOR loop when you update an element

    // assign priority number according to working times
    
    FOR l := 0 to 10 DO
        WorkingTimes[l].Priority := -1;
    END_FOR;
    
    FOR m := 0 TO 10 DO
        FOR l := 0 TO 10 DO
            IF OrderedList[m] = WorkingTimes[l].Time AND WorkingTimes[l].Priority = -1 THEN
                WorkingTimes[l].Priority := m;
                EXIT;
            END_IF;
        END_FOR;
    END_FOR;
    

    I haven't tested this, but the concept should work.