PDA

View Full Version : timeout of Hserin, goto, gosub or both?



flipper_md
- 26th October 2009, 17:32
Hi All


does anybody knows how TimeOUT and Hserin behave with sub(s) routine(s)?

Manual refer to "Jump To", but can it jump to a sub and come back with return.(would be nice)

or it would be a GOTO, with a point of no return?

my code behave weirdly sometimes..



Ex program:



Main:

LOW RED_LED ; Error led off
HSEROUT ["GIMEDATA",13]
GoSub BufferChar

;Show received
if RED_LED=0
DEBUG StrList[0],StrList[1],StrList[2]
else
DEBUG "ERROR",13
endif

GOTO Main


;A buffering char sub routine
BUfferChar:
gotchar=0
Hserin 50, NoData, [return_code]
While return_code<>13
Hserin 50, NoData, [return_code]
StrLIst[gotchar]=return_code
gotchar=gotchar+1
WEND

RETURN


NoData: ; Called by many routines that uses Hserin

RCSTA.4 = 0 ; disable receiving of any other char

return_code = 13 ; will end the While loop
HIGH RED_LED ; Show Error led

RCSTA.4 = 1 ; Enable receiving

RETURN

END

aratti
- 26th October 2009, 21:52
It is a GOTO.

I think that you should place a limit to the While/Wend othewise if the Chr 13 doesn't arrive, gotchar=gotchar+1 will grow bigger than your array, and you will corrupt some data in your program.

If gotchar>X then do something could help.

Al.

flipper_md
- 26th October 2009, 23:19
Oh, thanks for that!

I wonder actually, how that "WHILE" would react since HSerin is doing a GOTO somewhere else.
Either that loop automatically get closed or it create a bug...

I guess I should not use serial-In that way.


and now I get even more confused about GOTO's inside a gosub? :) is that clean?

Any GO-GO masters?

aratti
- 27th October 2009, 08:10
Difficult to give suggestions without knowing what you have in mind to achieve. The below is a simple way to get out of the While/Wend loop.



StrLIst var byte[10] ' for example not knowing the true dimension
gotchar var byte


BUfferChar:
gotchar=0
return_code=0
low HIGH RED_LED
While return_code<>13 or gotchar >9
Hserin 50, NoData, [return_code]
StrLIst[gotchar]=return_code
gotchar=gotchar+1
WEND
RETURN


Nodata:
HIGH RED_LED ; Show Error led (use the high state of this pin to workout the timeout in the main routine)
Return



Al.

flipper_md
- 28th October 2009, 01:04
well, I believe as you suggest that it's ok to break a While loop...

but what's tickle me is the GOTO timeout of Hserin, he would break out of a GoSub.

If it's really a GOTO, there must not be any trace of where it was initiated in the stack, so NoData will not know where to return...
And even more buggy, the BufferChar Gosub will not be completed.
hence creating a bug.

So in the end, I would believe it's not safe to use either a GOTO or a HSerin inside a Gosub.

aratti
- 28th October 2009, 07:53
If it's really a GOTO, there must not be any trace of where it was initiated in the stack, so NoData will not know where to return...
And even more buggy, the BufferChar Gosub will not be completed.
hence creating a bug.

When your program jumps to NoData label, in the stacker is still pending the last GOSUB instruction (the one that directed the program flow to the routine under BUfferChar label), so no matter how many GOTO you will use, the first RETURN will clear the last pending gosub and pointer will return the program flow to the main program routine (if the gosub had been executed there). And this is what will happen with the RETURN under NoData label.

Al.

flipper_md
- 28th October 2009, 18:43
very interres-thing!

thanks much for this Al

So it means goto's are safe inside a gosub, as long as there is a return in the end. I like that!