Private Sub Command1_Click() Text1.Text = "00 AA AA FF FF" Dim Str As String Dim a() As String Dim Data() As String Str = Text1.Text a = Split(Str, " ") ReDim Data(UBound(a)) As Byte For i = 0 To UBound(a) Data(i) = CInt("&H" & a(i)) Next MSComm.output = Data End Sub
ReDim Data(UBound(a)) As Byte 这句话运行提示"不能改变数组元素的数据类型",这是怎么回事?
把Dim Data() As String与ReDim Data(UBound(a)) As Byte的类型改为一样的,要不然就全是string,要不然就全是byte 试试^_^
Dim buffer As Variant 'Dim i As Integer Text2.Text = "00 AA AA FF FF" Dim Str As String Dim a() As String Dim Data() As Byte Str = Text2.Text a = Split(Str, " ") ReDim Data(UBound(a)) As Byte For i = 0 To UBound(a) Data(i) = CInt("&H" & a(i)) Next With MSC .InputMode = comInputModeBinary .CommPort = 1 .Settings = "19200,n,8,1" If .PortOpen = False Then .PortOpen = True End If End With buffer = Data MSComm.Output = buffer 如果这么写,运行时报错"要求对象",问题出在哪?
Text1.Text = "00 AA AA FF FF"
Dim Str As String
Dim a() As String
Dim Data() As String
Str = Text1.Text
a = Split(Str, " ")
ReDim Data(UBound(a)) As Byte
For i = 0 To UBound(a)
Data(i) = CInt("&H" & a(i))
Next
MSComm.output = Data
End Sub
这句话运行提示"不能改变数组元素的数据类型",这是怎么回事?
试试^_^
'Dim i As Integer
Text2.Text = "00 AA AA FF FF"
Dim Str As String
Dim a() As String
Dim Data() As Byte
Str = Text2.Text
a = Split(Str, " ")
ReDim Data(UBound(a)) As Byte
For i = 0 To UBound(a)
Data(i) = CInt("&H" & a(i))
Next
With MSC
.InputMode = comInputModeBinary
.CommPort = 1
.Settings = "19200,n,8,1"
If .PortOpen = False Then
.PortOpen = True
End If End With
buffer = Data
MSComm.Output = buffer
如果这么写,运行时报错"要求对象",问题出在哪?
Dim Data() As BYTE