Ending a program on error

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
victorf
Posts: 342
Joined: 01 January 2006, 4:08 AM
Location: Schenectady, New York

Ending a program on error

Post by victorf »

Suppose I detect an error during execution that does not crash the program. Something like a bad return condition from a function. I might want to end the program at that point. I searched the docs for something like Stop or End but the best I could do is Exit. Exit can only get you out of one level of the program (i.e. Exit Sub ) but that does not terminate the program. I see ExitTask but don't get the impression that it will do what I want. If I am NOT running multiple tasks does it end the program?

Any enlightenment will be appreciated.

Vic
Vic Fraenckel
KC2GUI
windswaytoo ATSIGN gmail DOT com
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

There are multiple ways to end your program. For example:
1. Use ExitTask (or Exit Sub) on Main if you only have one task
2. Use SetJmp()/LongJmp() to jump to some exit routine - see http://www.zbasic.net/doc/ZBasicSysLib/ ... ib116.html
3. Use some kind of infinite loop such as:

Code: Select all

Sub Main()
again: 
goto again
End Sub
Mike Perks
wwwoholic
Posts: 53
Joined: 23 December 2010, 20:58 PM

Post by wwwoholic »

Wow, this is VERY old topic.

The new requirements forced me to add a lot of safeguarding code into my programs recently. Sensor inputs out of range, logical inconsistencies in data, stuff like that. So, I started looking for a way to unconditionally and immediately exit the program from any point, including non-Main tasks.

The answers in the previous post are rather workarounds than a solution. Having system level Exit() routine would help a lot here.

I understand that "exiting a program" is weird concept for micro-controllers and any possible implementation would in the end do exactly that - loop indefinitely somewhere. However being a system routine it can also do some additional stuff like disabling interrupts, tri-stating pins, going into hardware sleep mode... basically making sure that controller turns into harmless brick that hardly consumes any power, does not output anything and does not react to inputs.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

wwwoholic wrote:[...] basically making sure that controller turns into harmless brick that hardly consumes any power, does not output anything and does not react to inputs.
Disabling interrupts and entering sleep mode could be done but changing the state of I/O pins would probably not be a good idea because the "safe" state for a given I/O pin can't be known.

Further, it might be a good idea for an application to have an LED or other indicator that conveys the fact that the device has been idled.

It would be simple enough to write a subroutine that disables interrupts and enters sleep mode. That subroutine could also do whatever additional configuration is appropriate for the target application.

Code: Select all

Sub vegetate()
   Dim intFlag as Byte
   intFlag = DisableInt()
   ' do other preparation here
   Call CPUSleep(&H04)
End Sub
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

If you wanted to ensure that the vegetate() sub never returns it could be modified thus:

Code: Select all

#pragma warning(8: off)
Sub vegetate()
   Dim intFlag as Byte
   intFlag = DisableInt()
   ' do other preparation here
   Call CPUSleep(&H04)
   Do
   Loop
End Sub
#pragma warning(8: default)
- Don Kinzer
wwwoholic
Posts: 53
Joined: 23 December 2010, 20:58 PM

Post by wwwoholic »

Good point re. "safe" pin state for specific application. Definitely should be done on case-by-case basis, not in system procedure.

Speaking of system procedures... IDE highlights "stop" as reserved word :)
GTBecker
Posts: 616
Joined: 17 January 2006, 19:59 PM
Location: Cape Coral

Ending a program on error

Post by GTBecker »

> ... "safe" pin state for specific application...

Isn't this the same concern of uncontrolled function while in the Reset
state, i.e. all floating pins? If the design tolerates all floating
pins (which, I suggest, it must) can't that be a predictable and safe
state in which to "halt"?


Tom
Tom
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

wwwoholic wrote:IDE highlights "stop" as reserved word
Yes, Stop is a reserved word, listed in Appendix A of the ZBasic Language Reference Manual. The IDE highlights it because it is in the value list of keywordclass.bas, defined in bas.properties (which can be opened from the Options menu.

You may edit the keyword list in the bas.properties file but you'll have to remember to do so again when a new version of ZBasic is installed. Alternately, you can copy the definition of keywordclass.bas to your UserOptions file (again, accessible from the Options menu) and edit it there.
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Ending a program on error

Post by dkinzer »

GTBecker wrote:Isn't this the same concern of uncontrolled function while in the Reset state, i.e. all floating pins?
That is correct.
- Don Kinzer
Post Reply