VB实现单片机PC串口通讯

来源:百度知道 编辑:UC知道 时间:2024/05/10 13:49:31
Private Sub MSComm1_OnComm()
On Error Resume Next
Text1 = ""
Dim a() As Byte
Dim strBuff As String
Dim strData As String
Dim i As Integer
Dim x As Integer

Select Case MSComm1.CommEvent Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
a() = strBuff

For i = 0 To UBound(a)
If Len(Hex(a(i))) = 1 Then
strData = strData & "0" & Hex(a(i))
Else
strData = strData & Hex(a(i))
End If
Next 'for [exit for] next

Text1 = Text1 + strData
End Select
End Sub

以上可以实现接收,但只能接收7个字节的数据`如果数据是9个字节则接收不了`这是为什么?应该怎么改啊?

最好不要用 Private Sub MSComm1_OnComm事件
向串口发完指令后就等待回应就行了
首先将
MSComm1.InputLen = 0 读出所有数据
MSComm1.InputMode=comInputModeBinary
with mscomm1
do
doevents
if .InBufferCount>9 then exit do
loop
dim varstr as Variant
varstr=.input
end with
所有数据都在varstr数组里了