关键是要搞清楚,想发送的数据类型,如字符串,还是二进制数(命令),我的理解是你要通过串口某些设备的数据,那就要发送二进制命令数据.....
先定义一个Byte 数组,把要发送的16进制字符放如数组,然后发送就OK.
Exp:
dim sendBuffer(5) as Byte
sendBuffer(1)=&H30
sendBuffer(2)=&H30
sendBuffer(3)=&H5F
......
msComm.Output=sendBuffer
Dim 数据(8) As Byte
数据(0) = &H68
数据(1) = &H11
数据(2) = &H22
数据(3) = &H33
数据(4) = &H44
数据(5) = &H55
数据(6) = &H66
数据(7) = &H77
串口1.Write(数据, 0, 8)
//数组可以,单个数据就不对
Private Sub Command1_Click()
Dim sj() As String
Dim sjByt() As Byte
Dim i As Long
sj = Split(Text1, " ")
ReDim sjByt(UBound(sj))
For i = 0 To UBound(sj)
sjByt(i) = Val("&H" & Str(Val(sj(i))))
Next i
MSComm1.Output = sjByt
End Sub
Private Sub Form_Load()
Text1 = "30h 30h 5Fh 63h 31h 0Dh"
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
End Sub
Dim sandcher() As Byte
ReDim sandcher(5)
sandcher(0) = &H30
sandcher(1) = &H30
sandcher(2) = &H5F
sandcher(3) = &H63
sandcher(4) = &H31
sandcher(5) = &H0D
With MSComm1
If .PortOpen = False Then .PortOpen = True
.Output = sandcher
End With
只要发送吗,我这有一份程序,你看看能不能用。这个程序是没有问题的,把Text1中的文字以16进制的形式发送出去(不包括转换16进制的过程)。
'十六进制发送
Private Sub Hexsent()
Dim hexchrlen%, Hexchr As String, hexcyc%, hexmid As Byte, hexmiddle As String, CmdLenth As Integer
Dim hexchrgroup() As Byte, i As Integer
hexchrlen = Len(Text1text)
For hexcyc = 1 To hexchrlen '检查Text1文本框内数值是否合适
Hexchr = Mid(Text1text, hexcyc, 1)
If InStr("0123456789ABCDEFabcdef", Hexchr) = 0 Then
MsgBox "无效的数值,请重新输入", , "错误信息"
Exit Sub
End If
Next
ReDim hexchrgroup(1 To hexchrlen \ 2) As Byte
For hexcyc = 1 To hexchrlen Step 2 '将文本框内数值分成两个、两个
i = i + 1
Hexchr = Mid(Text1text, hexcyc, 2)
hexmid = Val("&H" & CStr(Hexchr))
hexchrgroup(i) = hexmid
Next
CmdLenth = 5 + hexchrgroup(5) * 2
MSComm1.RThreshold = CmdLenth
MSComm1.Output = hexchrgroup
End Sub