Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Dialing a modem without knowing its command language

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Dialing a modem without knowing its command language

    Code:
    '
    '   This contains the routines for dialing and hanging up a modem
    '   without prior knowledge of its command language, but assuming
    '   it was properly installed under windows.
    '
    '   NOTE: This is example code only. If you use it in anything
    '   you actually care about, you should ADD error checking.
    '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
          ' Copyright ©2000 Michael Burns, All Rights Reserved.
          '
          ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
          ' You are free to use this code within your own applications,
          ' but you are expressly forbidden from selling or otherwise
          ' distributing this source code without prior written consent.
          ' This includes both posting free demo projects made from this
          ' code as well as reproducing the code in text or html format.
          ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '===========================================================================
    FUNCTION EnumerateModemWindowsInit(ComPortToMatch AS INTEGER, ModemInitString AS STRING,_
                 ModemCommandPrefix AS STRING, ModemHangUp AS STRING, ModemDialPrefix AS STRING, _
                 ModemDialSuffix AS STRING, ModemSpeakerOff AS STRING) AS STRING
       '
       '   Here we examine the Modem Registry Settings
       '   Find out which one is on ComPortToMatch
       '   and we get the Windows command strings for dialing, hanging up,
       '   turning off the speaker, and putting things back to the Windows
       '   defaults. Note that by getting the command & dial prefixes, and dial
       '   suffix, we are not restricted to Hayes compatible modems.
       '
       '    EnumerateModemWindowsInit itself is a string of the description of the modem
       '
        LOCAL ModemNum AS INTEGER
        LOCAL I AS INTEGER
        LOCAL A$
        LOCAL KEY$
        ModemInitString=""
        KEY$="HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\Modem\"
        ModemNum=0
    CheckModem:
        A$ = GetStringValue(KEY$+TRIM$(FORMAT$(ModemNum,"0000")), "DriverDesc")
        IF A$="" AND ModemNum>8 THEN
                FUNCTION = ""
                ModemInitString=""
                ModemCommandPrefix=""
                ModemHangUp=""
                ModemDialPrefix=""
                ModemDialSuffix=""
                ModemSpeakerOff=""
           EXIT FUNCTION
        END IF
        IF A$<>"" THEN
            FUNCTION = A$
            A$ = GetStringValue(KEY$+TRIM$(FORMAT$(ModemNum,"0000")), "AttachedTo")
            A$=TRIM$(A$)
            A$=RIGHT$(A$,1)
            I=VAL(A$)
            IF I = ComPortToMatch THEN
               ModemInitString = GetStringValue(KEY$+TRIM$(FORMAT$(ModemNum,"0000"))+"\Init", "2")
               ModemInitString = REMOVE$(ModemInitString, "<cr>")
               ModemCommandPrefix = GetStringValue(KEY$+TRIM$(FORMAT$(ModemNum,"0000"))+"\Init", "1")
               ModemCommandPrefix = REMOVE$(ModemCommandPrefix, "<cr>")
               ModemHangUp = GetStringValue(KEY$+TRIM$(FORMAT$(ModemNum,"0000"))+"\Hangup", "1")
               ModemHangUp = REMOVE$(ModemHangUp, "<cr>")
               ModemDialPrefix = GetStringValue(KEY$+TRIM$(FORMAT$(ModemNum,"0000"))+"\Settings", "DialPrefix")
               ModemDialSuffix = GetStringValue(KEY$+TRIM$(FORMAT$(ModemNum,"0000"))+"\Settings", "DialSuffix")
               ModemSpeakerOff  = GetStringValue(KEY$+TRIM$(FORMAT$(ModemNum,"0000"))+"\Settings", "SpeakerMode_Off")
               EXIT FUNCTION
            END IF
        END IF
        ModemNum=ModemNum+1
        GOTO CheckModem
    END FUNCTION
    '===========================================================================
    SUB DialThePhone(NumberToDial AS STRING, PortToUse AS INTEGER, hCommOut AS INTEGER)
       '
       ' Here we dial the Modem
       '
       ' Number to Dial is the full string, including leading +1 and
       ' before that, any Caller ID or Call waiting overide codes.
       '
       ' hCommOut is to pass the logical unit number back to the calling program.
       ' The port must be closed external to this subroutine. If hCommOut=0, then
       ' we know an error occured.
       '
       LOCAL A$
       LOCAL hComm AS LONG
       LOCAL OutString AS STRING
       LOCAL ModemResetString AS STRING
       LOCAL ModemCommandString AS STRING
       LOCAL ModemHangUp AS STRING
       LOCAL ModemDialPre AS STRING
       LOCAL ModemDialSuf AS STRING
       LOCAL ModemSpkOff AS STRING
       hComm = FREEFILE
       IF PortToUse<1 OR PortToUse>9 THEN
          hCommOut=0
          EXIT SUB
       END IF
       '
       '    Here we get the command language for the modem
       '
       A$ = EnumerateModemWindowsInit(PortToUse, ModemResetString, ModemCommandString, _
                 ModemHangUp, ModemDialPre, ModemDialSuf, ModemSpkOff )
       IF A$="" THEN EXIT SUB
       A$ = "Com" + TRIM$(STR$(PortToUse))
       COMM OPEN A$ AS #hComm
       IF ERRCLEAR THEN
          hCommOut=0
          EXIT SUB ' If it won't open, we punt
       END IF
       hCommOut=hComm
       OutString=ModemCommandString + ModemSpkOff
       'MSGBOX "String to silence Modem " + OutString + " on Port "+ STR$(PortToUse)
       COMM PRINT #hComm, OutString
       CALL Delay(1)
       OutString=ModemCommandString + ModemDialPre+"T" + NumberToDial +ModemDialSuf
       'MSGBOX "String to Dial Modem " + OutString + " on Port "+ STR$(PortToUse)
       COMM PRINT #hComm, OutString
       CALL Delay(1)
    END SUB
    '===========================================================================
    SUB HangUpPhone(PortToUse AS INTEGER, hCommIn AS INTEGER)
        '
        '   Here we hang up the phone and reset the modem to match
        '   its original Windows settings.
        '
        '   PortToUse is the Serial Port Number of the Modem
        '   hCommIn is the Logical Unit Number of the Open Modem Line
        '
        LOCAL A$
        LOCAL ModemResetString AS STRING
        LOCAL ModemCommandString AS STRING
        LOCAL ModemHangUp AS STRING
        LOCAL ModemDialPre AS STRING
        LOCAL ModemDialSuf AS STRING
        LOCAL ModemSpkOff AS STRING
        '
        '    If hCommIn=0, then the Dialer failed and we can just exit
        '
        IF hCommIn=0 THEN EXIT SUB
        '
        '    Here we get the command language for the modem
        '
    
        A$ = EnumerateModemWindowsInit(PortToUse, ModemResetString, ModemCommandString, _
                 ModemHangUp, ModemDialPre, ModemDialSuf, ModemSpkOff )
        IF A$="" THEN EXIT SUB
        '
        '   MModemHangUp already is a full command string, so can be
        '   sent as-is
        '
        'MSGBOX "String to Hangup Modem " + ModemHangUp + " on Port "+ STR$(PortToUse)
        COMM PRINT #hCommIn, ModemHangUp ' Here we tell it to hang up the phone
        CALL Delay(1)
        '
        '   ModemResetString already is a full command string, so can be
        '   sent as-is
        '
        'MSGBOX "String to Restore Modem " + ModemResetString + " on Port "+ STR$(PortToUse)
        COMM PRINT #hCommIn, ModemResetString ' Here we put the modem back as we found it
        CALL Delay(1)
        '
        COMM CLOSE #hCommIn
    END SUB

    ------------------
    Michael Burns http://www.revise.com

    [This message has been edited by Michael Burns (edited November 02, 2000).]
    Michael Burns

    #2
    I forgot that the code I posted uses a delay routine that I did not
    include. Here that is. It's crude, but it works. The function NOW
    that it uses is based on code from Dave Navarro's web site.

    Code:
    '---------------------------------------------------
    SUB Delay(X AS LONG)
    		'
    	        '       THIS ROUTINE DELAYS FOR X SECONDS. THE RESOLUTION
    	        '       OF THE TIME DELAY IS 1 SECONDS.
            	'
            	'       If you need timing delays of less than 1 second, use
            	'       DELAYHIBERNATE.
            	'
            	'       If you need timing delays but DO NOT need the calling
            	'       program fully responsive to events, as for example, just
            	'       killing time, then use DELAYEFFICIENTLY.
            	'
            	'       written by Michael Burns
            	'
            	'
            	DIM DelayEnd  AS DOUBLE
            	DIM DRemainder AS SINGLE
            	DIM Offset AS DOUBLE
            	Offset=0#
            	IF X <= 0 THEN EXIT SUB
            	Offset=X/(3600*24)
            	DelayEnd = Offset + NOW
            	DO WHILE DelayEnd > NOW
                		DIALOG DOEVENTS
            	LOOP
    END SUB  
    '---------------------------------------------------
    FUNCTION Now() AS DOUBLE
    '
    ' Return the current date and time as a DOUBLE (compatible with VB)
    ' The DOUBLE is the number of days since January 1, 1900. 1 = 1 day
    '
    ' Based on code from Dave Navarro.
    '
    
        	LOCAL st     AS SYSTEMTIME
        	LOCAL vbDate AS DOUBLE
    
        	GetSystemTime st
        	CALL SystemTimeToVariantTime (st, vbDate)
        	FUNCTION = vbDate
    END FUNCTION

    ------------------
    Michael Burns http://www.revise.com

    [This message has been edited by Michael Burns (edited November 02, 2000).]
    Michael Burns

    Comment

    Working...
    X
    😀
    🥰
    🤢
    😎
    😡
    👍
    👎