Home AMX User Forum NetLinx Studio

Wait with Vars

Hi all



I need to make this code to work with a delay between every loop iteration:

SET_VIRTUAL_PORT_COUNT (Panel.NUMBER:1:Panel.SYSTEM,PORT_COUNT)
for(port=1;port<=PORT_COUNT;port++)
{
SET_VIRTUAL_CHANNEL_COUNT (Panel.NUMBER : port: Panel.SYSTEM,CHANNEL_COUNT)
}


So the simples way is to put wait in the loop: WAIT 5 SET_VIRTUAL_CHANNEL_COUNT (Panel.NUMBER : port: Panel.SYSTEM,CHANNEL_COUNT)

But its not so simple. The Port and the Panel are stack vars, and if I am changing them to be members or local vars, the loop will change their value before the wait will happen.

Any idea how to implement this without moving to hard coded solution?

Thanks

Ady.

Comments

  • mpullinmpullin Posts: 949
    Try using a recursive function. I have not tested this specific code, but this approach worked for me in a similar situation.
    DEFINE_FUNCTION setChanCounts(INTEGER port, INTEGER chan_count, INTEGER port_count){
       if(port > port_count) return; // base case - don't do a port that doesn't exist.
       SET_VIRTUAL_CHANNEL_COUNT(Panel.NUMBER:port:Panel.SYSTEM,chan_count) // do whatever you want with port
       wait 15 setChanCounts(port+1,chan_count,port_count); // do next port
    }
    

    Then, in your button_event or what have you, call setChanCounts(1,CHANNEL_COUNT,PORT_COUNT)

    Hope that works for you.
  • AMXJeffAMXJeff Posts: 450
    Confused as to the reason for the delay... The best thing to do is to put the wait in mainline. If you must use a loop, waits do not work in "for" loops, only long_while...

    // Option 1
    DEFINE_START
    
    SET_VIRTUAL_PORT_COUNT (Panel.NUMBER:1:Panel.SYSTEM,PORT_COUNT);
    
    bINIT_PANEL = TRUE
    
    (***********************************************************)
    (*                THE EVENTS GO BELOW                      *)
    (***********************************************************)
    DEFINE_EVENT
    
    (***********************************************************)
    (*            THE ACTUAL PROGRAM GOES BELOW                *)
    (***********************************************************)
    DEFINE_PROGRAM
    
    IF (bINIT_PANEL == TRUE)
    {
    	LOCAL_VAR INTEGER nPort;
    
    	WAIT 5
    	{
    		IF (nPort < PORT_COUNT)
    		{
    			nPort++;
    			SET_VIRTUAL_CHANNEL_COUNT(Panel.NUMBER:nPort:Panel.SYSTEM, CHANNEL_COUNT);
    		}
    		ELSE
    			bINIT_PANEL = FALSE;
    	}		
    }
    

    // Option 2
    (***********************************************************)
    (*                STARTUP CODE GOES BELOW                  *)
    (***********************************************************)
    DEFINE_START
    
    //PANEL INIT
    {
    	LOCAL_VAR INTEGER nPort;
    	
    	SET_VIRTUAL_PORT_COUNT (Panel.NUMBER:1:Panel.SYSTEM,PORT_COUNT);
    
    	LONG_WHILE (nPort < PORT_COUNT)
    	{
    		WAIT 5
    		{
    			nPort++;
    			SET_VIRTUAL_CHANNEL_COUNT(Panel.NUMBER:nPort:Panel.SYSTEM, CHANNEL_COUNT);
    		}
    	}
    }
    
    adys wrote:
    Hi all



    I need to make this code to work with a delay between every loop iteration:

    SET_VIRTUAL_PORT_COUNT (Panel.NUMBER:1:Panel.SYSTEM,PORT_COUNT)
    for(port=1;port<=PORT_COUNT;port++)
    {
    SET_VIRTUAL_CHANNEL_COUNT (Panel.NUMBER : port: Panel.SYSTEM,CHANNEL_COUNT)
    }


    So the simples way is to put wait in the loop: WAIT 5 SET_VIRTUAL_CHANNEL_COUNT (Panel.NUMBER : port: Panel.SYSTEM,CHANNEL_COUNT)

    But its not so simple. The Port and the Panel are stack vars, and if I am changing them to be members or local vars, the loop will change their value before the wait will happen.

    Any idea how to implement this without moving to hard coded solution?

    Thanks

    Ady.
  • mpullinmpullin Posts: 949
    AMXJeff wrote:
    Confused as to the reason for the delay... The best thing to do is to put the wait in mainline.
    Not a bad solution, but if you do this make sure that PORT_COUNT and CHANNEL_COUNT can't be changed somewhere else in the program while bINIT_PANEL is true.
  • adysadys Posts: 395
    thanks

    Jeff that is a good one. the only problem is that its online event that listen to 14 TPs so I need to change it a little, but thats a good idea.

    The recursion is also an option, maybe I will try it.

    The reason for the wait is that I am probably loosing some of the messages when my TP gets online, and some TPs works fine and some are not :http://www.amxforums.com/showthread.php?t=3754

    Must do the maximum to make sure this is not the problem..
  • ericmedleyericmedley Posts: 4,177
    If the timing is supposed to be asyncronous (you have to wait for responces from devices before moving on to next step) you probably want to do it in mainline.

    If you're just trying to slow down the loop, you might be able to put it in a timeline.

    WAITs do not work within a for loop.

    I wish Netlinx FOR loops worked like old BASIC loops in that you had to put a 'NEXT' command in the loop to trigger the next loop cycle. a lot of BASIC interpretors also would allow you to leave it out and the compiler would assume a NEXT at the end of the command structure. So, if you just wanted the loop to run at 'thread' speed, it would. But if you did need to introduce a little bit of dealy it was no big deal.
  • adysadys Posts: 395
    I just need some delay

    When debugging the loop, everything is working cause its a "slow" loop...
  • viningvining Posts: 4,368
    This should work as well.
    DEFINE_CONSTANT	  
    
    PORT_COUNT = 12 ;      //number of ports. use an array if ports are to be skipped
    CHANNEL_COUNT = 1024 ; //number of desired virtual channels
    
    DEFINE_VARIABLE     
    
    VOLATILE INTEGER nPortCounter ; 
    
    DEFINE_START    
    
    nPortCounter = 1 ;
    
    DEFINE_PROGRAM
    
    wait 5
         {
         if(nPortCounter <= PORT_COUNT)
    	  {
    	  SET_VIRTUAL_CHANNEL_COUNT (Panel.NUMBER : nPortCounter: Panel.SYSTEM,CHANNEL_COUNT)
    	  nPortCounter ++ ;
    	  }
         }
    

    If you don't have any other feedback statements to go under the wait I would probably swap the "wait" and the "if" so tha the processor isn't always maintaining a wait for a statement that will always evaluate as false except after a reboot.
  • why not using a timeline

    it could be a goo way to make a loop, you can change the "waiting" time between events changing timeline times array... maybe you should think about this ?
Sign In or Register to comment.