Runtime Error traps on ERR=0

General FreeBASIC programming questions.
nobozoz
Posts: 238
Joined: Nov 17, 2005 6:24
Location: Chino Hills, CA, USA

Runtime Error traps on ERR=0

Postby nobozoz » Sep 14, 2013 6:53

Why would fb.24 and fb.90.1 trap a runtime error for ERR=0 which means "No Error" (not "Unknown Error") according to the Freebasic Manual?
I can't for the life of me figure this out - I've never encountered this before.

Jim

Edit-1: These lines caused the problem:

Dim Shared As Integer iToggler
Dim Shared As Integer iCommNum
iCommNum=FreeFile
iToggler=OPEN ("Com1:9600,n,8,1,CS0,DS0,CD0,OP0" FOR RANDOM AS iCommNum )

The COM port opens OK, but at some point fb decides that it doesn't like iCommNum. If I replace iCommNum with a literal value like #67, fb won't complain. A bug, maybe? Or maybe its intentional that FreeFile can't be used with OPEN COM. I didn't see any warnings in the docs.
counting_pine
Site Admin
Posts: 6173
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Runtime Error traps on ERR=0

Postby counting_pine » Sep 14, 2013 14:39

It might be helpful to know how the error is manifesting - it sounds like you have an error handling routine. It also might be useful to see how you are writing things to the port.
Still, I have a couple of possible ideas:
- in your error handling code, one of the very first things you should do is save the Err value, because it is quite volatile and any function that may return an error, such as Print, will change Err to reflect its own error status, which will usually be 0.
- make sure iCommValue never changes.  If a literal number works fine in file operations, so should a variable that is equal to that number.
Print its value occasionally to make sure it's a valid number between 1 and 255, and doesn't change.
- a couple of obvious points: make sure you're Closing the port somewhere, and make sure your error handling code isn't being entered into when no error is occurring. Error handlers are often put at the end of a routine, so you have to make sure either that the routine exits before the error handler, or put the label and code inside an 'if 0 then' block or something.
nobozoz
Posts: 238
Joined: Nov 17, 2005 6:24
Location: Chino Hills, CA, USA

Re: Runtime Error traps on ERR=0

Postby nobozoz » Sep 15, 2013 8:36

Thanks for the suggestions.
It turns out that the return value from Freefile() that I was holding in use when the program was initialized was colliding with another hard coded instance of OPEN file for RANDOM as #n where #n was already being used. ON ERROR GOTO apparently trapped the collision, but gave Err=0 anyway.
Getting rid of all the hard coded OPEN ... AS #n instances cured the collision problem.

Jim
fxm
Posts: 9258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Runtime Error traps on ERR=0

Postby fxm » Sep 15, 2013 9:18

Code: Select all

On Error Goto Er
Dim As Integer f = Freefile
Open "text1.txt" As #f
Open Com "COM1:9600,N,,2" As #1
Close #1
Close #f
End

Er:
Print "Error:"; Err
Sleep

Code: Select all

Error: 0
Because 'Print "Error:"' overwrites "Err".

Code: Select all

On Error Goto Er
Dim As Integer f = Freefile
Open "text1.txt" As #f
Open Com "COM1:9600,N,,2" As #1
Close #1
Close #f
End

Er:
Dim As Integer E = Err
Print "Error:"; E
Sleep

Code: Select all

Error: 1

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests