关于与单片机通讯出现的问题
本人是自学的,现在在练习写一个程序,单片机通过DS18B20采集环境温度,然后传输数据给上位机,界面如下[local]1[/local]
程序如下
Dim datatemp(1000) As Single
Dim num As Integer
Private Sub Cmdquit_Click()
MSComm1.PortOpen = False
Unload Me
End Sub
Private Sub Form_Load()
With MSComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.InputMode = 1
.PortOpen = True
End With
alarm1.FillColor = QBColor(10)
alarm2.FillColor = QBColor(10)
Call tabinit
End Sub
Public Sub tabinit()
Grid.Cols = 3
Grid.Rows = 201
Grid.ColWidth(0) = 700
Grid.ColWidth(1) = 1500
Grid.ColWidth(2) = 1500
Grid.Col = 0
For i = 1 To 200
Grid.Row = i
Grid.Text = " " + Str$(i)
Next i
Grid.Row = 0
Grid.Col = 0: Grid.Text = " 序号"
Grid.Col = 1: Grid.Text = " 温度值(十六进制)"
Grid.Col = 2: Grid.Text = " 温度值十进制)"
Grid.TopRow = 1 '第一行置顶
Grid.LeftCol = 1 '置在第一页
End Sub
Private Sub Timer1_Timer()
Dim inbyte() As Byte
Dim buffer As String
inbyte = MSComm1.Input
For i = LBound(inbyte) To UBound(inbyte)
buffer = buffer + Hex(inbyte(i)) + Chr(32)
Next i
Dim datastr(20) As String
For i = 1 To 5
datastr(i) = Mid(Trim(buffer), i, 1)
Next i
If Len(Trim(buffer)) > 2 Then
If datastr(2) = "" And datastr(4) <> "" Then
datatemp(num) = (Val("&H" & "0") * (16 ^ 3) + Val("&H" & datastr(1)) * (16 ^ 2) + Val("&H" & datastr(3)) * (16 ^ 1) + Val("&H" & datastr(4)) * (16 ^ 0)) * 0.1
End If
If datastr(2) = "" And datastr(4) = "" Then
datatemp(num) = (Val("&H" & "0") * (16 ^ 3) + Val("&H" & datastr(1)) * (16 ^ 2) + Val("&H" & "0") * (16 ^ 1) + Val("&H" & datastr(3)) * (16 ^ 0)) * 0.1
End If
If datastr(3) = "" And datastr(5) <> "" Then
datatemp(num) = (Val("&H" & datastr(1)) * (16 ^ 3) + Val("&H" & datastr(2)) * (16 ^ 2) + Val("&H" & datastr(4)) * (16 ^ 1) + Val("&H" & datastr(5)) * (16 ^ 0)) * 0.1
End If
If datastr(3) = "" And datastr(5) = "" Then
datatemp(num) = (Val("&H" & datastr(1)) * (16 ^ 3) + Val("&H" & datastr(2)) * (16 ^ 2) + Val("&H" & "0") * (16 ^ 1) + Val("&H" & datastr(4)) * (16 ^ 0)) * 0.1
End If
ElseIf Len(Trim(buffer)) = 2 Then
datatemp(num) = (Val("&H" & "0") * (16 ^ 3) + Val("&H" & "0") * (16 ^ 2) + Val("&H" & datastr(1)) * (16 ^ 1) + Val("&H" & datastr(2)) * (16 ^ 0)) * 0.1
ElseIf Len(Trim(buffer)) = 1 Then
datatemp(num) = (Val("&H" & "0") * (16 ^ 3) + Val("&H" & "0") * (16 ^ 2) + Val("&H" & "0") * (16 ^ 1) + Val("&H" & datastr(4)) * (16 ^ 0)) * 0.1
End If
Grid.Row = num + 1
Grid.Col = 1: Grid.Text = buffer
Grid.Col = 2: Grid.Text = Format(datatemp(num), "0.0")
TempText(0).Text = Format(datatemp(num), "0.0")
Call alarm
num = num + 1
Call cal
Call draw
End Sub
Sub alarm()
If datatemp(num) < 25 Then
alarm1.FillColor = QBColor(12)
alarm2.FillColor = QBColor(10)
End If
If datatemp(num) >= 25 And datatemp(num) <= 50 Then
alarm1.FillColor = QBColor(10)
alarm2.FillColor = QBColor(10)
End If
If datatemp(num) >= 50 Then
alarm1.FillColor = QBColor(10)
alarm2.FillColor = QBColor(12)
End If
End Sub
Sub cal()
On Error GoTo hh
Sum = 0
Max = datatemp(0): Mim = Max
For i = 0 To num - 1
If datatemp(i) >= Max Then Max = datatemp(i)
If datatemp(i) <= Min Then Min = datatemp(i)
Sum = Sum + datatemp(i)
Next i
aver = Sum / num
MaxText(3).Text = Format$(Max, "0.0")
MinText(1).Text = Format$(Min, "0.0")
AverText(2).Text = Format$(aver, "0.0")
hh:
Exit Sub
End Sub
Private Sub draw()
Picture1.Cls
Picture1.DrawWidth = 1
Picture1.BackColor = QBColor(15)
Picture1.Scale (0, 100)-(200, 0)
For i = 1 To num - 1
X1 = (i - 1): Y1 = datatemp(i - 1)
X2 = i: Y2 = datatemp(i)
Picture1.Line (X1, Y1)-(X2, Y2), QBColor(0)
Next i
End Sub
现在出现了一个问题
[local]5[/local]
温度值(16进制)有接收到数据,数值也是正确的,但是温度值(10进制)那一栏都是“0.0”,另外当前值,最大值,最小值,平均值也都是显示“0.0”,找不到是哪里出了BUG,请各位大神指导指导