Private Sub mnuDial_Click() On Local Error Resume Next Static Num As String
Num = "1-206-936-6735" ' This is the MSDN phone number
' Get a number from the user. Num = InputBox$("Enter Phone Number:", "Dial Number", Num) If Num = "" Then Exit Sub
' Open the port if it isn't already open. If Not MSComm1.PortOpen Then mnuOpen_Click If Err Then Exit Sub End If
' Enable hang up button and menu item mnuHangUp.Enabled = True tbrToolBar.Buttons("HangUpPhone").Enabled = True
' Dial the number. MSComm1.Output = "ATDT" & Num & vbCrLf
' Start the port timer StartTiming End Sub
' The OnComm event is used for trapping communications events and errors. Private Static Sub MSComm1_OnComm() Dim EVMsg$ Dim ERMsg$
' Branch according to the CommEvent property. Select Case MSComm1.CommEvent ' Event messages. Case comEvReceive Dim Buffer As Variant Buffer = MSComm1.Input Debug.Print "Receive - " & StrConv(Buffer, vbUnicode) ShowData txtTerm, (StrConv(Buffer, vbUnicode)) Case comEvSend Case comEvCTS EVMsg$ = "Change in CTS Detected" Case comEvDSR EVMsg$ = "Change in DSR Detected" Case comEvCD EVMsg$ = "Change in CD Detected" Case comEvRing EVMsg$ = "The Phone is Ringing" Case comEvEOF EVMsg$ = "End of File Detected" ' Error messages. Case comBreak ERMsg$ = "Break Received" Case comCDTO ERMsg$ = "Carrier Detect Timeout" Case comCTSTO ERMsg$ = "CTS Timeout" Case comDCB ERMsg$ = "Error retrieving DCB" Case comDSRTO ERMsg$ = "DSR Timeout" Case comFrame ERMsg$ = "Framing Error" Case comOverrun ERMsg$ = "Overrun Error" Case comRxOver ERMsg$ = "Receive Buffer Overflow" Case comRxParity ERMsg$ = "Parity Error" Case comTxFull ERMsg$ = "Transmit Buffer Full" Case Else ERMsg$ = "Unknown error or event" End Select
If Len(EVMsg$) Then ' Display event messages in the status bar. sbrStatus.Panels("Status").Text = "Status: " & EVMsg$
' Enable timer so that the message in the status bar ' is cleared after 2 seconds Timer2.Enabled = True
ElseIf Len(ERMsg$) Then ' Display event messages in the status bar. sbrStatus.Panels("Status").Text = "Status: " & ERMsg$
' Display error messages in an alert message box. Beep Ret = MsgBox(ERMsg$, 1, "Click Cancel to quit, OK to ignore.")
' If the user clicks Cancel (2)... If Ret = 2 Then MSComm1.PortOpen = False ' Close the port and quit. End If
' Enable timer so that the message in the status bar ' is cleared after 2 seconds Timer2.Enabled = True End If End Sub
On Local Error Resume Next
Static Num As String
Num = "1-206-936-6735" ' This is the MSDN phone number
' Get a number from the user.
Num = InputBox$("Enter Phone Number:", "Dial Number", Num)
If Num = "" Then Exit Sub
' Open the port if it isn't already open.
If Not MSComm1.PortOpen Then
mnuOpen_Click
If Err Then Exit Sub
End If
' Enable hang up button and menu item
mnuHangUp.Enabled = True
tbrToolBar.Buttons("HangUpPhone").Enabled = True
' Dial the number.
MSComm1.Output = "ATDT" & Num & vbCrLf
' Start the port timer
StartTiming
End Sub
Private Static Sub MSComm1_OnComm()
Dim EVMsg$
Dim ERMsg$
' Branch according to the CommEvent property.
Select Case MSComm1.CommEvent
' Event messages.
Case comEvReceive
Dim Buffer As Variant
Buffer = MSComm1.Input
Debug.Print "Receive - " & StrConv(Buffer, vbUnicode)
ShowData txtTerm, (StrConv(Buffer, vbUnicode))
Case comEvSend
Case comEvCTS
EVMsg$ = "Change in CTS Detected"
Case comEvDSR
EVMsg$ = "Change in DSR Detected"
Case comEvCD
EVMsg$ = "Change in CD Detected"
Case comEvRing
EVMsg$ = "The Phone is Ringing"
Case comEvEOF
EVMsg$ = "End of File Detected" ' Error messages.
Case comBreak
ERMsg$ = "Break Received"
Case comCDTO
ERMsg$ = "Carrier Detect Timeout"
Case comCTSTO
ERMsg$ = "CTS Timeout"
Case comDCB
ERMsg$ = "Error retrieving DCB"
Case comDSRTO
ERMsg$ = "DSR Timeout"
Case comFrame
ERMsg$ = "Framing Error"
Case comOverrun
ERMsg$ = "Overrun Error"
Case comRxOver
ERMsg$ = "Receive Buffer Overflow"
Case comRxParity
ERMsg$ = "Parity Error"
Case comTxFull
ERMsg$ = "Transmit Buffer Full"
Case Else
ERMsg$ = "Unknown error or event"
End Select
If Len(EVMsg$) Then
' Display event messages in the status bar.
sbrStatus.Panels("Status").Text = "Status: " & EVMsg$
' Enable timer so that the message in the status bar
' is cleared after 2 seconds
Timer2.Enabled = True
ElseIf Len(ERMsg$) Then
' Display event messages in the status bar.
sbrStatus.Panels("Status").Text = "Status: " & ERMsg$
' Display error messages in an alert message box.
Beep
Ret = MsgBox(ERMsg$, 1, "Click Cancel to quit, OK to ignore.")
' If the user clicks Cancel (2)...
If Ret = 2 Then
MSComm1.PortOpen = False ' Close the port and quit.
End If
' Enable timer so that the message in the status bar
' is cleared after 2 seconds
Timer2.Enabled = True
End If
End Sub
设置波特率、串行口
用Input属性接收数据