Home AMX User Forum NetLinx Studio

While Loop Question

This will probably be a dumb ? but i have seen several reference to an infinite loop, if you create this situation via incorrect code and the master sticks in the loop can you just resend another file. Or is it locked up once and for all.

Comments

  • ColzieColzie Posts: 470
    Resending code (or even rebooting the processor) will end the loop. The processor is not ruined.
  • Loop

    Thanks, i can get creative now then, and try and make my rs232 command queue
  • HedbergHedberg Posts: 671
    If you do happen to get your code to lock up your master (and I know that this can be done), you can set the dipswitches on the back of the master to prevent the program from running and then load new code and reset the switches to run. Not a lot of fun, but you can do it. It's also possible to reboot the master and get code loading before the master actually processes any code. I had to do this once when I was programming a master in a remote city via M2M, which is painful enough without hosed code.
  • a_riot42a_riot42 Posts: 1,624
    Often an infinite loop will steal all processor cycles and so a resend isn't possible. Sometimes you can't even telnet to it, and it has to be reboot by pulling power (no fun on a remote job). Just write your rs232 code so that right after the while condition is met, you immediately falsify it so that an infinite loop can't happen.

    ie:
    while (find_string(sRxBuf,"$0D",1))
    {
    sReply = remove_string(sRxBuf,"$0D",1)

    // more code here
    }

    This while loop can never run infinitely.
    Paul
  • TurnipTruckTurnipTruck Posts: 1,485
    Try checking your queue in the DEFINE_PROGRAM section. If there is anything in the queue and some condition is true, you can send the command and remove it from the queue.
  • ColzieColzie Posts: 470
    a_riot42 wrote: »
    Often an infinite loop will steal all processor cycles and so a resend isn't possible.

    I've never run into this situation (not saying it isn't possible). I've had code get into infinite loops but I could still resend code.
  • mpullinmpullin Posts: 949
    Colzie wrote: »
    I've never run into this situation (not saying it isn't possible). I've had code get into infinite loops but I could still resend code.
    I agree with Colzie, infinite loops don't prevent one from rebooting the master.

    While loops (and recursive functions for that matter) can be a powerful tool in your programming arsenal, you just have to use some logic to make sure that every pass through the loop will approach a state where the while condition is false. The loop posted by a_riot, a good example, will always approach a state where there is no $0D in the buffer because each pass through the loop removes the first instance of $0D from the buffer. Since we don't know beforehand how many $0D will be in the buffer, a while is more appropriate than, say, a for loop.
  • DHawthorneDHawthorne Posts: 4,584
    It depends how tight the loop is and how fast the processor jumps into it. I've once or twice created one so tight that the processor never fully started up, and could not be connected to at all. In those cases, only the dip switch 1 solution works. Other times, I've been able to load the fixed code and be done.
  • jjamesjjames Posts: 2,908
    Colzie wrote: »
    I've never run into this situation (not saying it isn't possible). I've had code get into infinite loops but I could still resend code.

    Weird - I have. Not sure what the circumstances are, but I think it was a WHILE loop.
  • a_riot42a_riot42 Posts: 1,624
    mpullin wrote: »
    I agree with Colzie, infinite loops don't prevent one from rebooting the master.

    Interesting. I wonder why my master wouldn't reboot or even accept a telnet connection when I accidentally misplaced a parenthesis that created an infinite while loop. Perhaps the moon was full that day.
    Paul
  • mpullinmpullin Posts: 949
    DHawthorne wrote: »
    It depends how tight the loop is and how fast the processor jumps into it. I've once or twice created one so tight that the processor never fully started up, and could not be connected to at all. In those cases, only the dip switch 1 solution works. Other times, I've been able to load the fixed code and be done.
    Let's let this be the definitive post on the issue.
  • a_riot42a_riot42 Posts: 1,624
    mpullin wrote: »
    While loops (and recursive functions for that matter) can be a powerful tool in your programming arsenal,

    First rule of programming: Never use recursion.
    Second rule of programming: See rule number one.
    mpullin wrote: »
    you just have to use some logic to make sure that every pass through the loop will approach a state where the while condition is false.

    That's obvious, but its easier said than done. Using an invariant you can mathematically prove a while loop will terminate. A nice-to-have in the 'arsenal'.
    Paul
  • DHawthorneDHawthorne Posts: 4,584
    My experience has been it's not usually a matter of making some egregious programming error ... it's usually a silly typo, or missed line that creates an endless loop. The key here is knowing what to do about it when the poop hits the fan.
  • mpullinmpullin Posts: 949
    a_riot42 wrote: »
    That's obvious, but its easier said than done. Using an invariant you can mathematically prove a while loop will terminate. A nice-to-have in the 'arsenal'.
    Paul
    What is your problem "Paul"? I was addressing the original thread poster, who implied that he saw while loops as "incorrect code" so I was trying to explain how while loops can be used well. You jumping in and labeling my explanation as "obvious" strikes me as being rather sniveling. Especially after I complimented your example!

    Using logic in programming IS easier said than done. But that shouldn't stop one from doing it.
  • a_riot42a_riot42 Posts: 1,624
    mpullin wrote: »
    What is your problem "Paul"? I was addressing the original thread poster, who implied that he saw while loops as "incorrect code" so I was trying to explain how while loops can be used well. You jumping in and labeling my explanation as "obvious" strikes me as being rather sniveling. Especially after I complimented your example!

    No offense intended. What is not obvious about loops is how to achieve guaranteed loop termination in all circumstances regardless of what the loop does and that is where the loop invariant comes into play so I thought I would mention it. The OP wasn't implying that loops are incorrect code in and of themselves, but that through incorrect code, infinite loops can happen.

    I also wanted to make the point that recursion isn't generally a good idea in programming and in fact most compilers will take a recursive function and convert into a while loop. The overhead for a recursive function is so large that invariably they overflow the call stack if you aren't careful.
    Paul
  • mpullinmpullin Posts: 949
    a_riot42 wrote: »
    No offense intended. What is not obvious about loops is how to achieve guaranteed loop termination in all circumstances regardless of what the loop does and that is where the loop invariant comes into play so I thought I would mention it. The OP wasn't implying that loops are incorrect code in and of themselves, but that through incorrect code, infinite loops can happen.

    I also wanted to make the point that recursion isn't generally a good idea in programming and in fact most compilers will take a recursive function and convert into a while loop. The overhead for a recursive function is so large that invariably they overflow the call stack if you aren't careful.
    Paul

    All right... sorry about that then.

    You make some good points. I had some memory issues with a system last week, stuff was getting allocated and not returned, and certain events (e.g. compiling with debug info) were pushing my NI-3000 over the edge. It got me thinking about strategy in memory management which, although I have programmed in many languages on many platforms, had never been a problem for me before.

    We since ordered an NI-3100 to get more memory... but I still am reviewing the code to try to figure out how we can better make use of our resources.

    I usually use recursive functions to calculate, or to do some type of unorthodox parsing. I remember once I had a function taking a DEV and a CHAR[] that would lop off the most significant digit, pulse that digit to the DEV, then after a wait, send the remainder of the string and the same DEV to itself (the function). (you can't put a stack var in a wait, but a function parameter is A-OK) So the process would keep going until there was just an empty string, in which case we'd pulse an Enter command and stop.

    Of course then someone had to go and point out that this was built in via the XCH command. But this is the kind of thing I'd use a recursive function for.
Sign In or Register to comment.