Shop OBEX P1 Docs P2 Docs Learn Events
Timing out a serial connection? — Parallax Forums

Timing out a serial connection?

DiverBobDiverBob Posts: 1,108
edited 2009-02-02 22:08 in Propeller 1
I haven't found any posts with this particular problem...

I have a serial connection as part of my initialization routine where the connection is initialized, 2 characters are transmitted and then I wait for a response. But it is possible that there may not be any response - using the standard .rx command in FullDuplexSerialPlus.spin the program then hangs at this point until I cut the power. Is there a method for timing out the connection if there is no response after a reasonable amount of time so the rest of the app can run?

Thanks

Bob Sweeney

Comments

  • TreeLabTreeLab Posts: 138
    edited 2009-02-01 16:20
    There is a rxcheck routine that returns immediately; if the result is -1, there was no character ready, otherwise it brings back the first received character (ie the first in the receive buffer). You can put a call to rxcheck in a timing loop to see if a coherent response is obtained in a specific time.

    There is an rxtime routine as well that does this for a specific delay, but this blocks the execution cog from doing other things while in the polling loop.

    Cheers!
    Paul Rowntree

    Post Edited (TreeLab) : 2/1/2009 4:25:40 PM GMT
  • DiverBobDiverBob Posts: 1,108
    edited 2009-02-01 16:31
    I hadn't been online for about 3 days, I posted my issue, then started reading all the current posts and wouldn't you know it, someone else with a simular issue and solution! Teach me to finish reading before posting!
    Thanks for the info, I had looked over the rxcheck command but I guess it didn't sink in that it only checks the rx buffer for input. I can put the rxcheck in a 1 or 2 second timing loop and if the buffer is still empty I can push the app into 'test mode' and run fake data instead of waiting forever for serial data!

    Thanks again!

    Bob Sweeney
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-02-01 20:32
    Hello Bob,

    take a look into the RxTime-method inside the FullDuplexSerial-object

    best regards

    Stefan
  • DiverBobDiverBob Posts: 1,108
    edited 2009-02-01 20:47
    I did look at the rxtime routine but for some reason it didn't seem to have a time-out. Can you explain how it is supposed to work?

    Bob
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2009-02-01 20:59
    a:=serial.rxtime(100)

    That will try for 100ms and if no response returns -1
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-02-01 21:32
    Hello Bob,

    you should trust a little more in the comments of the code of FullDuplexSerial

    PUB rxtime(ms) : rxbyte | t
    
    '' Wait ms milliseconds for a byte to be received
    '' returns -1 if no byte received, $00..$FF if byte
    
      t := cnt
      repeat until (rxbyte := rxcheck) => 0 or (cnt - t) / (clkfreq / 1000) > ms
      
    
    



      t := cnt 
    
    




    stores a snapshot of the systemcounter in variable t

      (cnt - t) / (clkfreq / 1000)
    
    



    calculates the amount of time that has been gone since the snapshot

    and as this time is MORE than the value of "ms" the waiting inside the repeat until-loop ends

      repeat until "byte received or (cnt - t) / (clkfreq / 1000) > ms
    
    




    here is a democode showing how it works

    CON    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
    OBJ
      Debug : "FullDuplexSerial" 'serial connection to PC       
    
      
    PUB Main | Char
    
      Debug.Start(31,30,0,115200) 'start(rxpin, txpin, mode, baudrate) :
      ''       okay '"rx" and "tx" viewed from Propeller-Chip Propeller-Chip-PIN-Tx ----> PC
    
      Debug.Str(string("START",13))
      Debug.Tx(13)
    
      repeat
        Debug.Str(string("Wait for 5 seconds to receive a byte...",13))
        Char := Debug.rxtime(5000)
    
        if Char == -1
          Debug.Str(string("nothing received until Timeout",13))
        else
          Debug.Str(string("received byte:"))
          Debug.Tx(Char)
          Debug.Tx(13)
    
        Debug.Tx(13)
        Debug.Tx(13)
        Debug.Tx(13)
      
    
    



    best regards

    Stefan

    Maybe I should add a signature like that

    "there are two ways of learning programming
    1.) a slow one: ask EVERYTHING on the forum three words short
    2.) a quick one: try something yourselve - ask the forum 10 sentences long -attaching your code - go adding debugoutput to your code while waiting for the forum-answers
  • DiverBobDiverBob Posts: 1,108
    edited 2009-02-02 22:08
    For some reason when I read the comments it just didn't 'click' as to what was happening. Thank you to everyone who answered.

    I have been a professional database and applications programmer for well over 20 years, I have studied numerous languages and am proficient in several, but I will be the first to admit that I am not perfect in each language I use. Since spin is only a hobby and I usually am using it when I'm tired in the evenings after a full day of my paid job, sometimes I just don't 'get it' right away. Once I read the posts and checked the serial code I saw what I needed then. Sometimes a little help goes a long way.

    So forgive me if I am a little slow sometimes, I think this forum has been a great place to learn no matter what your level of competance. Please have some patience with those who aren't at your level yet.

    Bob Sweeney
Sign In or Register to comment.