'===========================================================================
' Subject: ALARM ON CONNECTION                Date: 10-18-92 (10:18)       
'  Author: James Vahn                         Code: QB, QBasic, PDS        
'  Origin: FidoNet QUIK_BAS Echo            Packet: MODEM.ABC
'===========================================================================
'>  Does anyone have code that allows you to dial a number through
'>  the modem, and allow the user to pick up the phone when it
'>  connects (and things to watch for, like how do you know when
'>  it's safe to pick up the phone)?


' This routine sends an alarm when a connection is made.


'modem.bas  is an ASCII terminal to demo an autodialer.  James Vahn
DECLARE SUB Keyscan ()
DECLARE SUB Delay (td!)
DECLARE SUB Dial (num$)

' Put all modem response into a 10k buffer declared global.
COMMON SHARED ModemIn$

ON ERROR GOTO Handler
ON COM(2) GOSUB GetBuf
COM(2) ON

CALL Dial ("555-1212")

DO
 CALL Keyscan      ' You're online now. Stay in this loop forever.
LOOP

Handler:
RESUME NEXT

GetBuf:
InStr$ = INPUT$(LOC(1), #1)

   ' swap a backspace char for a left cursor.
   DO
      BackSpace = INSTR(InStr$, CHR$(8))
      IF BackSpace THEN
         MID$(InStr$, BackSpace) = CHR$(29)
      END IF
   LOOP WHILE BackSpace

   ' eliminate line feeds.
   DO
      LineFeed = INSTR(InStr$, CHR$(10))
      IF LineFeed THEN
         InStr$ = LEFT$(InStr$, LineFeed - 1) + MID$(InStr$, LineFeed + 1)
      END IF
   LOOP WHILE LineFeed

   ModemIn$ = RIGHT$(ModemIn$ + InStr$, 10240)
   PRINT (InStr$);            'print modem buffer to screen.
RETURN

SUB Delay (td!)
  TimeDelay! = (TIMER + td!) mod 86400
  WHILE TimeDelay! > TIMER: WEND
END SUB

SUB Dial (num$)

OPEN "COM2:2400,N,8,1" FOR RANDOM AS #1

CLS
LOCATE 25, 40: PRINT "ALT-X to exit.."
LOCATE 1, 1, 1
  PRINT #1, "ATZ"
  CALL Delay(1.25)
  PRINT #1, "ATS7=45 S0=0 V1 M0"     ' modem initialization string
  CALL Delay(1.25)

DO
  CALL Delay(1)
  PRINT "Dialing ....."
  PRINT #1, "atdt" + Num$ + CHR$(13)

 TimeDelay! = TIMER + 40

   DO UNTIL TIMER > TimeDelay!
       CALL Keyscan
       test = INSTR(RIGHT$(ModemIn$, 20), "CONNECT")
        IF test THEN result = -1: EXIT DO
       test = INSTR(RIGHT$(ModemIn$, 5), "BUSY")
        IF test THEN result = 0: EXIT DO
       test = INSTR(RIGHT$(ModemIn$, 12), "NO DIALTONE")
        IF test THEN result = 0: CALL Delay(2): EXIT DO
       test = INSTR(RIGHT$(ModemIn$, 11), "NO CARRIER")
        IF test THEN result = 0: CALL Delay(2): EXIT DO

   LOOP

LOOP UNTIL result

FOR t = 1 TO 5            ' It answered! ring the alarm!
  SOUND 750, 2
  SOUND 550, 2
  SOUND 650, 2
  IF INKEY$ <> "" THEN EXIT FOR
NEXT

END SUB

SUB Keyscan
' This would be a good place to check for PgDn/PgUp and shell to an
' external transfer protocol like Zmodem.

a$ = INKEY$
    IF a$ = CHR$(0) + CHR$(45) THEN CLOSE : END '  ALT-X to exit.
    PRINT #1, a$;    ' send keypress to modem
END SUB
