dim strReceive as StringPrivate Sub Form_Load()
With Me.MSComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.OutBufferCount = 0
.InBufferCount = 0
.RThreshold = 1
.PortOpen = True
End With
End SubPrivate Sub Command1_Click()
Me.MSComm1.Output = "AT+CMGS = +8613912121212" & Chr(13) & "Please don't reply this SMS." & Chr(26)
End SubPrivate Sub MSComm1_OnComm()
Select Case Me.MSComm1.CommEvent
Case comEvReceive
Me.MSComm1.RThreshold = 0
Me.MSComm1.InputLen = 0
Me.MSComm1.InputMode = comInputModeText
strReceive = strReceive & Me.comSMS.Input
If InStr(strReceive, "OK" & vbCrLf) <> 0 Or InStr(strReceive, "ERROR" & vbCrLf) <> 0 Then
MsgBox strReceive
strReceive = ""
End If
Me.MSComm1.RThreshold = 1
Case comEvRing
MsgBox "Ring"
Me.MSComm1.Output = "ATH" & Chr(13)
End Select
End SubPrivate Sub Form_Unload(Cancel As Integer)
Me.MSComm1.PortOpen = False
End Sub
With Me.MSComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.OutBufferCount = 0
.InBufferCount = 0
.RThreshold = 1
.PortOpen = True
End With
End SubPrivate Sub Command1_Click()
Me.MSComm1.Output = "AT+CMGS = +8613912121212" & Chr(13) & "Please don't reply this SMS." & Chr(26)
End SubPrivate Sub MSComm1_OnComm()
Select Case Me.MSComm1.CommEvent
Case comEvReceive
Me.MSComm1.RThreshold = 0
Me.MSComm1.InputLen = 0
Me.MSComm1.InputMode = comInputModeText
strReceive = strReceive & Me.comSMS.Input
If InStr(strReceive, "OK" & vbCrLf) <> 0 Or InStr(strReceive, "ERROR" & vbCrLf) <> 0 Then
MsgBox strReceive
strReceive = ""
End If
Me.MSComm1.RThreshold = 1
Case comEvRing
MsgBox "Ring"
Me.MSComm1.Output = "ATH" & Chr(13)
End Select
End SubPrivate Sub Form_Unload(Cancel As Integer)
Me.MSComm1.PortOpen = False
End Sub
见MSDN解释:
当接收字符后,若 Rthreshold 属性设置为 0(缺省值)则不产生 OnComm 事件。
例如,设置 Rthreshold 为 1,接收缓冲区收到每一个字符都会使 MSComm 控件产生 OnComm 事件。
所以你代码中需注释掉此行:
Private Sub MSComm1_OnComm()
Select Case Me.MSComm1.CommEvent
Case comEvReceive
'Me.MSComm1.RThreshold = 0 '此行注释掉
Me.MSComm1.InputLen = 0
Me.MSComm1.InputMode = comInputModeText
strReceive = strReceive & Me.comSMS.Input
If InStr(strReceive, "OK" & vbCrLf) < > 0 Or InStr(strReceive, "ERROR" & vbCrLf) < > 0 Then
MsgBox strReceive
strReceive = ""
End If
Me.MSComm1.RThreshold = 1
Case comEvRing
MsgBox "Ring"
Me.MSComm1.Output = "ATH" & Chr(13)
End Select
End Sub
中comSMS控件名拼错
应为MSComm1
strReceive = strReceive & Me.MSComm1.Input
OMCOMM事件代码修改如下:
Private Sub MSComm1_OnComm()
Select Case Me.MSComm1.CommEvent
Case comEvReceive
'Me.MSComm1.RThreshold = 0
Me.MSComm1.InputLen = 0
Me.MSComm1.InputMode = comInputModeText
strReceive = strReceive & Me.MSComm1.Input
Text1 = strReceive '新增句用于显示接收的字符
If InStr(strReceive, "OK" & vbCrLf) <> 0 Or InStr(strReceive, "ERROR" & vbCrLf) <> 0 Then
MsgBox strReceive
strReceive = ""
End If
Me.MSComm1.RThreshold = 1
Case comEvRing
MsgBox "Ring"
Me.MSComm1.Output = "ATH" & Chr(13)
End Select
End Sub
建议IF THEN
END IF
判断结构及后几句中MsgBox函数不要用,回中止接收引起错误。
Select Case MSComm1.CommEvent
Case comEvReceive
'在第一次触发事件时启动TimerRead
If TimerRead.Enabled = False Then
MSComm1.RThreshold = 0
TimerRead.Enabled = True
bfcount = MSComm1.InBufferCount
End If
Case Else
WriteLog "MSComm1.CommEvent=" & MSComm1.CommEvent
End Select
End Sub
Private Sub TimerRead_Timer()
If MSComm1.InBufferCount = bfcount Then
InBuffer = MSComm1.Input
MSComm1.RThreshold = 1
WriteLog InBuffer
TimerRead.Enabled = False
End If
bfcount = MSComm1.InBufferCount
End Sub
Dim strReceive As StringPrivate Sub Command1_Click()
Me.MSComm1.Output = "AT+CMGS = +8613912345678" & Chr(13) & "Please don't reply this SMS." & Chr(26)
End SubPrivate Sub Form_Load()
With Me.MSComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.PortOpen = True
.OutBufferCount = 0
.InBufferCount = 0
.RThreshold = 1
End With
End SubPrivate Sub MSComm1_OnComm()
Select Case Me.MSComm1.CommEvent
Case comEvReceive
Me.MSComm1.InputLen = 0
strReceive = strReceive & Me.MSComm1.Input
If InStr(strReceive, "OK" & vbCrLf) <> 0 Or InStr(strReceive, "ERROR" & vbCrLf) <> 0 Then
Debug.Print strReceive
strReceive = ""
End If
Case comEvRing
Debug.Print "Ring"
Me.MSComm1.Output = "ATH" & Chr(13)
End Select
End Sub
Select Case Me.MSComm1.CommEvent
Case comEvReceive
Me.MSComm1.InputLen = 0
strReceive = strReceive & Me.MSComm1.Input
'If InStr(strReceive, "OK" & vbCrLf) < > 0 Or InStr(strReceive, "ERROR" & vbCrLf) < > 0 Then
Debug.Print strReceive
strReceive = ""
'End If
Case comEvRing
Debug.Print "Ring"
Me.MSComm1.Output = "ATH" & Chr(13)
End Select
End Sub
用以上代码,检查Debug.Print strReceive 反馈内容.