发表于:2007-06-23 07:40:00
楼主
最近做了一用VB采集质量流量计的东西,发出来,大家看看,还有什么地方要完善
Dim index As Integer
Dim currst As Byte
Private Type command
comm(0 To 8) As Byte
End Type
Dim cm(0 To 6) As command
Option Explicit
'浮点数的算法'
Private Function Str2Float(str As String) As Double
Dim k As Integer
Dim i As Integer
Dim j As Integer
Dim s As Integer
Dim SS As Integer
Dim E As Integer
Dim M As Long
Dim d As Double
Dim n As Integer
Dim h As Integer
Dim l As Integer
Dim s2() As String, st As String
s2 = Split(str, " ")
If UBound(s2) <> 3 Then
MsgBox "数据格式错误,无法转换!"
Str2Float = 0
Exit Function
End If
For i = 0 To UBound(s2)
If Len(s2(i)) = 1 Then s2(i) = "0" & s2(i)
st = st & s2(i)
Next i
Dim buff(7) As Byte
For i = 0 To 7
buff(i) = Asc(Mid(st, i + 1, 1))
Next i
Dim a(15) As Byte
For i = 0 To 15
a(i) = i
Next i
Dim b(7) As Byte
For j = 0 To 7
If buff(j) >= 48 And buff(j) <= 57 Then
k = buff(j) - 48
b(j) = a(k)
ElseIf buff(j) >= 65 And buff(j) <= 70 Then
k = buff(j) - 65
b(j) = a(k + 10)
Else
Exit Function
End If
Next j
Dim c(3) As Long
For n = 0 To 6 Step 2
c(n / 2) = ((b(n) And &H7F) * (2 ^ 4)) Or (b(n + 1) And &H7F)
Next n
s = c(0) / 2 ^ 7
If s = 1 Then
SS = 1
Else
SS = 1
End If
E = ((c(0) And &H7F) * 2) Or ((c(1) And &H80) / 2 ^ 7)
M = ((c(1) And &HFFFFFF) * 2 ^ 16 And &H7FFFFF) Or ((c(2) And &HFFFFFF) * 2 ^ 8) Or ((c(3) And &HFFFFFF))
d = SS * 2 ^ (E - 127) * (M / (2 ^ 23) + 1)
Str2Float = d
End Function
Private Sub comm_OnComm()
Dim temp() As Byte
Dim d1 As String
Dim d2 As String
Dim d3 As String
Dim d4 As String
Dim d5 As String
Dim d6 As String
Dim t1 As String
Dim t2 As String
Dim t3 As String
Dim t4 As String
Dim t5 As String
Dim t6 As String
With comm
Select Case .CommEvent
Case comEvReceive '对收到串口数据做处理
temp = .Input