本帖最后由 定格 于 2015-7-11 09:47 编辑
Private Sub Command2_Click()
With MSComm1
.Settings = "9600,N,8,1" '设置通信口参数
.InputMode = comInputModeBinary '设置接收数据模式为二进制形式
.InputLen = 1 '设置input一次从接收缓冲区读取字节数为1
.InBufferCount = 0 '清除接收缓冲区,等待计算机接收的字符数为0
.RThreshold = 1 '设置接收一个字节产生oncomm事件,打开接收
.SThreshold = 0
.PortOpen = True
End With
End Sub
Private Sub Form_Load()
Text1.Text = ""
End Sub
Private Sub MSComm1_OnComm()
Dim buffer As Variant
Dim arr() As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
buffer = MSComm1.Input
arr = buffer '返回一组二进制数据
Text1.Text = Text1.Text + " " + Hex(arr(0))
End Select
End Sub
Private Sub Timer1_Timer()
If Text1.Text = "1" Then
SendKeys "{h}"
ElseIf Text1.Text = "12" Then
SendKeys "p"
ElseIf Text1.Text = "123" Then
SendKeys "y"
End If
End Sub
接收来自单片机的发来的数据1 2.3,显示在Text1.Text ,但同时如果Text1.Text =1 在光标闪烁的地方就发送h .ElseIf Text1.Text = "2" Then SendKeys "p". 程序这样为什么不行呢?
|