| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1476 人关注过本帖
标题:关于与单片机通讯出现的问题
只看楼主 加入收藏
cxb_880905
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-1-12
结帖率:0
收藏
已结贴  问题点数:10 回复次数:1 
关于与单片机通讯出现的问题
本人是自学的,现在在练习写一个程序,单片机通过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,请各位大神指导指导


搜索更多相关主题的帖子: 单片机 False 
2016-01-12 10:44
hjxlj
Rank: 10Rank: 10Rank: 10
来 自:江西
等 级:贵宾
威 望:14
帖 子:292
专家分:1519
注 册:2013-6-25
收藏
得分:10 
这个和下位机的代码也有关系,建议加我企鹅号775420425沟通交流。

本人QQ:775420425
2016-01-12 15:08
快速回复:关于与单片机通讯出现的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037010 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved