采用MSCOMM1控件获取的串口数据,每个时钟周期内获取的有效为21位(包括起始位和结束位) 以FO开始 FF结束,我要取的值有两个,一个是除开始位从第1为到第6位,第二个数从第7位到12位,下面是一组有效数据,请各位帮忙分析一下。 F0 34 33 2E 34 36 39 00 36 2E 38 39 37 01 2D 31 2E 32 31 35 FF Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Dim Fbit(5) As Byte Dim Sbit(5) As Byte Private Sub Timer1_Timer()
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
Text1 = MSComm1.InBufferCount Text2 = "" Dim BytesReceived() As Byte Dim buffer As Variant buffer = MSComm1.Input BytesReceived = buffer For i = 0 To UBound(BytesReceived) If UBound(BytesReceived) >= 21 Then If BytesReceived(i) = 240 And BytesReceived(i + 20) = 255 Then For j = i To i + 20 If Len(Hex(BytesReceived(j))) = 1 Then HData = 0 & Hex(BytesReceived(j)) Else HData = Hex(BytesReceived(j)) End If Text2.Text = Text2.Text & " " & HData Next For k = 0 To 5 Fbit(k) = BytesReceived(k + 1) Sbit(k) = BytesReceived(k + 6)
Debug.Print Fbit(k) Next Exit For Else i = i + 1 End If End If Next Dim singleF As Single Dim singleS As Single
*****************************************************************************
欢迎使用CSDN论坛阅读器 : CSDN Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
一运行VB就自动关掉了
F0 34 33 2E 34 36 39 00 36 2E 38 39 37 01 2D 31 2E 32 31 35 FF
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Dim Fbit(5) As Byte
Dim Sbit(5) As Byte
Private Sub Timer1_Timer()
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
Text1 = MSComm1.InBufferCount
Text2 = ""
Dim BytesReceived() As Byte
Dim buffer As Variant
buffer = MSComm1.Input
BytesReceived = buffer
For i = 0 To UBound(BytesReceived)
If UBound(BytesReceived) >= 21 Then
If BytesReceived(i) = 240 And BytesReceived(i + 20) = 255 Then
For j = i To i + 20
If Len(Hex(BytesReceived(j))) = 1 Then
HData = 0 & Hex(BytesReceived(j))
Else
HData = Hex(BytesReceived(j))
End If
Text2.Text = Text2.Text & " " & HData
Next
For k = 0 To 5
Fbit(k) = BytesReceived(k + 1)
Sbit(k) = BytesReceived(k + 6)
Debug.Print Fbit(k)
Next
Exit For
Else
i = i + 1
End If
End If
Next
Dim singleF As Single
Dim singleS As Single
CopyMemory singleF, Fbit(0), 6
CopyMemory singleS, Sbit(0), 6 Text3 = Format(CStr(singleF), "#0.0")
Text4 = Format(CStr(singleS), "#0.0")End Sub