我现在可以实现收发了,但是我收的数据strdata每次都是一组数据,比如 12 67 ad 等,每次都是两位数,我想把这些数据都显示在一个数组里面,请问哪个高手能帮一下? Dim BytReceived() As Byte
Dim strData As String
Dim lenInput As Integer
Private Sub Command1_Click()
Dim OutBuffer() As Byte
Dim tem As Variant
Dim e As Integer
Dim q As Integer
Dim LenOfText As Integer
LenOfText = Len(txtSum.Text) \ 2 - 1
ReDim OutBuffer(LenOfText) If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
q = 0
For e = 1 To Len(txtSum.Text) Step 2
tem = Mid(txtSum.Text, e, 2)
OutBuffer(q) = Val("&H" & tem)
' Print Val("&H" & tem)
q = q + 1
Next
MSComm1.Output = OutBuffer
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "1200,e,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0
MSComm1.InBufferSize = 4096 '''''设置接收缓冲区的字节数
MSComm1.OutBufferSize = 4096 ''''''以字节的形式设置传输缓冲区的大小
MSComm1.PortOpen = True
MSComm1.SThreshold = 0 '''''''''''''设置并返回传输缓冲区中允许的最小字符数
MSComm1.RThreshold = 1 ''''''''''''''当接收字符后,若 Rthreshold 属性设置为 0(缺省值)则不产生 OnComm 事件
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
'lenInput = Len(strData)
'Text2 = lenInput \ 2
'数据处理代码
End Select
End SubPublic Function jieshou() '接收数据处理为16进制等待命令
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
Text1.Text = strData
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.InBufferCount = 0 '清除接收缓冲区
'MSComm1.PortOpen = False ''''''''加上他以后,收的数据不全,紧紧收到一半的数据
End Function
Dim strData As String
Dim lenInput As Integer
Private Sub Command1_Click()
Dim OutBuffer() As Byte
Dim tem As Variant
Dim e As Integer
Dim q As Integer
Dim LenOfText As Integer
LenOfText = Len(txtSum.Text) \ 2 - 1
ReDim OutBuffer(LenOfText) If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
q = 0
For e = 1 To Len(txtSum.Text) Step 2
tem = Mid(txtSum.Text, e, 2)
OutBuffer(q) = Val("&H" & tem)
' Print Val("&H" & tem)
q = q + 1
Next
MSComm1.Output = OutBuffer
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "1200,e,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0
MSComm1.InBufferSize = 4096 '''''设置接收缓冲区的字节数
MSComm1.OutBufferSize = 4096 ''''''以字节的形式设置传输缓冲区的大小
MSComm1.PortOpen = True
MSComm1.SThreshold = 0 '''''''''''''设置并返回传输缓冲区中允许的最小字符数
MSComm1.RThreshold = 1 ''''''''''''''当接收字符后,若 Rthreshold 属性设置为 0(缺省值)则不产生 OnComm 事件
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
'lenInput = Len(strData)
'Text2 = lenInput \ 2
'数据处理代码
End Select
End SubPublic Function jieshou() '接收数据处理为16进制等待命令
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
Text1.Text = strData
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.InBufferCount = 0 '清除接收缓冲区
'MSComm1.PortOpen = False ''''''''加上他以后,收的数据不全,紧紧收到一半的数据
End Function
For i = 0 To UBound(bitin)
sinput = sinput & Format(Hex(bitin(i)), "00")
Next
text1.text = sinput
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
Dim BytReceived() As Byte
Dim strData As String
Dim lenInput As Integer
Dim strSum() As String
Private Sub Command1_Click()
Dim OutBuffer() As Byte
Dim tem As Variant
Dim e As Integer
Dim q As Integer
Dim LenOfText As Integer
LenOfText = Len(txtSum.Text) \ 2 - 1
ReDim OutBuffer(LenOfText) If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
q = 0
For e = 1 To Len(txtSum.Text) Step 2
tem = Mid(txtSum.Text, e, 2)
OutBuffer(q) = Val("&H" & tem)
' Print Val("&H" & tem)
q = q + 1
Next
MSComm1.Output = OutBuffer
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "1200,e,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0
MSComm1.InBufferSize = 4096 '''''设置接收缓冲区的字节数
MSComm1.OutBufferSize = 4096 ''''''以字节的形式设置传输缓冲区的大小
MSComm1.PortOpen = True
MSComm1.SThreshold = 0 '''''''''''''设置并返回传输缓冲区中允许的最小字符数
MSComm1.RThreshold = 1 ''''''''''''''当接收字符后,若 Rthreshold 属性设置为 0(缺省值)则不产生 OnComm 事件
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
'lenInput = Len(strData)
'Text2 = lenInput \ 2
'数据处理代码
End Select
End SubPublic Function jieshou() '接收数据处理为16进制等待命令
Dim i As Integer
If SafeArrayGetDim(strSum) <= 0 Then
ReDim strSum(0)
Else
ReDim Preserve strSum(UBound(strSum) + 1)
End If
For i = 0 To UBound(BytReceived)
strData = strData & Right("0" & Hex(BytReceived(i)), 2)
Next
strSum(UBound(strSum)) = strData
strData = ""
Text1.Text = Join(strSum, " ") '/////////////////////////什么时候输入自已定位置
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.InBufferCount = 0 '清除接收缓冲区
'MSComm1.PortOpen = False ''''''''加上他以后,收的数据不全,紧紧收到一半的数据
End Function