Endless loop: Main program terminated by M99
Login to Your Account
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2011
    Post Thanks / Like
    Likes (Given)
    Likes (Received)

    Question Endless loop: Main program terminated by M99

    I'm adding new features to the rs274ngc G-code interpreter to make it more compatible with Fanuc and other controllers. One such feature is numbering the main or sub-program with e.g. O1, and adding the M98 and M99 codes. (By contrast, rs274ngc-flavored code allows no main program number, and sub-programs are defined with O1 sub...O1 endsub and called with O1 call.)

    Smid 3rd Ed. offhandedly mentions on p. 384, "Subprogram Functions" section, para. 2:

    Although M99 is mostly used to end a subprogram, it may also
    be rarely used in the main program, replacing the M30 function.
    In this case, the program will run 'forever', or until the _Reset_
    key is pressed.
    On p. 488, Smid mentions the use case of a machine warm-up program where the program runs endlessly until the operator toggles block skipping, which enables a / M30 block. Another use case is in bar cutting.

    My question for you experienced machine operators out there is, how is a main program distinguished from a subprogram? Consider the following example:

    O1 (Main program)
    (print, Main program 1: calling subprogram 2)
    M98 P2
    O2 (Subprogram)
    (print, Subprogram 2:  returning to main program 1)
    The G-Wizard guy explores endless loops at length in a tutorial; look about 40% down the page for the section "The G-Wizard CNC Editor Can Help You Debug Macros and Identify Infinite Loops". He says Mach3 prohibits endless loops, and

    Most g-code dialects, when faced with an M99 and no M98 having
    called will simply start executing the very first line of g-code
    at the top of the program again.
    This seems to make sense in the tutorial's examples, where there is no O-word indicating the main program number. But those examples and others I can find (e.g. a Haas lathe manual) don't treat the case of the numbered main program.

    I can see two ways to handle this:

    1) Simply pick the first numbered program (O1 in the above example) as the main program. The Fanuc examples all seem to have subprograms following the main program. However, I don't know if this is a requirement, and the rs274ngc case isn't instructive, since O sub-style subroutines may come either before or (now that I unbroke a several-year-old regression) after.

    2) Prohibit M99 terminating numbered programs. The controller would raise an error for the above example, and the example could be fixed by removing the O1 block.

    Any suggestions?

  2. #2
    Join Date
    Sep 2010
    Post Thanks / Like
    Likes (Given)
    Likes (Received)


    Fanuc control does not differentiate between main program and subprogram. All programs are registered in the same manner, with different program numbers. Any program may call any other program, provided it is logically correct. Nesting up to four levels is also allowed.
    Normally, main programs end with M30/M02, and subprograms/macros with M99.

  3. Likes zultron, Greg White liked this post
  4. #3
    Join Date
    Jan 2021
    Post Thanks / Like
    Likes (Given)
    Likes (Received)


    Hi all,

    I found a solution to avoid that thing.

    My O0100 is a macro to reset the machine status.
    It can be called in MDI and as MAIN programm to.

    the macro look like that:

    move safely
    #100=99(DEFAULT M99 END CODE)
    IF[#4000EQ100]THEN#100=30(USE M30 INSTEAD OF M99 IF O0100 SELECTED AS MAIN)

    I hope that can helps

Tags for this Thread


Posting Permissions

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