patrickokeeffe

continue ignored in while loop

Recommended Posts

Does try...catch do something undocumented regarding continue? I've been debugging the sequence below and the whole sequence halts anytime it hits catch(), instead of continue'ing as I had expected. It doesn't ever reach the 3rd print statement (after the continue) but it does fail to continue the while loop. My alert window shows these two messages and the sequence gets ended.

Quote

Ignoring error: C1136 Timeout: read_200wx_nmea0183 Line 34
finished delaying... next while loop

 

I'm using DF 18.1 bld 2347 on Windows 10 Enterprise, with 'Run as administrator' compatibility mode enabled. I'm not using ignore(..) because I plan to re-initialize comms if C1136 occurs.

(the try-catch block is my attempt to resolve this issue that's cropping up again: serial data streams cut out after indeterminate length of time and require reinitialization through the configuration menu)

 

Auto-start sequence named "read_200wx_nmea0183":

private string msg
private string msgid
private string vals
private v1
private v2

while (1)
   try
      msg = Device.AIRMAR_200WX.ReadUntil(CHR(10))
   catch ()//"C1136")
      ? "Ignoring error: " + strLastError
      delay(0.5)
      ? "finished delaying... next while loop"
      continue
      ? "should never reach here, after continue"
   endcatch

   if (Compare(Left(msg,1),"$"))
      continue // skip partial message
   endif
   // WARNING message checksum will be discarded without use

   msgid = Mid(msg,1,5) // defer data parsing until matching header is found
   switch
      //
      // ... lots of code removed ...
      //
   endcase
endwhile

 

Share this post


Link to post
Share on other sites

Possibly.  Personally I typically reverse my logic, checking for valid, rather than invalid cases, so don't use continue much.  In your case, I would consider putting the entire script inside the while inside a try/catch block rather than just the comms handling.  

Share this post


Link to post
Share on other sites

The help for while loops (section 5.14) says this about continue:

Quote

The continue statement causes the sequence to jump back to the while statement, reevaluate the condition and either start the loop over, or break out of it

It works as expected inside an if block (the second instance which discards partial messages) but inside the catch block it behaves funny. When serial comms fail, my command window should be littered with these two messages but instead they print once each and the sequence ends. 

So far I just added debug statements to help determine where the loop ends when it catches an error. I think it does make sense though to relocate the try-catch so it contains the whole script.

Share this post


Link to post
Share on other sites

So I rearranged my try-catch block to wrap the entire script:

while (1)
   try
      msg = Device.AIRMAR_200WX.ReadUntil(CHR(10))

      if (Compare(Left(msg,1),"$"))
         continue // skip partial message
      endif
      // WARNING message checksum will be discarded without use

      msgid = Mid(msg,1,5) // defer data parsing until matching header is found
      switch
         //
         // ... lots of code removed ...
         //
      endcase

   catch ("C1136")
      ? "Caught serial comm error: " + strLastError
      delay(5)
      Device.AIRMAR_200WX.Purge()
      Device.AIRMAR_200WX.InitComm()
      //continue
   endcatch

   catch ()
      ? "Ignoring error: " + strLastError
   endcatch
endwhile

With the second continue statement enabled, the sequence halts but without it, the loop keeps running until the comm error is resolved. Something about the catch block turns the continue into a break instead. 

Share this post


Link to post
Share on other sites

Sounds like a bug.  I've noted it so it is looked at and fixed in a future release.  I tested with this simple sequence and it looks like continue inside of a catch() doesn't just break out of the while, but ends the sequence:

while(1)
   delay(0.1)
   try
      ? 'a'
      ? b
   catch()
      ? 'c'
      continue
   endcatch
   ? 'd'
endwhile
? 'e'
   

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now