Ring Detect

Anyone help me detect a phone ring...

I have my modem connected with my phone and I want to my program to know when somebody is calling (the phone is ringing). Any help appreciated.
LVL 2
polygonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
apjConnect With a Mentor Commented:
polygon,

   You can do it using TAPI32.dll.

   This example only recognize if the phone is ringing, you can't answer the call.

   For more information about TAPI32.dll
take a look at microsoft site.

   EXAMPLE:

In a module, write:

'=======================================

Option Explicit

Global Device        As Long
Global HandleToCall  As Long

' Define Simple Telephony Public Constants.
Public Const LINEMEDIAMODE_UNKNOWN = &H2&
Public Const LINEMEDIAMODE_INTERACTIVEVOICE = &H4&
Public Const LINEMEDIAMODE_AUTOMATEDVOICE = &H8&
Public Const LINEMEDIAMODE_DATAMODEM = &H10&
Public Const LINEMEDIAMODE_G3FAX = &H20&
Public Const LINEMEDIAMODE_TDD = &H40&
Public Const LINEMEDIAMODE_G4FAX = &H80&
Public Const LINEMEDIAMODE_DIGITALDATA = &H100&
Public Const LINEMEDIAMODE_TELETEX = &H200&
Public Const LINEMEDIAMODE_VIDEOTEX = &H400&
Public Const LINEMEDIAMODE_TELEX = &H800&
Public Const LINEMEDIAMODE_MIXED = &H1000&
Public Const LINEMEDIAMODE_ADSI = &H2000&

Public Const LINECALLPRIVILEGE_NONE = &H1&
Public Const LINECALLPRIVILEGE_MONITOR = &H2&
Public Const LINECALLPRIVILEGE_OWNER = &H4&

Public Const LINECALLPARAMS_FIXEDSIZE = 112

Global Const LINECALLSTATE_IDLE = &H1&
Global Const LINECALLSTATE_OFFERING = &H2&
Global Const LINECALLSTATE_ACCEPTED = &H4&
Global Const LINECALLSTATE_DIALTONE = &H8&
Global Const LINECALLSTATE_DIALING = &H10&
Global Const LINECALLSTATE_RINGBACK = &H20&
Global Const LINECALLSTATE_BUSY = &H40&
Global Const LINECALLSTATE_SPECIALINFO = &H80&
Global Const LINECALLSTATE_CONNECTED = &H100&
Global Const LINECALLSTATE_PROCEEDING = &H200&
Global Const LINECALLSTATE_ONHOLD = &H400&
Global Const LINECALLSTATE_CONFERENCED = &H800&
Global Const LINECALLSTATE_ONHOLDPENDCONF = &H1000&
Global Const LINECALLSTATE_ONHOLDPENDTRANSFER = &H2000&
Global Const LINECALLSTATE_DISCONNECTED = &H4000&
Global Const LINECALLSTATE_UNKNOWN = &H8000&

' Messages for Phones and Lines
Global Const LINE_ADDRESSSTATE = 0&
Global Const LINE_CALLINFO = 1&
Global Const LINE_CALLSTATE = 2&
Global Const LINE_CLOSE = 3&
Global Const LINE_DEVSPECIFIC = 4&
Global Const LINE_DEVSPECIFICFEATURE = 5&
Global Const LINE_GATHERDIGITS = 6&
Global Const LINE_GENERATE = 7&
Global Const LINE_LINEDEVSTATE = 8&
Global Const LINE_MONITORDIGITS = 9&
Global Const LINE_MONITORMEDIA = 10&
Global Const LINE_MONITORTONE = 11&
Global Const LINE_REPLY = 12&
Global Const LINE_REQUEST = 13&
Global Const PHONE_BUTTON = 14&
Global Const PHONE_CLOSE = 15&
Global Const PHONE_DEVSPECIFIC = 16&
Global Const PHONE_REPLY = 17&
Global Const PHONE_STATE = 18&

' Tapi function prototypes
Declare Function lineInitialize Lib "TAPI32.DLL" (lphLineApp As Long, ByVal hInstance As Long, ByVal lpfnCallback As Long, ByVal lpszAppName As String, lpdwNumDevs As Long) As Long
Declare Function lineOpen Lib "TAPI32.DLL" (ByVal hLineApp As Long, ByVal dwDeviceID As Long, lphLine As Long, ByVal dwAPIVersion As Long, ByVal dwExtVersion As Long, ByVal dwCallbackInstance As Long, ByVal dwPrivileges As Long, ByVal dwMediaModes As Long, lpCallParams As Any) As Long
Declare Function lineGetDevCaps Lib "TAPI32.DLL" (ByVal hLineApp As Long, ByVal dwDeviceID As Long, ByVal dwAPIVersion As Long, ByVal dwExtVersion As Long, lpLineDevCaps As LINEDEVCAPS) As Long
Declare Function lineDeallocateCall Lib "TAPI32.DLL" (ByVal hCall As Long) As Long
Declare Function lineClose Lib "TAPI32.DLL" (ByVal hLine As Long) As Long

