Serial Port Timeout Detection

Just starting out? Need help? Post your questions and find answers here.
User avatar
kh1234567890
New User
New User
Posts: 3
Joined: Sat Jan 15, 2005 4:44 pm
Location: Inside your head

Serial Port Timeout Detection

Post by kh1234567890 »

How do you detect that a timeout had occurred using the in-built PB 4.32 serial port functions ?

There are no obvious clues in the documentation. :(
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Serial Port Timeout Detection

Post by Rook Zimbabwe »

You have to keep checking to see if it is active.

Interesting username?
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
John Puccio
User
User
Posts: 26
Joined: Fri Jun 12, 2009 6:56 am
Location: My Keyboard

Re: Serial Port Timeout Detection

Post by John Puccio »

Hi KH 1 thru 0,

Serial ports don't exactly time out, they are closed just like a file. Basically the host program says "if I do not see activity for X amount of time then close the port" that's how you get a timeout. What you need to do is look at the DCD line (Data Carrier Detect) if it is low then you have no carrier (no connection) and thus your timeout.

Hope that helps.

JP
User avatar
kh1234567890
New User
New User
Posts: 3
Joined: Sat Jan 15, 2005 4:44 pm
Location: Inside your head

Re: Serial Port Timeout Detection

Post by kh1234567890 »

Thanks for the replies.

So when the serial port 'times out' after an interval set by SerialPortTimeouts it just closes and disappears ?

I was hoping for something more useful - I suppose it is back to the kludge of using the API timer to periodically check if any characters have appeared in the serial buffer.

In this application I have a simple three wire serial connection with no handshaking lines, connected to an unpredictable data acquisition front end sending data whenever it feels like it.

It is all coming back, why I gave up on PB about four years ago :(

kh1234567890 (because people kept using kh1, kh2 etc.)
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Re: Serial Port Timeout Detection

Post by dhouston »

Create a separate thread for your serial port receive procedure.

Code: Select all

If IsSerialPort(ComID):ComThread=CreateThread(@ComEventRcv(),0):EndIf

Code: Select all

Procedure ComEventRcv(*nada)          ;serial input thread
  Protected ComRcv.s,char.s=" ",buffer.b,n
  Repeat
    If IsSerialPort(ComID)
      While AvailableSerialPortInput(ComID)   
        n=ReadSerialPortData(ComID,@buffer,1)
        Select buffer ;Asc(char)
          Case 10,13,32 To 126:ComRcv+Chr(buffer)
        EndSelect
        If Right(ComRcv,2)=#CRLF$
          If CmdMode
            cmd=Left(ComRcv,Len(ComRcv)-2)
          Else
            UpdateIO(Left(ComRcv,Len(ComRcv)-2))
          EndIf  
          ComRcv=""
        EndIf
      Wend 
    EndIf  
  ForEver
EndProcedure
User avatar
kh1234567890
New User
New User
Posts: 3
Joined: Sat Jan 15, 2005 4:44 pm
Location: Inside your head

Re: Serial Port Timeout Detection

Post by kh1234567890 »

Neat !

Thanks, I'll try that.

[Edit]
Works fine, just needed a short delay in the Repeat loop to stop the thread hoarding the processor.
Thanks again.

kh
User avatar
John Puccio
User
User
Posts: 26
Joined: Fri Jun 12, 2009 6:56 am
Location: My Keyboard

Re: Serial Port Timeout Detection

Post by John Puccio »

kh1234567890 wrote:Thanks for the replies.

So when the serial port 'times out' after an interval set by SerialPortTimeouts it just closes and disappears ?

I was hoping for something more useful - I suppose it is back to the kludge of using the API timer to periodically check if any characters have appeared in the serial buffer.

In this application I have a simple three wire serial connection with no handshaking lines, connected to an unpredictable data acquisition front end sending data whenever it feels like it.

It is all coming back, why I gave up on PB about four years ago :(

kh1234567890 (because people kept using kh1, kh2 etc.)
EDIT:

KH

I'm glad you found a workable solution. Just to clarify, It's not a PB issue. It's a com port issue. Looking over my old docs, in a 3 wire no handshake situation there is no carier signal so DCD doesn't even apply.

JP
Post Reply