Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Array/Struct Index Write

mak1891
2022-09-14
2022-09-16
  • mak1891 - 2022-09-14

    Hello guys. New to codesys here, and trying to write into a position.
    What i want is the following:

    For the positions between 42 and 60, if tool "status" is valid, that tool gets the positions number, if not continue.
    My code does not work, i get values between 45 and 65.

    Code:
    //====================Write Tool Numbers X3 =================
    FOR X3:=42 TO 60 DO
    FOR Index3:=1 TO 22 DO
    IF DbAutomatic.PosX3.Index = 3 THEN
    IF DbAutomatic.PosX3.Status[Index3] THEN
    DbAutomatic.PosX3.ToolNumber[Index3]:=X3;
    DbAutomatic.AllTools[X3].Number:=X3;
    DbAutomatic.AllTools[X3].Xpos:=DbAutomatic.PosX3.XPosition[Index3];
    DbAutomatic.AllTools[X3].Ypos:=DbAutomatic.PosX3.YPosition[Index3];

                    IF X3=61 THEN
                    RETURN; 
                    END_IF
    
                    END_IF  
                END_IF  
            END_FOR
            X3:=X3+1;       
        END_FOR
    

    Can you tell me what i am doing wrong?

    Thank you!

     
  • dFx

    dFx - 2022-09-16

    X3:=X3+1; is part of your issue.
    a FOR loop automatically increments your loop variable.

    //====================Write Tool Numbers X3 =================
    FOR X3:=42 TO 60 DO
        Base1Index := X3-41;
        IF DbAutomatic.PosX3.Index = 3 THEN // looks missplaced or missing array reference ? (I would put this ahead of for loop)
            IF DbAutomatic.PosX3.Status[Base1Index] THEN
                DbAutomatic.PosX3.ToolNumber[Base1Index]:=X3;
                DbAutomatic.AllTools[X3].Number:=X3; // This is weird (copying your own index ? I would expect Base1Index)
                DbAutomatic.AllTools[X3].Xpos:=DbAutomatic.PosX3.XPosition[Base1Index];
                DbAutomatic.AllTools[X3].Ypos:=DbAutomatic.PosX3.YPosition[Base1Index];
            END_IF     
        END_IF    
    END_FOR
    
     

Log in to post a comment.