请VB高手进,关于串行通信的

来源:百度知道 编辑:UC知道 时间:2024/05/23 14:15:29
Private Sub Form_Load()
Dim buf As String
MSComm1.PortOpen = True
MSComm1.Settings = "1200,N,8,1"
MSComm1.InputMode = 1
MSComm1.InputLen = 0
MSComm1.RThreshold = 1
MSComm1.Output = "A"
line: buf = MSComm1.Input
If buf <> "A" Then GoTo line
End Sub
我从下位机传送字符A给buf 得到buf="A"
但是做到If buf <> "A" Then GoTo line时
却发现程序跳到line
请高手解决啊
在线等

Private Sub MSComm1_OnComm()事件才是得到串口返回的信息
line: buf = MSComm1.Input 这句是在串口没有返回任何值的时候读取串口信息,当然buf="",所以下一句肯定就goto line
If buf <> "A" Then GoTo line
应该改为:
Private Sub Form_Load()
Dim buf As String
MSComm1.PortOpen = True
MSComm1.Settings = "1200,N,8,1"
MSComm1.InputMode = 1
MSComm1.InputLen = 0
MSComm1.RThreshold = 1
MSComm1.Output = "A"

Private Sub MSComm1_OnComm()
line: buf = MSComm1.Input
If buf <> "A" Then GoTo line
End Sub

MSComm1.Output = "A" 会让上位机送给下位机,"A" .Output 是写入下位机命令,Input才是从下位机读回来的呢,你在详细说下你的问事,我帮你解决