Option Explicit Dim strData As StringPrivate Sub MsComm1_OnComm() Dim inByte() As Byte Dim i As Integer Select Case MsComm1.CommEvent Case comEvReceive inByte = MsComm1.Input For i = 0 To UBound(inByte) If Len(Hex(inByte(i))) = 1 Then strData = strData & "0" & Hex(inByte(i)) Else strData = strData & Hex(inByte(i)) End If Next If Mid(strData,1,2) = "11" And Mid(strData,5,2) = "22" Then Text1.Text = strData strData = "" End If End Select End SubPrivate Sub Form_Load() With MsComm1 .CommPort = 1 .Settings = "9600,n,8,1" .InBufferCount = 0 .InputLen = 0 .RThreshold = 1 .InputMode = comInputModeBinary '以2进制接收 .PortOpen = True End With Text1 = "" End Sub
1 如果有起始字节和结束字节,则不必限定数据块的长度。 2 如果数据块的实意字节不会出现 11H 和 22H,情况较简单。如下例。如果会出现,则用重发机制处理,略为复杂。 3 数据部分编码是可打印字符,还是二进制数据?下例按后者处理。前者用 Chr() 函数转换即可。Private Sub Form_Load() With MsComm1 .CommPort = 1 .Settings = "9600,n,8,1" .InBufferCount = 0 .InputLen = 0 .RThreshold = 1 .InputMode = comInputModeBinary '以2进制接收 .PortOpen = True End With Text1 = "" 'Set Following Properties at Design Time 'Text1.MultiLine = True 'Text1.ScrollBars = vbVertical '2 - Vertical End SubPrivate Sub Form_Unload() MsComm1.PortOpen = False End SubPrivate Sub MsComm1_OnComm() Dim strLine As String Dim varTmp As Variant Dim bytIn() As Byte Dim i As Integer Select Case MsComm1.CommEvent Case comEvReceive varTmp = MsComm1.Input bytIn = varTmp For i = LBound(bytIn) To UBound(bytIn) If bytIn(i) = &H22 Then '结束字符 If strLine > "" Then Text1 = Text1 & IIf(Text1 > "", vbCrLf, "") & strLine Text1.SelStart = Len(Text1) End If ElseIf bytIn(i) = &H11 Then '起始字符 strLine = "" Else '实意字节 strLine = strLine & IIf(strLine > "", " ", "") & Right("0" & Hex(bytIn(i)), 2) End If Next i End Select End Sub
Option Explicit Dim strData As StringPrivate Sub MsComm1_OnComm() Dim inByte() As Byte Dim i As Integer Select Case MsComm1.CommEvent Case comEvReceive inByte = MsComm1.Input For i = 0 To UBound(inByte) If Len(Hex(inByte(i))) = 1 Then strData = strData & "0" & Hex(inByte(i)) Else strData = strData & Hex(inByte(i)) End If Next If Mid(strData,1,2) = "11" And RighT(strData,2) = "22" Then Text1.Text = strData strData = "" End If End Select End SubPrivate Sub Form_Load() With MsComm1 .CommPort = 1 .Settings = "9600,n,8,1" .InBufferCount = 0 .InputLen = 0 .RThreshold = 1 .InputMode = comInputModeBinary '以2进制接收 .PortOpen = True End With Text1 = "" End Sub
LZ:如果你每组数据总长5字节: Private Sub MsComm1_OnComm() Dim inByte() As Byte Dim i As Integer Select Case MsComm1.CommEvent Case comEvReceive inByte = MsComm1.Input For i = 0 To UBound(inByte) If Len(Hex(inByte(i))) = 1 Then strData = strData & "0" & Hex(inByte(i)) Else strData = strData & Hex(inByte(i)) End If Next If Mid(strData,1,2) = "11" And Mid(strData,9,2) = "22" Then Text1.Text = strData strData = "" End If End Select End Sub
我看你将inputlen设置成=0 这样的话不是将接受的数据全部接受拉吗? 我感觉好象需要将inputlen设置成=1,这样每次接受一个字节,才方便判断啊 还有 If Len(Hex(inByte(i))) = 1 Then strData = strData & "0" & Hex(inByte(i)) Else strData = strData & Hex(inByte(i)) End If 这两句话没看懂 帮忙解释下吧
If Len(Hex(inByte(i))) = 1 Then '将接收的Byte字节流转换为16进制字符串 strData = strData & "0" & Hex(inByte(i)) Else strData = strData & Hex(inByte(i)) End If
Dim strData As StringPrivate Sub MsComm1_OnComm()
Dim inByte() As Byte
Dim i As Integer
Select Case MsComm1.CommEvent
Case comEvReceive
inByte = MsComm1.Input
For i = 0 To UBound(inByte)
If Len(Hex(inByte(i))) = 1 Then
strData = strData & "0" & Hex(inByte(i))
Else
strData = strData & Hex(inByte(i))
End If
Next
If Mid(strData,1,2) = "11" And Mid(strData,5,2) = "22" Then
Text1.Text = strData
strData = ""
End If
End Select
End SubPrivate Sub Form_Load()
With MsComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.InBufferCount = 0
.InputLen = 0
.RThreshold = 1
.InputMode = comInputModeBinary '以2进制接收
.PortOpen = True
End With
Text1 = ""
End Sub
2 如果数据块的实意字节不会出现 11H 和 22H,情况较简单。如下例。如果会出现,则用重发机制处理,略为复杂。
3 数据部分编码是可打印字符,还是二进制数据?下例按后者处理。前者用 Chr() 函数转换即可。Private Sub Form_Load()
With MsComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.InBufferCount = 0
.InputLen = 0
.RThreshold = 1
.InputMode = comInputModeBinary '以2进制接收
.PortOpen = True
End With Text1 = ""
'Set Following Properties at Design Time
'Text1.MultiLine = True
'Text1.ScrollBars = vbVertical '2 - Vertical
End SubPrivate Sub Form_Unload()
MsComm1.PortOpen = False
End SubPrivate Sub MsComm1_OnComm()
Dim strLine As String
Dim varTmp As Variant
Dim bytIn() As Byte
Dim i As Integer Select Case MsComm1.CommEvent
Case comEvReceive
varTmp = MsComm1.Input
bytIn = varTmp For i = LBound(bytIn) To UBound(bytIn)
If bytIn(i) = &H22 Then '结束字符
If strLine > "" Then
Text1 = Text1 & IIf(Text1 > "", vbCrLf, "") & strLine
Text1.SelStart = Len(Text1)
End If
ElseIf bytIn(i) = &H11 Then '起始字符
strLine = ""
Else '实意字节
strLine = strLine & IIf(strLine > "", " ", "") & Right("0" & Hex(bytIn(i)), 2)
End If
Next i
End Select
End Sub
Dim strData As StringPrivate Sub MsComm1_OnComm()
Dim inByte() As Byte
Dim i As Integer
Select Case MsComm1.CommEvent
Case comEvReceive
inByte = MsComm1.Input
For i = 0 To UBound(inByte)
If Len(Hex(inByte(i))) = 1 Then
strData = strData & "0" & Hex(inByte(i))
Else
strData = strData & Hex(inByte(i))
End If
Next
If Mid(strData,1,2) = "11" And RighT(strData,2) = "22" Then
Text1.Text = strData
strData = ""
End If
End Select
End SubPrivate Sub Form_Load()
With MsComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.InBufferCount = 0
.InputLen = 0
.RThreshold = 1
.InputMode = comInputModeBinary '以2进制接收
.PortOpen = True
End With
Text1 = ""
End Sub
能够接受到数据
但是每次我的strData里面的都是很多组11××××××22的数据连在一起的,等于是我一次接受的数据里面
实际是接受了不定的很多数据
这样我没法处理啊
能不能让我的strData每次接受的就是一组11××××××22数据呢????
Private Sub MsComm1_OnComm()
Dim inByte() As Byte
Dim i As Integer
Select Case MsComm1.CommEvent
Case comEvReceive
inByte = MsComm1.Input
For i = 0 To UBound(inByte)
If Len(Hex(inByte(i))) = 1 Then
strData = strData & "0" & Hex(inByte(i))
Else
strData = strData & Hex(inByte(i))
End If
Next
If Mid(strData,1,2) = "11" And Mid(strData,9,2) = "22" Then
Text1.Text = strData
strData = ""
End If
End Select
End Sub
这样的话不是将接受的数据全部接受拉吗?
我感觉好象需要将inputlen设置成=1,这样每次接受一个字节,才方便判断啊
还有
If Len(Hex(inByte(i))) = 1 Then
strData = strData & "0" & Hex(inByte(i))
Else
strData = strData & Hex(inByte(i))
End If
这两句话没看懂
帮忙解释下吧
If Len(Hex(inByte(i))) = 1 Then '将接收的Byte字节流转换为16进制字符串
strData = strData & "0" & Hex(inByte(i))
Else
strData = strData & Hex(inByte(i))
End If
返回 Variant (String),其中包含字符串中指定数量的字符。
语法
Mid(string, start[, length])
Mid 函数的语法具有下面的命名参数:
部分 说明
string 必要参数。字符串表达式,从中返回字符。如果 string 包含 Null,将返回 Null。
start 必要参数。为 Long。string 中被取出部分的字符位置。如果 start 超过 string 的字符数,Mid 返回零长度字符串 ("")。
length 可选参数;为 Variant (Long)。要返回的字符数。如果省略或 length 超过文本的字符数(包括 start 处的字符),将返回字符串中从 start 到尾端的所有字符。
2)适当范围内的数字,前缀以 &H,可以直接表示十六进制数字。例如,十六进制表示法的
&H10 代表十进制的 16
&H1CB 代表十进制的459
strdata是一串字符串y=hex( val("&H" & mid(strdata,1,2)) xor val("&H" & mid(strdata,3,2)) xor
val("&H" & mid(strdata,5,2)))从第一个十六进制字符开始,每两个字符取出来,用&H确认是16进制数,再用val转化为数字,进行异或操作后
换算成十六进制付给y,以供使用看看这样把吧字符串转化为数字了吧
帮忙看下
运气比较好的是数字的位置都是固定的
所以好处理ps:
对vb不熟,但是通过大家的帮忙,慢慢摸索,开始觉得有点意思了
谢谢各位