Shop OBEX P1 Docs P2 Docs Learn Events
Serial Timeout — Parallax Forums

Serial Timeout

SamTheManSamTheMan Posts: 43
edited 2009-05-26 19:39 in General Discussion
I want to receive a serial data.
but if it timed-out, I want to do some other tasks
if I used this function:

FUNC RX_Byte
· ·tmp var byte
·SERIN Ser_pin, Baud, tmp,__PARAM1,timeout······· ' receive a byte
·return tmp
ENDFUNC


timeout:



can I branch out to timeout lable outside the function?
do I have to add "return" at the end of timeout: ?
how can I go back to where the function was called from?



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Comments

  • BeanBean Posts: 8,129
    edited 2009-05-26 15:18
    You should NOT branch out of the function. That is bad "form", and will mess up the local variable stack.

    Just put a goto after the SERIN to the RETURN.

    FUNC RX_Byte 
      tmp var byte 
      SERIN Ser_pin, Baud, tmp,__PARAM1,timeout ' receive a byte 
      GOTO RX_Done 
    Timeout: 
      ' timeout code here 
    RX_Done: 
      return tmp 
    ENDFUNC
    


    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There is a fine line between arrogance and confidence. Make sure you don't cross it...

    ·
  • JonnyMacJonnyMac Posts: 9,213
    edited 2009-05-26 19:39
    I don't use timeouts very often but when I have to I include an error flag in the program and then code RX_BYTE like this:

    ' Use: byteVal = RX_BYTE
    ' -- receive "byteVal" on "RX" at "BaudMode"
    
    FUNC RX_BYTE
      SERIN RX, BaudMode, __PARAM1, RxMaxTime, RX_Timeout
      toError = No
      GOTO RX_Exit
    
    RX_Timeout:
      toError = Yes
      __PARAM1 = 0
    
    RX_Exit:
      ENDFUNC
    


    In my case __PARAM1 already has the received byte (or 0 if on a timeout) so there is no need for RETURN.
Sign In or Register to comment.