/*
      Try to located serial port FST Timer is connected to.

      Sample Clipper Code

      Copyright 1996 Atlantis Computer Services
*/
function Find_FST_Timer()


#include "beds.ch"
#include "colors.ch"

local nBuffer   := FST_BUFFER
local nBaud     := FST_BAUD_RATE
local cParity   := FST_PARITY
local nData     := FST_DATA_BITS
local nStop     := FST_STOP_BITS

local nPort, cString, lFound := .F.
local lOpened, lInit, lClosed, nChars

// Disable Com Messages
ComMsgs( .F. )

nPort := 0
do while .T. 				// loop through com1 to com4
   nPort++
   if nPort = 5
      exit
   endif

   // Open Comm Port
   lOpened := COM_OPEN( nPort, nBuffer )

   if lOpened
      // Successfully Opened, Initialize Port
      lInit := COM_INIT( nPort, nBaud, cParity, nData, nStop )

      if lInit

         if !ChkStatus( nPort, 1 )                    // Any Timer
            if !ChkStatus( nPort, 17 )                // Any Timer
               if !ChkStatus( nPort, 25 )             // 6, 12, 16
                  if !ChkStatus( nPort, 33 )          // 8, 12, 16
                     if !ChkStatus( nPort, 37 )       // 6, 8, 12, 16
                        if !ChkStatus( nPort, 49 )    // 8 or 16
                           Sound( "COMERR" )
                           QwkMsg( "No FST Timers were located on Serial Port #" ;
                            + ltrim( str( nPort ) ) + ", press any key...", 30 )
                        endif
                     endif
                  endif
               endif
            endif
         endif
      endif

      lClosed := COM_CLOSE( nPort )
   endif
enddo

// Turn Com Messages Back On
ComMsgs( .T. )

return NIL


/*
	Check Status for Station and report
*/
static function ChkStatus( nPort, nStation )

local nStatus
local cMsg := ""
local lRtnValue := .F.

@ maxrow(), 00 say padr( "Checking Status for Station " + alltrim( str( nStation ) ) + " on Serial Port #" + alltrim( str( nPort ) ), maxcol() ) color C_TITLE

nStatus := FST_Status( nPort, nStation )

if nStatus == BED_AVAIL
   cMsg := "Station " + alltrim( str( nStation ) ) + " is Ready"

elseif nStatus == BED_RUNNING
   cMsg := "Station " + alltrim( str( nStation ) ) + " is Running"

elseif nStatus == BED_CLEAN_ME
   cMsg := "Station " + alltrim( str( nStation ) ) + " needs Cleaning"

elseif nStatus == BED_COUNTDOWN
   cMsg := "Station " + alltrim( str( nStation ) ) + " is in Undress Period"

elseif nStatus == BED_PAUSED
   cMsg := "Station " + alltrim( str( nStation ) ) + " is Paused"

else
  // Error Msg handled at lower level function
endif

if !empty( cMsg )
   cMsg := "FST Timer found on Serial Port #" + alltrim( str( nPort ) ) + ";" + cMsg
   Sound( "DONE" )
   alert( cMsg )
   lRtnValue := .T.
endif

return lRtnValue