vb 串口通信,如何将以空格分割的数据以16进制的方式发送出去
发送的数据不是固定的是手动输入在文本框中例如在文本框中输入1011121314之前有朋友给出代码是发送固定的数据:Dimbt(0To4)AsBytebt(0)="&H"&...
发送的数据不是固定的是手动输入在文本框中例如在文本框中输入10 11 12 13 14
之前有朋友给出代码是发送固定的数据:
Dim bt(0 To 4) As Byte
bt(0) = "&H" & "10"
bt(1) = "&H" & "11"
bt(2) = "&H" & "12"
bt(3) = "&H" & "13"
bt(4) = "&H" & "14"
mscomm1.output=bt
但是发送的数据不是固定的,长度也不是固定的,下面的代码可以将以空格分割的数据全部提取出来,但是取出来之后不知道用怎样发送了?
'-----------------------------------------------------
t = Split(Text1.Text, " ")
For i = 0 To UBound(t)
MsgBox t(i)
Next 展开
之前有朋友给出代码是发送固定的数据:
Dim bt(0 To 4) As Byte
bt(0) = "&H" & "10"
bt(1) = "&H" & "11"
bt(2) = "&H" & "12"
bt(3) = "&H" & "13"
bt(4) = "&H" & "14"
mscomm1.output=bt
但是发送的数据不是固定的,长度也不是固定的,下面的代码可以将以空格分割的数据全部提取出来,但是取出来之后不知道用怎样发送了?
'-----------------------------------------------------
t = Split(Text1.Text, " ")
For i = 0 To UBound(t)
MsgBox t(i)
Next 展开
3个回答
推荐于2016-07-31 · 知道合伙人软件行家
关注
展开全部
Private Sub Command1_Click()
Dim t As Variant
Dim i As Long
Dim sjSend() As Byte
t = Split(Text1.Text, " ")
ReDim sjSend(UBound(t))
For i = 0 To UBound(t)
sjSend(i) = "&H" & t(i)
Next
MSComm1.Output = sjSend
End Sub
Private Sub Form_Load()
Text1 = "10 11 12 13 14"
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
End Sub
Dim t As Variant
Dim i As Long
Dim sjSend() As Byte
t = Split(Text1.Text, " ")
ReDim sjSend(UBound(t))
For i = 0 To UBound(t)
sjSend(i) = "&H" & t(i)
Next
MSComm1.Output = sjSend
End Sub
Private Sub Form_Load()
Text1 = "10 11 12 13 14"
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
End Sub
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
下面代码就是将文本框中以一个空格隔开的十六进制转为字节的代码,文本框中数字格式为:01 02 03
Dim TestArray() As String = Split(TextBox1.Text)
Dim hexBytes() As Byte
ReDim hexBytes(TestArray.Length - 1)
Dim i As Integer
For i = 0 To TestArray.Length - 1
hexBytes(i) = Val("&h" & TestArray(i))
Next
SerialPort.Write(hexBytes, 0, hexBytes.Length)
Dim TestArray() As String = Split(TextBox1.Text)
Dim hexBytes() As Byte
ReDim hexBytes(TestArray.Length - 1)
Dim i As Integer
For i = 0 To TestArray.Length - 1
hexBytes(i) = Val("&h" & TestArray(i))
Next
SerialPort.Write(hexBytes, 0, hexBytes.Length)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
好办,试试如下代码:
dim 发送数据() as byte
t = Split(Text1.Text, " ")
redim 发送数据(0 to ubound(t)
For i = 0 To UBound(t)
发送数据(i)="&H" & t(i)
Next
mscomm1.output=发送数据
dim 发送数据() as byte
t = Split(Text1.Text, " ")
redim 发送数据(0 to ubound(t)
For i = 0 To UBound(t)
发送数据(i)="&H" & t(i)
Next
mscomm1.output=发送数据
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询