在VB中使用MSComm控件发短信但不能触发OnComm下的comEvReceive事件
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 Sub
Private Sub Command1_Click()
Me.MSComm1.Output = "AT+CMGS = +8613912121212" & Chr(13) & "Please don't reply this SMS." & Chr(26)
End Sub
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
Private Sub Form_Unload(Cancel As Integer)
Me.MSComm1.PortOpen = False
End Sub --------------------编程问答-------------------- 打电话给Modem,可以弹出显示“Ring”的对话框; 但是从Modem发短信出去,成功或失败后却不能激发comEvReceive事件。是不是控件版本低呀?但是我的VB6已经打了SP6的补丁了啊! --------------------编程问答-------------------- 你MSComm1_OnComm()事件代码中的Me.MSComm1.RThreshold = 0使得OnComm 事件中止.
见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
--------------------编程问答-------------------- zdingyun,感谢你的回复!将Me.MSComm1.RThreshold = 0注释掉后还是不可以。我在OnComm事件中写上此句的目的是避免在产生comEvReceive事件后的处理过程中再次激发OnComm事件。所以在接到comEvReceive将RThreshold = 0避免再次激发,处理完后再将RThreshold = 1,接受下次comEvReceive事件。 --------------------编程问答-------------------- 代码strReceive = strReceive & Me.comSMS.Input
中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函数不要用,回中止接收引起错误。 --------------------编程问答-------------------- msgbox一弹出接收就会断掉 --------------------编程问答-------------------- zdingyun 果然好眼力。不过这样似乎会造成频繁读取缓冲区,我的办法是再OnComm第一次触发的时候,启动一个timer,每次获得当前缓冲区的InBufferCount,然后与上一次的进行比较,如果相同则代表一个消息包已经接收完毕。这时候再一次性读入。我的timer的间隔设为500,反正运行到现在没有发现有读取消息包错误的。
Private Sub MSComm1_OnComm()
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 --------------------编程问答-------------------- 感谢各位的回复,按照各位提醒,修改了程序,还是不可以!comEvReceive还是不能触发!
Option Explicit
Dim strReceive As String
Private Sub Command1_Click()
Me.MSComm1.Output = "AT+CMGS = +8613912345678" & Chr(13) & "Please don't reply this SMS." & Chr(26)
End Sub
Private Sub Form_Load()
With Me.MSComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.PortOpen = True
.OutBufferCount = 0
.InBufferCount = 0
.RThreshold = 1
End With
End Sub
Private 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 --------------------编程问答-------------------- Private 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
用以上代码,检查Debug.Print strReceive 反馈内容. --------------------编程问答-------------------- zdingyun,谢谢你的回复!按照你的要求,我注释调if then语句,仍然不可以!我怀疑是不是我的vb6安装有问题,你能把在你那里测试成功的代码发给我吗?我的信箱是gisoft@163.com
--------------------编程问答-------------------- 已给你邮箱发了"串口编程调试精灵"的工程压缩文件. --------------------编程问答-------------------- 已给你邮箱发了"测试成功的代码"的工程压缩文件.提醒一点,你的数据线接法是否正确。 --------------------编程问答-------------------- 谢谢各位!
补充:VB , 控件