COM接收有关问题还没解决,请高手帮忙看看小弟我的代码,万分感谢

COM接收问题还没解决,请高手帮忙看看我的代码,万分感谢。
--目的--:
通过单击命令1,得到设备当前频率,并显示在Frequency.txt 文本框;
通过单击命令2,得到设备当前温度,并显示在Temp.txt 文本框;

----问题如下----:
(1)点击命令1的话,Frequency.txt 为空白;再按一次,正常了;
(2)点击命令2的话,Temp.txt 显示的是命令1的频率信息;然后再点击一下命令2,这时Temp.txt 显示正常;

---代码----:

Private Sub Command1_Click() ' 初始化

MSComm1.CommPort = PortNo
MSComm1.Settings = "115200,n,8,1"
MSComm1.InputMode = comInputModeText ‘文本方式传输
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.RThreshold = 1 '通过oncomm事件读取COM的返回值
MSComm1.PortOpen = True
MSComm1.InputLen = 0

End Sub


Private Sub Command1_Click() ’1命令-得到设备当前频率

If MSComm1.PortOpen = True Then
MSComm1.Output = vbLf & "r?"
Frequency.Text = result
result = ""
End If

End Sub


Private Sub Command2_Click() ‘2命令-得到设备当前温度
If MSComm1.PortOpen = True Then

MSComm1.Output = vbLf & "t?"
Temp.Text = result
result = ""
End If

End Sub



Private Sub MSComm1_OnComm()

If (MSComm1.CommEvent = comEvReceive) Then

result = result & Trim(MSComm1.Input) 

End If

MSComm1.InBufferCount = 0
 
MSComm1.OutBufferCount = 0

End Sub


应该按一次命令按钮就能得到正确的信息才行啊,

请问这是怎么回事?怎么解决啊?



------解决方案--------------------
Dim blnIsFreq As Boolean

Private Sub Form_Load() ' 初始化 

MSComm1.CommPort = PortNo 
MSComm1.Settings = "115200,n,8,1" 
MSComm1.InputMode = comInputModeText ‘文本方式传输 
MSComm1.InBufferCount = 0 
MSComm1.OutBufferCount = 0 
MSComm1.RThreshold = 1 '通过oncomm事件读取COM的返回值 
MSComm1.PortOpen = True 
MSComm1.InputLen = 0 

End Sub 

Private Sub Command1_Click() ’1命令-得到设备当前频率 
If MSComm1.PortOpen = True Then 
MSComm1.Output = vbLf & "r?" 
End If 
End Sub 


Private Sub Command2_Click() ‘2命令-得到设备当前温度 
If MSComm1.PortOpen = True Then 
MSComm1.Output = vbLf & "t?" 
End If
End Sub 


Private Sub MSComm1_OnComm() 

If (MSComm1.CommEvent = comEvReceive) Then 

result = Trim(MSComm1.Input) 

If blnIsFreq Then
Frequency.Text = Frequency.Text & result 
Else
Temp.Text = Temp.Text & result
End If

End If 


End Sub 

结构还是不太好。最好能够判断已经接收到完整的信息,一次提取。
------解决方案--------------------
VB code
Option Explicit
    Dim result As String
    Dim strSend As String
Private Sub Command1_Click()  '1命令-得到设备当前频率
    If MSComm1.PortOpen = True Then
        strSend = vbLf & "r?"
        MSComm1.Output = strSend
    End If
End Sub


Private Sub Command2_Click() '2命令-得到设备当前温度
    If MSComm1.PortOpen = True Then
        strSend = vbLf & "t?"
        MSComm1.Output = strSend
    End If
End Sub

Private Sub Form_Load()
    MSComm1.CommPort = 1 'PortNo
    MSComm1.Settings = "9600,n,8,1"
    MSComm1.InputMode = comInputModeText '文本方式传输
    MSComm1.InBufferCount = 0
    MSComm1.OutBufferCount = 0
    MSComm1.RThreshold = 1            '通过oncomm事件读取COM的返回值
    MSComm1.PortOpen = True
    MSComm1.InputLen = 0
End Sub

Private Sub MSComm1_OnComm()
    Select Case MSComm1.CommEvent
    Case comEvReceive
        result = result & Trim(MSComm1.Input)
        If strSend = vbLf & "r?" Then
            Frequency.Text = result
            result = ""
        ElseIf strSend = vbLf & "t?" Then
            Temp.Text = result
            result = ""
        End If
        MSComm1.InBufferCount = 0
        MSComm1.OutBufferCount = 0
    End Select
End Sub