'--------------------------------------------------------
' DIALER.FRM
''
' Demonstrates how to dial phone numbers with a modem.
'
' For this program to work, your telephone and
' modem must be connected to the same phone line.
'--------------------------------------------------------
Option Explicit
DefInt A-Z
' This flag is set when the user chooses Cancel.
Dim CancelFlag
Private Sub CancelButton_Click()
' CancelFlag tells the Dial procedure to exit.
CancelFlag = True
CancelButton.Enabled = False
End Sub
Private Sub Dial(Number$)
Dim DialString$, FromModem$, dummy
' AT is the Hayes compatible ATTENTION command and is required to send commands to the modem.
' DT means "Dial Tone." The Dial command uses touch tones, as opposed to pulse (DP = Dial Pulse).
' Numbers$ is the phone number being dialed.
' A semicolon tells the modem to return to command mode after dialing (important).
' A carriage return, vbCr, is required when sending commands to the modem.
DialString$ = "ATDT" + Number$ + ";" + vbCr
' Communications port settings.
' Assuming that a mouse is attached to COM1, CommPort is set to 2
MSComm1.CommPort = 2
MSComm1.Settings = "9600,N,8,1"
' Open the communications port.
On Error Resume Next
MSComm1.PortOpen = True
If Err Then
MsgBox "COM2: not available. Change the CommPort property to another port."
Exit Sub
End If
' Flush the input buffer.
MSComm1.InBufferCount = 0
' Dial the number.
MSComm1.Output = DialString$
' Wait for "OK" to come back from the modem.
Do
dummy = DoEvents()
' If there is data in the buffer, then read it.
If MSComm1.InBufferCount Then
FromModem$ = FromModem$ + MSComm1.Input
' Check for "OK".
If InStr(FromModem$, "OK") Then
' Notify the user to pick up the phone.
Beep
MsgBox "Please pick up the phone and either press Enter or click OK"
Exit Do
End If
End If
' Did the user choose Cancel?
If CancelFlag Then
CancelFlag = False
Exit Do
End If
Loop
' Disconnect the modem.
MSComm1.Output = "ATH" + vbCr
' Close the port.
MSComm1.PortOpen = False
End Sub
' Get the number to dial.
Number$ = InputBox$("Enter phone number:", Number$)
If Number$ = "" Then Exit Sub
Temp$ = Status
Status = "Dialing - " + Number$
Private Sub Form_Load()
' Setting InputLen to 0 tells MSComm to read the entire
' contents of the input buffer when the Input property
' is used.
MSComm1.InputLen = 0
Private Sub MyComm_OnComm()
With MyComm
Select case .CommEvent
Case .comEvReceive
' 接收数据
' 对数据进行处理
case .comEVSend
'发送数据
case .comEventRxParity
'对奇偶校验错误进行处理
end select
end with
end sub