VB 串口通信接收数据为什么是乱码呢?
我写的一个PC与单片机的一个测试的程序,接收单片机发送过来的数据显示在Text里面,但我在Text里面看到是乱码呢?DimaAsVariantDimb()AsBytePr...
我写的一个PC与单片机的一个测试的程序,接收单片机发送过来的数据显示在Text里面,但我在Text里面看到是乱码呢?
Dim a As Variant
Dim b() As Byte
Private Sub Form_Load()
MSComm1.InBufferSize = 1024
MSComm1.CommPort = 1
Timer1.Interval = 500
Timer1.Enabled = True
MSComm1.InputMode = 1
End Sub
Private Sub Timer1_Timer()
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputLen = 10
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
If MSComm1.InBufferCount > 0 Then
a = MSComm1.Input
b = a
Text1.Text = b
End If
End Sub
1楼的朋友,
为什么在调试时对这一句“For i = 0 To UBound(b) ”会出现“下标越界”的提示呢? 展开
Dim a As Variant
Dim b() As Byte
Private Sub Form_Load()
MSComm1.InBufferSize = 1024
MSComm1.CommPort = 1
Timer1.Interval = 500
Timer1.Enabled = True
MSComm1.InputMode = 1
End Sub
Private Sub Timer1_Timer()
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputLen = 10
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
If MSComm1.InBufferCount > 0 Then
a = MSComm1.Input
b = a
Text1.Text = b
End If
End Sub
1楼的朋友,
为什么在调试时对这一句“For i = 0 To UBound(b) ”会出现“下标越界”的提示呢? 展开
2个回答
展开全部
抱歉FOR NEXT循环漏了NEXT句:
Dim a As Variant
Dim b() As Byte
Dim strData As String
Private Sub Form_Load()
MSComm1.InBufferSize = 1024
MSComm1.CommPort = 1
Timer1.Interval = 500
Timer1.Enabled = True
MSComm1.InputMode = 1
End Sub
Private Sub Timer1_Timer()
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputLen = 10
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
If MSComm1.InBufferCount > 0 Then
a = MSComm1.Input
b = a
Dim i As Integer
For i = 0 To UBound(b)
IF Hex(b(i))=1 Then
strData = strData & "0" & Hex(b(i))
Else
strData = strData & Hex(b(i))
End If
Next i
Text1.Text = strData
End If
End Sub
Dim a As Variant
Dim b() As Byte
Dim strData As String
Private Sub Form_Load()
MSComm1.InBufferSize = 1024
MSComm1.CommPort = 1
Timer1.Interval = 500
Timer1.Enabled = True
MSComm1.InputMode = 1
End Sub
Private Sub Timer1_Timer()
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputLen = 10
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
If MSComm1.InBufferCount > 0 Then
a = MSComm1.Input
b = a
Dim i As Integer
For i = 0 To UBound(b)
IF Hex(b(i))=1 Then
strData = strData & "0" & Hex(b(i))
Else
strData = strData & Hex(b(i))
End If
Next i
Text1.Text = strData
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询