' added by MS (rob young) to get proper instance handle
Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long


  Type LINEDIALPARAMS
    dwDialPause As Long
    dwDialSpeed As Long
    dwDigitDuration As Long
    dwWaitForDialtone As Long
End Type
Public Const LINEDIALPARAMS_FIXEDSIZE = 16


  Type LINECALLPARAMS
    dwTotalSize As Long

    dwBearerMode As Long
    dwMinRate As Long
    dwMaxRate As Long
    dwMediaMode As Long

    dwCallParamFlags As Long
    dwAddressMode As Long
    dwAddressID As Long

    DialParams As LINEDIALPARAMS

    dwOrigAddressSize As Long
    dwOrigAddressOffset As Long

    dwDisplayableAddressSize As Long
    dwDisplayableAddressOffset As Long

    dwCalledPartySize As Long
    dwCalledPartyOffset As Long

    dwCommentSize As Long
    dwCommentOffset As Long

    dwUserUserInfoSize As Long
    dwUserUserInfoOffset As Long

    dwHighLevelCompSize As Long
    dwHighLevelCompOffset As Long

    dwLowLevelCompSize As Long
    dwLowLevelCompOffset As Long

    dwDevSpecificSize As Long
    dwDevSpecificOffset As Long
    '
    mem As String * 2048 ' added by mca
End Type


Type LINECALLPARAMS_STR
    mem As String * LINECALLPARAMS_FIXEDSIZE
End Type

Type LINEDEVCAPS
    dwTotalSize As Long
    dwNeededSize As Long
    dwUsedSize As Long

    dwProviderInfoSize As Long
    dwProviderInfoOffset As Long

    dwSwitchInfoSize As Long
    dwSwitchInfoOffset As Long

    dwPermanentLineID As Long
    dwLineNameSize As Long
    dwLineNameOffset As Long
    dwStringFormat As Long

    dwAddressModes As Long
    dwNumAddresses As Long
    dwBearerModes As Long
    dwMaxRate As Long
    dwMediaModes As Long

    dwGenerateToneModes As Long
    dwGenerateToneMaxNumFreq As Long
    dwGenerateDigitModes As Long
    dwMonitorToneMaxNumFreq As Long
    dwMonitorToneMaxNumEntries As Long
    dwMonitorDigitModes As Long
    dwGatherDigitsMinTimeout As Long
    dwGatherDigitsMaxTimeout As Long

    dwMedCtlDigitMaxListSize As Long
    dwMedCtlMediaMaxListSize As Long
    dwMedCtlToneMaxListSize As Long
    dwMedCtlCallStateMaxListSize As Long

    dwDevCapFlags As Long
    dwMaxNumActiveCalls As Long
    dwAnswerMode As Long
    dwRingModes As Long
    dwLineStates As Long

    dwUUIAcceptSize As Long
    dwUUIAnswerSize As Long
    dwUUIMakeCallSize As Long
    dwUUIDropSize As Long
    dwUUISendUserUserInfoSize As Long
    dwUUICallInfoSize As Long

    MinDialParams As LINEDIALPARAMS
    MaxDialParams As LINEDIALPARAMS
    DefaultDialParams As LINEDIALPARAMS

    dwNumTerminals As Long
    dwTerminalCapsSize As Long
    dwTerminalCapsOffset As Long
    dwTerminalTextEntrySize As Long
    dwTerminalTextSize As Long
    dwTerminalTextOffset As Long

    dwDevSpecificSize As Long
    dwDevSpecificOffset As Long
   
    mem As String * 2048 ' ADDED BY MCA
End Type
Global Const LINEDEVCAPS_FIXEDSIZE = 236

Type LINEDEVCAPS_STR
    mem As String * LINEDEVCAPS_FIXEDSIZE
End Type


Public Sub LINECALLBACK(ByVal hDevice As Long, _
                        ByVal dwMessage As Long, _
                        ByVal dwInstance As Long, _
                        ByVal dwParam1 As Long, _
                        ByVal dwParam2 As Long, _
                        ByVal dwParam3 As Long)
   
       
    ' handle callback msgs from TAPI32.DLL
   
    Dim strTemp As String
   
    ' build string for display
    strTemp = "hDevice=" & CStr(hDevice) & ", "
    strTemp = strTemp & "dwMessage=" & CStr(dwMessage) & ", "
    strTemp = strTemp & "dwInstance=" & CStr(dwInstance) & ", "
    strTemp = strTemp & "dwParam1=" & CStr(dwParam1) & ", "
    strTemp = strTemp & "dwParam2=" & CStr(dwParam2) & ", "
    strTemp = strTemp & "dwParam3=" & CStr(dwParam3)
   
    Select Case dwMessage
   
        Case LINE_CALLSTATE
           
            Select Case dwParam1
                Case LINECALLSTATE_OFFERING         ' get call handle
                   
                    HandleToCall = hDevice
                    MsgBox "The phone is RING !!"
                   
                Case LINECALLSTATE_IDLE             ' they hung up on me!
               
                    'This can  delay 20 seconds or more.
                               
                    If lineDeallocateCall(HandleToCall) = 0 Then
                       MsgBox "LineDeallocate Successful"
                    Else
                       MsgBox "ERROR in LineDeallocate Successful"
                    End If
           
            End Select
           
        Case LINE_REPLY

        Case Else

    End Select
   
    Exit Sub
   
End Sub

'======================================

In the form write:

Option Explicit

Dim Device As Long

Dim udtLineCall As LINECALLPARAMS
Dim lines       As Long
Dim hInst       As Long
Dim lineApp     As Long
Dim lphLine     As Long
Dim lphCall     As Long
Dim adrCallBack As Long


Public Function PtrToLong(ByVal lngFnPtr As Long) As Long

    ' convert address into a long
    PtrToLong = lngFnPtr

End Function

Sub OpenLine()
   
    Dim retcode As Long

    adrCallBack = PtrToLong(AddressOf LINECALLBACK)
   
    hInst = GetModuleHandle(vbNullString)
   
    udtLineCall.dwTotalSize = LINECALLPARAMS_FIXEDSIZE + 2048
   
    retcode = lineOpen(lineApp, Device, lphLine, 65540, &H0, AddressOf LINECALLBACK, _
                       LINECALLPRIVILEGE_OWNER + LINECALLPRIVILEGE_MONITOR, _
                       LINEMEDIAMODE_INTERACTIVEVOICE, udtLineCall)
                 
    If retcode <> 0 Then
        MsgBox "ERROR !!"
    End If

End Sub

Public Function DeviceName(pDevice As Long) As String

Dim LineDev As LINEDEVCAPS
Dim retcode As Long
     
    LineDev.dwTotalSize = LINEDEVCAPS_FIXEDSIZE + 2048
   
    retcode = lineGetDevCaps(lineApp, pDevice, 65540, &H0, LineDev)
   
    If retcode > 0 Then
        MsgBox "ERROR !!"
    End If
   
    DeviceName = Mid(LineDev.mem, _
                    (LineDev.dwLineNameOffset - LINEDEVCAPS_FIXEDSIZE) + 1, _
                     LineDev.dwLineNameSize - 1)
   

End Function

Public Function DeviceNumber() As Long

Dim retcode As Long
   
    hInst = 0
    lineApp = 0
    lines = 0

    adrCallBack = PtrToLong(AddressOf LINECALLBACK)

    hInst = GetModuleHandle(vbNullString)
   
    retcode = lineInitialize(lineApp, hInst, adrCallBack, "TAPI", lines)
     
    If retcode <> 0 Then
        MsgBox "ERROR !!"
    End If
   
    DeviceNumber = lines
   
End Function


Private Sub Combo1_Click()

    Device = Combo1.ListIndex
    OpenLine

End Sub

Private Sub Form_Load()

Dim Devices As Long
Dim i       As Long

    Devices = DeviceNumber

    For i = 0 To Devices - 1
   
        Combo1.AddItem DeviceName(i)
   
    Next i

End Sub

Private Sub Form_Unload(Cancel As Integer)

Dim retcode  As Long

    retcode = lineClose(lphLine)
    If retcode <> 0 Then
        MsgBox "ERROR !!!"
    End If

End Sub

'=====================================

I hope have help.
APJ

0
 
mark2150Commented:
Your modem should send "RING" when it the phone rings. You can also configure it to fire a RI (Ring Interrupt) to "wake up" a task.

M
0
 
richardjonesCommented:
Can use the MSCOMM control, set it's com port to the port of your modem, and then you can use it to detect phone rings and such like

RJ
0
 
polygonAuthor Commented:
I do as you say, but I do not receive the OnComm event when the phone is ringing...
0
All Courses

From novice to tech pro — start learning today.