⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds18b20.frm

📁 DS18B20温度监控程序
💻 FRM
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form Form1 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "PC机与单片机串口通信"
   ClientHeight    =   5130
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   7410
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   5130
   ScaleWidth      =   7410
   ShowInTaskbar   =   0   'False
   StartUpPosition =   3  '窗口缺省
   Begin MSFlexGridLib.MSFlexGrid Grid 
      Height          =   2415
      Left            =   3480
      TabIndex        =   13
      Top             =   240
      Width           =   1935
      _ExtentX        =   3413
      _ExtentY        =   4260
      _Version        =   393216
      Rows            =   10
   End
   Begin VB.CommandButton CmdEnd 
      Caption         =   "关闭"
      Height          =   495
      Left            =   6000
      TabIndex        =   12
      Top             =   2520
      Width           =   1215
   End
   Begin VB.CommandButton CmdStop 
      Caption         =   "停止采集"
      Height          =   495
      Left            =   6000
      TabIndex        =   11
      Top             =   1440
      Width           =   1215
   End
   Begin VB.Frame Frame1 
      Caption         =   "统计计算"
      Height          =   1335
      Left            =   120
      TabIndex        =   2
      Top             =   3120
      Width           =   4455
      Begin VB.TextBox maxText 
         Height          =   270
         Left            =   3240
         TabIndex        =   10
         Top             =   840
         Width           =   1095
      End
      Begin VB.TextBox averText 
         Height          =   270
         Left            =   3240
         TabIndex        =   9
         Top             =   360
         Width           =   1095
      End
      Begin VB.TextBox minText 
         Height          =   270
         Left            =   840
         TabIndex        =   8
         Top             =   840
         Width           =   1095
      End
      Begin VB.TextBox tempText 
         Height          =   270
         Left            =   840
         TabIndex        =   7
         Top             =   360
         Width           =   1095
      End
      Begin VB.Label Label4 
         Caption         =   "最大值:"
         Height          =   255
         Left            =   2280
         TabIndex        =   6
         Top             =   840
         Width           =   735
      End
      Begin VB.Label Label3 
         Caption         =   "最小值:"
         Height          =   375
         Left            =   120
         TabIndex        =   5
         Top             =   840
         Width           =   735
      End
      Begin VB.Label Label2 
         Caption         =   "平均值:"
         Height          =   375
         Left            =   2280
         TabIndex        =   4
         Top             =   360
         Width           =   855
      End
      Begin VB.Label Label1 
         Caption         =   "当前值:"
         Height          =   255
         Left            =   120
         TabIndex        =   3
         Top             =   360
         Width           =   735
      End
   End
   Begin VB.PictureBox Pic1 
      BackColor       =   &H00FFFFFF&
      Height          =   2535
      Left            =   120
      ScaleHeight     =   2475
      ScaleWidth      =   3195
      TabIndex        =   1
      Top             =   240
      Width           =   3255
   End
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Interval        =   1000
      Left            =   5760
      Top             =   4080
   End
   Begin VB.CommandButton CmdStart 
      Caption         =   "开始采集"
      Height          =   495
      Left            =   6000
      TabIndex        =   0
      Top             =   360
      Width           =   1215
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   6600
      Top             =   3960
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim datatemp(1000) As Single
Dim num As Integer
Private Sub Form_Load()
    MSComm1.Settings = "9600,n,8,1"     '设置数据格式
    MSComm1.CommPort = 1
    MSComm1.InputLen = 0
    MSComm1.InputMode = comInputModeBinary '二进制接收
    MSComm1.InBufferSize = 16          '设置缓冲区
    MSComm1.InBufferCount = 0
    MSComm1.OutBufferSize = 16
    MSComm1.OutBufferCount = 0
    MSComm1.RThreshold = 1            '每个字符都触发事件
    MSComm1.SThreshold = 1
    MSComm1.PortOpen = True
    
    Call tabinit
End Sub
Private Sub cmdstart_click()
    Timer1.Enabled = True
End Sub
Private Sub cmdstop_click()
    Timer1.Enabled = False
End Sub
Private Sub cmdend_click()
    End
End Sub
Private Sub timer1_timer()
    Dim outstring As String
    outstring = "ST"
    MSComm1.Output = outstring
End Sub
Private Sub MSComm1_OnComm()
    Dim i As Integer
    Dim Inbyte() As Byte
    Dim buffer, startbyte As String
    Dim data1, data2 As String
    Dim data As Single
    '***********************************
    If num > 199 Then Call renew
    Select Case MSComm1.CommEvent
        Case comEvReceive
            Inbyte = MSComm1.Input
            For i = LBound(Inbyte) To UBound(Inbyte)
                buffer = buffer + Hex(Inbyte(i)) + Chr(32)
            Next i
        Case Else
    End Select
    tempText.Text = buffer
    startbyte = Mid(buffer, 1, 2)
    If startbyte = "23" Then
        data1 = Mid(buffer, 4, 1)
        If Len(Trim(buffer)) = 6 Then        '内语句块
            data2 = Mid(buffer, 6, 1)
        Else: data2 = Mid(buffer, 6, 2)
        End If    '加上此句则else后面的语句在startbyte!=“23”时执行
        data = Val(data1) * 16 + Val("&H" & data2) / 16
        tempText.Text = Str(data)
    End If                                  '外语句块
    '************************************
    datatemp(num) = data
    If datatemp(num) <> 0 Then
        Grid.Col = 1: Grid.Row = num + 1
        Grid.Text = Str(datatemp(num))
        num = num + 1
        Call cal
        Call draw
   End If
End Sub
Sub cal()
    Dim sum, aver, min, max As Single
    Dim i As Integer
    sum = 0
    max = datatemp(0): min = 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.Text = Format$(max, "0.0")
    minText.Text = Format$(min, "0.0")
    averText.Text = Format$(aver, "0.0")
End Sub
Private Sub draw()              '画图子程序
    Dim i As Integer
    Dim X1, X2, Y1, Y2 As Integer
    Pic1.Cls
    Pic1.DrawWidth = 1
    Pic1.BackColor = QBColor(15)
    Pic1.Scale (0, 50)-(200, 0)  '重新定义图片框坐标
    For i = 1 To num - 1
        X1 = (i - 1): Y1 = datatemp(i - 1)
        X2 = i: Y2 = datatemp(i)
        Pic1.Line (X1, Y1)-(X2, Y2), QBColor(0)
    Next i
End Sub
Private Sub tabinit()       '表格初始化
    Dim i As Integer
    Grid.Cols = 2
    Grid.Rows = 200 + 1
    Grid.ColWidth(0) = 700: Grid.ColWidth(1) = 950
    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.TopRow = 1                             '从第一页开始
    Grid.LeftCol = 1
    num = 0
End Sub
Private Sub renew()        '刷新绘图区,表格显示区
    Dim i As Integer
    If num = 0 Then Exit Sub
    tempText.Text = "": averText.Text = ""
    minText.Text = "": maxText.Text = ""
    Grid.Clear
    Pic1.Cls
    For i = 0 To num - 1
        datatemp(i) = 0
    Next i
    num = 0
    Call tabinit
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -