waiting in a procedure

Discussion in 'C-Touch/HomeGate/SchedulePlus/PICED Software' started by CABrouwes, Oct 15, 2011.

  1. CABrouwes

    CABrouwes

    Joined:
    Apr 25, 2009
    Messages:
    86
    Likes Received:
    2
    Location:
    New York Area
    Is there a way to make a a procedure wait (not a module).
    I am writting code involving a Wiser comnunicating with another device through a TCP socket. I need the program to wait for the socket to open. The code works well when it is residing within a module. However, to avoid repeating the same code many times, I would like the code to be in a procedure but I can't find a way to implement the waiting procedure.

    1. The "wait" and "waituntil" instructions are not available in procedures.
    2. Using an "until" loop generates an error because the number of instructions per scan cycle is exceeded.

    A work around found is to add a "slow" instruction to the waiting loop like making an http request to an external website, but this is really not clean.

    Here is the code. Note IPSwitch is a pascal record structure, the names of the fields are self-explanatory.

    Procedure SwitchIP(TheSwitch : IPSwitch; OnOrOff : Boolean);
    Var cTime : Integer;
    begin
    With theSwitch do
    Begin
    { Open socket and wait until it is open }
    OpenClientSocket(SwitchSocket,Address, Port);
    cTime := RunTime + 3 ;
    repeat
    until ClientSocketConnected(SwitchSocket) or (Runtime > cTime) ;
    { Send the on or off message string to socket }
    if OnOrOff then
    begin
    WriteClientSocket(SwitchSocket, OnString);
    end
    else
    begin
    WriteClientSocket(SwitchSocket, OffString);
    end;

    CloseClientSocket(SwitchSocket);
    end ;
    end;
     
    CABrouwes, Oct 15, 2011
    #1
  2. CABrouwes

    Darren Senior Member

    Joined:
    Jul 29, 2004
    Messages:
    2,361
    Likes Received:
    0
    Location:
    Adelaide, South Australia
    No, there isn't. The way that the logic engine operates prevents this from working.
     
    Darren, Oct 16, 2011
    #2
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.