VB串口发送数组数据

本人做一个VB控件发送两个文本框组成的一组十六进制数据,通过数据转换用BYTE类型直接发送,发送单个字节没有问题,可是转换后的数据发送的数好像是字符串类型的,显示出数值感... 本人做一个VB控件发送两个文本框组成的一组十六进制数据,通过数据转换用BYTE类型直接发送,发送单个字节没有问题,可是转换后的数据发送的数好像是字符串类型的,显示出数值感觉像乱码,并且发送数据采用循环发送好像无法实现,接收时总提示类型不符合,跪求高手指点!
部分主要发送程序如下:

Public Flag_ON As Boolean '串口打开标志
Dim Send_ID As String '发送ID数据
Dim Send_Data As String '发送Data数据
Dim Receive_ID As String '接收ID数据
Dim Receive_Data As Variant '接收Data数据
Dim Flag_Send As Boolean '发送按钮标志
Dim Timer_Count As Integer '定时时间
Dim Recevie_txt As String '接收数据单元

Dim num As Integer
Dim a(10) As Byte '接收数据缓冲区
Dim b(10) As String '发送数据缓冲区
Dim c(10) As String '接收数据缓冲区
Dim d(10) As Byte '发送数据缓冲区
Dim x(0) As Byte
Private Sub MSComm1_OnComm()
Dim i As Integer
MSComm1.InputMode = comInputModeBinary
Receive_Data = MSComm1.Input '串口接收
a(num) = Receive_Data
’c(num) = receive_change(Receive_Data) ’十进制数值转化为十六进制
num = num + 1
If num = 10 Then '////////////////////////////
For i = 0 To 9
c(i) = receive_change(a(i))
Next i
Recevie_txt = "Receive" & " " & "ID:" & c(0) & " " & c(1) & " DATA:" & c(2) & " " & c(3) & " " & c(4) & " " & c(5) & " " & c(6) & " " & c(7) & " " & c(8) & " " & c(9)
num = 0
List_Show.AddItem Recevie_txt
End If
End Sub
Private Sub Timer1_Timer()
Dim i, j As Integer
i = 0
j = 0
Send_ID = Left((Text1.Text), 5) '发送数据处理
Send_Data = Left(Trim(Text2.Text), 23)
For i = 0 To 1
b(i) = Int(Val(Mid(Send_ID, i + 1 + j, 2)))
j = j + 1
Next i
i = 0
j = 0
For i = 2 To 9
b(i) = Int(Val(Mid(Send_Data, i - 1 + j, 2)))
j = j + 2
Next i
’d(9) = 9
For i = 0 To 9
d(i) = send_change(b(i)) ’发送十六进制转化为十进制
List_Show.AddItem d(i)
Next i
Timer_Count = Val(Text3.Text) '读取定时发送时间
If Flag_ON And Timer_Count > 0 Then '串口已打开且发送时间不为0则发送 ////////////////////////
'For i = 0 To 9
x(0) = d(i)
Timer1.Interval = Timer_Count
MSComm1.Output = x()
'Next i
List_Show.AddItem ("Send " & " " & "ID:" & Send_ID & " DATA:" & Send_Data)
Else '串口未打开且发送时间为0则不发送
Timer1.Enabled = False
CmdSend.Caption = "发送"
Flag_Send = False
If Timer_Count <= 0 Then '发送时间为0
MsgBox "请输入发送时间!", vbOKOnly, "错误:"
Else '串口未打开
MsgBox "串口已关闭!", vbOKOnly, "错误:"
End If
End If
End Sub
展开
 我来答
地大挂科男
推荐于2016-01-29 · 超过19用户采纳过TA的回答
知道答主
回答量:46
采纳率:0%
帮助的人:0
展开全部
'字符表示的十六进制数转化为相应的整数,错误则返回 -1
Function ConvertHexChr(str As String) As Integer
Dim test As Integer
test = Asc(str)
If test >= Asc("0") And test <= Asc("9") Then
test = test - Asc("0")
ElseIf test >= Asc("a") And test <= Asc("f") Then
test = test - Asc("a") + 10
ElseIf test >= Asc("A") And test <= Asc("F") Then
test = test - Asc("A") + 10
Else
test = -1 '出错信息
End If
ConvertHexChr = test
End Function

'字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数
Function strHexToByteArray(strText As String, bytByte() As Byte) As Integer
Dim HexData As Integer '十六进制(二进制)数据字节对应值
Dim hstr As String * 1 '高位字符
Dim lstr As String * 1 '低位字符
Dim HighHexData As Integer '高位数值
Dim LowHexData As Integer '低位数值
Dim HexDataLen As Integer '字节数
Dim StringLen As Integer '字符串长度
Dim Account As Integer
Dim n As Integer
'计数
'txtSend = "" '设初值
HexDataLen = 0
strHexToByteArray = 0
StringLen = Len(strText)
Account = StringLen \ 2
ReDim bytByte(Account)
For n = 1 To StringLen
Do '清除空格
hstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While hstr = " "
Do
lstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While lstr = " "
n = n - 1
If n > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
HighHexData = ConvertHexChr(hstr)
LowHexData = ConvertHexChr(lstr)

If HighHexData = -1 Or LowHexData = -1 Then '遇到非法字符中断转化
HexDataLen = HexDataLen - 1
Exit For
Else
HexData = HighHexData * 16 + LowHexData
bytByte(HexDataLen) = HexData
HexDataLen = HexDataLen + 1
End If
Next n
If HexDataLen > 0 Then '修正最后一次循环改变的数值
HexDataLen = HexDataLen - 1
ReDim Preserve bytByte(HexDataLen)
Else
ReDim Preserve bytByte(0)
End If
If StringLen = 0 Then '如果是空串,则不会进入循环体
strHexToByteArray = 0
Else
strHexToByteArray = HexDataLen + 1
End If
End Function
下面跟你介绍strHexToByteArray(strText As String, bytByte() As Byte)的功能。
假如text内输入"ff fe aa 14 af"(引号内的包括空格)那么
strHexToByteArray=5
则byteByte()中 byteByte(0)=ff byteByte(1)=fe byteByte(2)=aa
byteByte(3)=14 byteByte(4)=af
注意:假如输入你想输入1,则必须写01
我想有了这个函数会给你很大的帮助。
zdingyun
2009-04-18 · 知道合伙人软件行家
zdingyun
知道合伙人软件行家
采纳数:15429 获赞数:48180
1982年上海业余工业大学化工系毕业 现退休

向TA提问 私信TA
展开全部
Option Explicit
Dim sj As String
Dim i As Integer
Dim sendSj() As Byte

Private Sub Command1_Click()
ReDim sendSj(Len(sj) / 2 - 1)
For i = 1 To Len(sj) Step 2
sendSj((i - 1) / 2) = Val("&H" & Mid(sj, i, 2))
Next
MSComm1.Output = sendSj
End Sub

Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 1
MSComm1.PortOpen = True
sj = "1234567890FF"
Text1 = sj
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式