Showing results 1 to 2 of 2

Thread: Logic, a loop for an array with type variables

  1. #1
    Join Date

    Default Logic, a loop for an array with type variables


    I have a type BatteryInRack:
    Voltage: real;
    Ready: bool;

    and a type BatteryRack:
    Battery1: BatteryInRack
    Battery2: BatteryInRack
    Battery3: BatteryInRack

    I have in Logic a variable:
    BatteryRack: BatteryRack

    I try to assign a value to the Voltage value, for example:
    for i:= 1 to 3 doBatteryRack[i]:= i;end_for;
    But it isn't work.

    Is there a way to assign the value in a Loop?

    Thank you in advance.

    Attached Thumbnails Attached Thumbnails Types.PNG   variable.PNG  

  2. #2
    Join Date

    Default Re: Logic, a loop for an array with type variables

    Hi genakust!

    Which zenon version do you use?

    The way you are trying to implement your battery rack will not work in logic for the following reasons:
    1) You do not have an array of BatteryInRack-Variables within your BatteryRack-Structure => using indices won't work
    2) Usually index variables are of Integer-based datatypes, so you might need a conversion if the index is used as value
    4) Most crucial point: you are trying to assign an Integer-value to a variable of type BatteryInRack => Will never work: how should the BatteryInRack-variablle know which of its sub-variables to take for the value assignment?

    I suggest you to modify your project the following way:
    1) Replace the single BatteryInRack-variables within BatteryRack-type by a variable array (e.g. Batteries) so you can use the index
    2) Rename the BatteryRack-variable to something like MyBatteryRack to avoid name collisions with the same-named datatype
    3) Use the length of the array within for-condition, then you are more flexible: for i:=0 to COUNTOF(MyBatteryRack.Batteries)-1 do
    4) Target the voltage of the BatteryInRack-variables by BatteryRack.[i].Voltage, e.g. BatteryRack.Batteries[i].Voltage
    5) Add an ANY_TO_REAL()- conversion to the value assignment for the voltage, e.g.: BatteryRack.Batteries[i].Voltage = ANY_TO_REAL(i);

    I tried it with version 7.60 and it worked in this case.
    You find screenshots of the adaptions I made attached.

    Click image for larger version

Name:	Program.PNG
Views:	16
Size:	4.0 KB
ID:	2311Click image for larger version

Name:	Datatypes.PNG
Views:	23
Size:	2.9 KB
ID:	2309Click image for larger version

Name:	Variables.PNG
Views:	22
Size:	2.8 KB
ID:	2310

    Hope this helps

    BR, klemensn
    Attached Thumbnails Attached Thumbnails Program.PNG  
    Last edited by klemensn : 9th May 2018 at 09:31 Reason: Small mistake in the program - out of bounds at top limit of the array, therefore -2

Similar Threads

  1. Passing variables to an array by reference
    By jesus_michel in forum straton / zenon Logic
    Replies: 3
    Last Post: 27th February 2018, 18:23
  2. Writing status bits on Zenon Logic Global Variables
    By maurodx in forum straton / zenon Logic
    Replies: 2
    Last Post: 6th November 2017, 13:26
  3. Replies: 5
    Last Post: 20th October 2015, 09:13
  4. Array and licensed I/O variables
    By florian in forum zenon Supervisor
    Replies: 1
    Last Post: 2nd June 2015, 07:35
  5. zenon logic doesn't communicate with external variables in zenon
    By mashjj in forum straton / zenon Logic
    Replies: 6
    Last Post: 27th February 2015, 13:10

Tags for this Thread

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts