Timing out a serial connection?

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
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
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
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
take a look into the RxTime-method inside the FullDuplexSerial-object
best regards
Stefan
Bob
That will try for 100ms and if no response returns -1
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
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
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