很简单的一个程序但是始终弄不通,就是发送命令,然后收回来。请各位帮忙看看。东拼西凑的程序也是。一个温度采集装置的通信Private Sub Form_Load()
MSComm1.CommPort = 1 ' 设置通信端口号为COM1
MSComm1.Settings = "9600,n,8,1" ' 设置串口1参数
MSComm1.InputMode = comInputModeText ' 接收文本型数据
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True ' 打开通信端口1
End Sub
Private Sub Command1_Click()
MSComm1.OutBufferCount = 0
MSComm1.Output = Text1.Text
End SubPrivate Sub Command2_Click()
Select Case MSComm1.CommEvent
Case comEvReceive '...有接受事件发生
Text2.Text = MSComm1.Input '...接受显示数据
MSComm1.InBufferCount = 0 '...清空输入寄存器
End Select
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Text2 = ""
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
Text2 = strData
End Select
End SubPublic Sub jieshou() '接收处理为16进制显示
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
End Sub
MSComm1.CommPort = 1 ' 设置通信端口号为COM1
MSComm1.Settings = "9600,n,8,1" ' 设置串口1参数
MSComm1.InputMode = comInputModeText ' 接收文本型数据
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True ' 打开通信端口1
End Sub
Private Sub Command1_Click()
MSComm1.OutBufferCount = 0
MSComm1.Output = Text1.Text
End SubPrivate Sub Command2_Click()
Select Case MSComm1.CommEvent
Case comEvReceive '...有接受事件发生
Text2.Text = MSComm1.Input '...接受显示数据
MSComm1.InBufferCount = 0 '...清空输入寄存器
End Select
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Text2 = ""
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
Text2 = strData
End Select
End SubPublic Sub jieshou() '接收处理为16进制显示
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1 ' 设置通信端口号为COM1
MSComm1.Settings = "9600,n,8,1" ' 设置串口1参数
MSComm1.InputMode = comInputModeText ' 接收文本型数据
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True ' 打开通信端口1
End Sub
Private Sub Command1_Click()
MSComm1.OutBufferCount = 0
MSComm1.Output = Text1.Text
Sleep 30 '延迟30ms,以便通信发出,信号返回
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.RThreshold = 0
Do
strBuff = strBuff & MSComm1.Input
Loop Until MSComm1.InBufferCount = 0
Text2.Text = strBuff
MSComm1.RThreshold = 1
End Select
End Sub
1.建议使用一个串口工具先对等通讯设备进行通讯测试,确认通讯良好,也即排除掉接口松脱,线断等问题,关于类似的串口工具网上有很多
2.在判断通讯良好的情况下再进行操作,如你的程序中.
3.断点的设置可以在ONCOMM事件的那一行的最前面点一下,即会出现一个褐色的大点点,即为断点.....
通过断点一般都能很快找出问题点
4.你的设备回传回来的字符串是否有结束字符?以结束字符作为结束判断标志