关于mscomm的通信问题
先看代码:
Option Explicit
Private Sub CmdClear_Click()
TxtSend.Text = ""
TxtReceive.Text = ""
End Sub
Private Sub CmdExit_Click()
Comm1.PortOpen = False
End
End Sub
Private Sub Comm1_OnComm()
Select Case Comm1.CommEvent
Case comEvCD
Case comEvCTS
Case comEvDSR
Case comEvRing
Case comEvReceive
Dim t&, buf$, StrPro%
t = GetTickCount()
Do
DoEvents
Loop Until GetTickCount - t > 100
buf = Trim(Comm1.Input)
If Len(buf) < 2 Then Exit Sub
TxtReceive.Text = buf
StrPro = InStr(1, buf, "%%")
If StrPro < 1 Then
buf = 0
Comm1.Output = "未接受到正确的格式"
Else
'TxtReceive.Text = Left(buf, Len(buf) - 2)
Comm1.Output = "执行" & Right(buf, Len(buf) - (StrPro + 1)) & "命令成功..."
End If
Case comEvSend
End Select
End Sub
Private Sub Form_Load()
Comm1.PortOpen = True
Comm1.InBufferCount = 0
Comm1.OutBufferCount = 0
End Sub
Private Sub TxtSend_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Comm1.Output = Trim(TxtSend.Text)
End If
End Sub
各位老师
我想当输入格式错误时文本框中显示“格式不正确”,但输入格式正确时显示“格式正确”,但是我不想用如下语句
TxtReceive.text="格式正确"
我想用Comm1.Output = "执行格式正确"这条指令执行后显示出“格式正确”
为啥上面的程序不能实现呢,
不知道哪里出现了问题。
请教各位老师
如果可以的话请留下QQ号码,以供经常交流。
先行谢过。
[解决办法]
Private Sub Comm1_OnComm() Select Case Comm1.CommEvent Case 2 sum = sum + 1 Text2 = sum Dim t&, buf$, StrPro% t = GetTickCount() Do DoEvents Loop Until GetTickCount - t > 100 buf = Comm1.Input If Len(buf) < 2 Then Exit Sub TxtReceive.Text = buf StrPro = InStr(1, buf, "%%") If StrPro < 1 Then buf = 0 Comm1.Output = "未接受到正确的格式" Else Comm1.Output = "执行" & Right(buf, Len(buf) - (StrPro + 1)) & "命令成功..." End If End SelectEnd SubPrivate Sub Form_Load() Comm1.Settings = "9600,n,8,1" Comm1.PortOpen = True Comm1.RThreshold = 1 Comm1.InBufferCount = 0 Comm1.OutBufferCount = 0 Comm1.InputLen = 0 Comm1.InputMode = comInputModeTextEnd Sub