VB编写串口收发数据时出现8020错误
用串口助手时正常收发,提示是inbuf=MSComm1.Input有问题。PrivateSubCommand1_Click()Dimsj(4)AsBytesj(0)=&H...
用串口助手时正常收发,提示是inbuf = MSComm1.Input有问题。
Private Sub Command1_Click()
Dim sj(4) As Byte
sj(0) = &HAA
sj(1) = &H55
sj(2) = &H5
sj(3) = &H5
sj(4) = &H9
MSComm1.Output = sj
End Sub
Private Sub Command2_Click()
Dim sj(4) As Byte
sj(0) = &HAA
sj(1) = &H55
sj(2) = &H5
sj(3) = &H7
sj(4) = &HB
MSComm1.Output = sj
End Sub
Private Sub Command3_Click()
Text1.Text = ""
End Sub
Private Sub Command4_Click()
MSComm1.PortOpen = False
Unload Me
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 3
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = 1
MSComm1.RThreshold = 1 '设置并返回要接收的字符数
MSComm1.SThreshold = 1 '设置并返回发送缓冲区中允许的最小字符数
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvCD
Case comEvCTS
Case comEvDSR
Case comEvRing
Case comEvReceive
Dim inbuf() As Byte, i%, buf$
buf = ""
inbuf = MSComm1.Input
Rev_num = UBound(inbuf)
ReDim lnum(0 To Rev_num) As Integer
For i = 0 To Rev_num
lnum(i) = inbuf(i)
buf = buf + Str(inbuf(i)) + " "
Next i
Text1.Text = Text1 + buf
Case comEvSend
End Select
End Sub
新人不知是啥问题,大神帮帮忙啊。 展开
Private Sub Command1_Click()
Dim sj(4) As Byte
sj(0) = &HAA
sj(1) = &H55
sj(2) = &H5
sj(3) = &H5
sj(4) = &H9
MSComm1.Output = sj
End Sub
Private Sub Command2_Click()
Dim sj(4) As Byte
sj(0) = &HAA
sj(1) = &H55
sj(2) = &H5
sj(3) = &H7
sj(4) = &HB
MSComm1.Output = sj
End Sub
Private Sub Command3_Click()
Text1.Text = ""
End Sub
Private Sub Command4_Click()
MSComm1.PortOpen = False
Unload Me
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 3
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = 1
MSComm1.RThreshold = 1 '设置并返回要接收的字符数
MSComm1.SThreshold = 1 '设置并返回发送缓冲区中允许的最小字符数
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvCD
Case comEvCTS
Case comEvDSR
Case comEvRing
Case comEvReceive
Dim inbuf() As Byte, i%, buf$
buf = ""
inbuf = MSComm1.Input
Rev_num = UBound(inbuf)
ReDim lnum(0 To Rev_num) As Integer
For i = 0 To Rev_num
lnum(i) = inbuf(i)
buf = buf + Str(inbuf(i)) + " "
Next i
Text1.Text = Text1 + buf
Case comEvSend
End Select
End Sub
新人不知是啥问题,大神帮帮忙啊。 展开
2个回答
推荐于2016-03-19 · 知道合伙人软件行家
关注
展开全部
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
' Case comEvCD
' Case comEvCTS
' Case comEvDSR
' Case comEvRing
Case comEvReceive
Dim inbuf() As Byte, i%, buf$
buf = ""
inbuf = MSComm1.Input
Rev_num = UBound(inbuf)
ReDim lnum(0 To Rev_num) As Integer
For i = 0 To Rev_num
lnum(i) = inbuf(i)
buf = buf + Str(inbuf(i)) + " "
Next i
Text1.Text = Text1 + buf
' Case comEvSend
End Select
End Sub
经测试,代码未发现问题。
追问
我同学可以运行这串代码,但是我一运行就出错。。。
追答
查看下你的COM口所用的硬件(估计是USB转的串口),不要收发同时进行。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询