|
DS18B20是一种很常用的数字温度传感器,温度检测范围是-55~+125度,手册 说明在-10~+85度范围内检测误差为±0.5度,作者在自己的产品中随机抽样验证 了几只传感器,在-25度误差0.1度,+25度与+50度误差小于0.1度,可见这种传感 器实际精度是很高的,传感器引脚如图19-1所示,工作电压范围是3.0~5.5V,通 常使用+5V,电源接反或接错一般是不会损坏传感器的,对于单只DS18B20的使用,我们按图19-2连接即可。
上位机vb电脑程序:
电路和介绍详见:http://www.51hei.com/bbs/dpj-47286-1.html(在这贴附件中可下载上位机vb及下位机单片机的完整的源码压缩包)
单片机下位机主程序如下(部分预览):
上位机电脑vb程序:
- Attribute VB_Name = "Form1"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- Option Explicit
- Dim MyForm As FRMSIZE '界面自动调整
- Dim DesignX As Long
- Dim DesignY As Long
- Dim Start_A As Boolean '左面绘图开始标志
- Dim Start_B As Boolean '右面绘图开始标志
- Dim FlagZuo As Integer '左边运行键按下标志
- Dim FlagYuo As Integer '右边运行键按下标志
- Dim DianHaoAZ As Integer '存放左波形横轴点号
- Dim DianHaoZ As Integer '存放当前的A点号
- Dim DianHaoAY As Integer '存放右波形横轴点号
- Dim DianHaoY As Integer '存放当前的A点号
- Dim timerz As Integer '定时器画波形_左
- Dim timery As Integer '定时器画波形_右
- Dim FLAG_A As Integer '延时结束标志(界面左右各一)
- Dim FLAG_B As Integer '延时结束标志(界面左右各一)
- Dim Zuo1_Liu As Single '1L额外处理(左用户流量)
- Dim You1_Liu As Single '1L额外处理(右用户流量)
- Dim Zuo1_Pin As Single '1L额外处理(左用户频率)
- Dim You1_Pin As Single '1L额外处理(右用户频率)
- Dim QianLiuLiang_Z As Single
- Dim QianLiuLiang_Y As Single
- Dim ADDROUT_A(1 To 1) As Byte
- Dim ADDROUT_B(1 To 6) As Byte
- Dim NA As Integer, NB As Integer '用于文本框显示数据(界面左右各一)
- Dim I, J, DBEGIN, WDEND, XSTEP, WDSTEP, KDSTEP As Long
-
- Dim PI As Single
- Private Sub Combo1_Click()
- Data1.DatabaseName = App.Path + "\水流量传感器.mdb"
- Data1.RecordSource = Combo1.Text
- Data1.Refresh
- Label12(0).Caption = "有输出"
- Label12(1).Caption = Data1.Recordset.Fields(2) & "-" & Data1.Recordset.Fields(3)
- Label12(2).Caption = Data1.Recordset.Fields(4) & "-" & Data1.Recordset.Fields(5)
- Label12(3).Caption = Data1.Recordset.Fields(6) & "-" & Data1.Recordset.Fields(7)
-
- End Sub
- Private Sub Combo2_Click()
- Data2.DatabaseName = App.Path + "\水流量传感器.mdb"
- Data2.RecordSource = Combo2.Text
- Data2.Refresh
- Label22(0).Caption = "有输出"
- Label22(1).Caption = Data2.Recordset.Fields(2) & "-" & Data2.Recordset.Fields(3)
- Label22(2).Caption = Data2.Recordset.Fields(4) & "-" & Data2.Recordset.Fields(5)
- Label22(3).Caption = Data2.Recordset.Fields(6) & "-" & Data2.Recordset.Fields(7)
-
- End Sub
- Private Sub Command1_Click(Index As Integer)
- Select Case Index
-
- Case 1 '暂 停
- If Command1(1).Caption = "暂 停" Then
- Command1(1).Caption = "继 续"
-
- If MSComm1.PortOpen = True Then
- MSComm1.PortOpen = False
- End If
-
- If MSCommAD1.PortOpen = True Then
- MSCommAD1.PortOpen = False
- End If
-
- Timer1.Enabled = False
- Else
- Command1(1).Caption = "暂 停"
- If MSComm1.PortOpen = False Then
- MSComm1.PortOpen = True
- End If
- If MSCommAD1.PortOpen = False Then
- MSCommAD1.PortOpen = True
- End If
- Timer1.Enabled = True
- End If
-
- Case 3 '退 出
- End
- End Select
- End Sub
- Private Sub Command2_Click(Index As Integer)
- Select Case Index
-
- Case 1 '暂 停
- If Command2(1).Caption = "暂 停" Then
- Command2(1).Caption = "继 续"
-
- If MSComm2.PortOpen = True Then
- MSComm2.PortOpen = False
- End If
- If MSCommAD2.PortOpen = True Then
- MSCommAD2.PortOpen = False
- End If
- Timer2.Enabled = False
- Else
- Command2(1).Caption = "暂 停"
- If MSComm2.PortOpen = False Then
- MSComm2.PortOpen = True
- End If
- If MSCommAD2.PortOpen = False Then
- MSCommAD2.PortOpen = True
- End If
- Timer2.Enabled = True
- End If
-
- Case 3 '退 出
- End
- End Select
-
- End Sub
- Private Sub Command3_Click()
- Data3.RecordSource = "流量记录表"
- Data3.Refresh
- Data3.Recordset.MoveFirst
- Do While Data3.Recordset.EOF = False
- Data3.Recordset.Delete
- Data3.Recordset.MoveNext
- Loop
- End Sub
- Private Sub Form_Activate()
- Call DRAWQX_A
- Call DRAWQX_B
-
- End Sub
- Private Sub DRAWQX_A()
- Pic2.DrawWidth = 1
- Pic2.DrawStyle = 0
-
- XSTEP = 2000 '曲线图 X 刻度步长
- WDSTEP = 1000 '曲线图 Y 刻度步长
- '*************************************************************************************
- '绘制 Y 刻度
-
- DBEGIN = 10000 '水平坐标轴向左终点(离图片框左边沿 5个小刻度)
- WDEND = 30000 '垂直坐标轴向下终点
- For I = 1 To 30
- If (I Mod 5) = 0 Then
- Pic2.Line (DBEGIN - 2400, WDEND - I * WDSTEP)-(DBEGIN, WDEND - I * WDSTEP)
- Else
- Pic2.Line (DBEGIN - 1200, WDEND - I * WDSTEP)-(DBEGIN, WDEND - I * WDSTEP)
- End If
- Next
-
- Pic2.Line (DBEGIN, 0)-(DBEGIN, WDEND) 'Y 轴线
-
- For I = 1 To 95 '绘制 X 刻度
- If (I Mod 5) = 0 Then
- Pic2.Line (DBEGIN + I * XSTEP, WDEND)-(DBEGIN + I * XSTEP, WDEND + 2400)
- Else
- Pic2.Line (DBEGIN + I * XSTEP, WDEND)-(DBEGIN + I * XSTEP, WDEND + 1200)
- End If
- Next
-
- Pic2.Line (DBEGIN, WDEND)-(200000, WDEND) 'X 轴线
-
- '*************************************************************************************
- '显示 X 坐标值
-
- For I = 0 To 18
- Pic2.CurrentX = I * (5 * XSTEP) + 5 * XSTEP - Pic2.TextWidth(Str(I * 20)) / 1.5
- Pic2.CurrentY = WDEND + 3 * WDSTEP
- Pic2.Print I * 20
- 'Pic2.PSet (I * (5 * XSTEP) + 5 * XSTEP, WDEND + 3 * WDSTEP) ' 确认X坐标是否准确(调试用)
- Next I
- Pic2.CurrentX = 19 * (5 * XSTEP) + 1 * XSTEP
- Pic2.CurrentY = WDEND + 3 * WDSTEP
- Pic2.Print "(ms)"
- '显示 Y 坐标值
- For I = 1 To 5
- Pic2.CurrentX = 500 '0.5 * XSTEP
- Pic2.CurrentY = (WDEND - I * 5 * WDSTEP) - Pic2.TextWidth(I) * 0.5
- Pic2.Print Format(I * 2.5, "0.00") '& "V"
- ' Pic2.PSet (4 * XSTEP, WDEND - I * 10 * WDSTEP) ' 确认Y坐标是否准确(调试用)
- Next I
- Pic2.CurrentX = 500
- Pic2.CurrentY = (WDEND - 29.5 * WDSTEP)
- Pic2.Print "(V)"
-
-
- '*************************************************************************************
-
- '*************************************************************************************
- '绘制网格线
-
- ' 要点 :X 坐标 = X*0.5*XSTEP + 5*XSTEP ' 5*XSTEP 处是X轴的0点,距左边5个步长(即5个刻度)
- ' Y 坐标 = WDEND - Y * 10 * WDSTEP ,10个小格步进值表示1度(10 * WDSTEP )
-
- Pic2.DrawStyle = 2 '点线
- For I = 1 To 19
- Pic2.Line (5 * I * XSTEP + 5 * XSTEP, WDEND)-(5 * I * XSTEP + 5 * XSTEP, 0) ' 垂直线
- Next
- For I = 1 To 5
- Pic2.Line (5 * XSTEP, WDEND - I * 5 * WDSTEP)-(95 * XSTEP + 5 * XSTEP, WDEND - I * 5 * WDSTEP) ' 水平线
- Next
-
- End Sub
- Private Sub DRAWQX_B()
- Pic4.DrawWidth = 1
- Pic4.DrawStyle = 0
-
- XSTEP = 2000 '曲线图 X 刻度步长
- WDSTEP = 1000 '曲线图 Y 刻度步长
- '*************************************************************************************
- '绘制 Y 刻度
-
- DBEGIN = 10000 '水平坐标轴向左终点(离图片框左边沿 10个小刻度)
- WDEND = 30000 '垂直坐标轴向下终点
- For I = 1 To 30
- If (I Mod 10) = 0 Then
- Pic4.Line (DBEGIN - 2400, WDEND - I * WDSTEP)-(DBEGIN, WDEND - I * WDSTEP)
- Else
- Pic4.Line (DBEGIN - 1200, WDEND - I * WDSTEP)-(DBEGIN, WDEND - I * WDSTEP)
- End If
- Next
-
- Pic4.Line (DBEGIN, 0)-(DBEGIN, WDEND) 'Y 轴线
-
- For I = 1 To 95 '绘制 X 刻度
- If (I Mod 5) = 0 Then
- Pic4.Line (DBEGIN + I * XSTEP, WDEND)-(DBEGIN + I * XSTEP, WDEND + 2400)
- Else
- Pic4.Line (DBEGIN + I * XSTEP, WDEND)-(DBEGIN + I * XSTEP, WDEND + 1200)
- End If
- Next
-
- Pic4.Line (DBEGIN, WDEND)-(200000, WDEND) 'X 轴线
-
- '*************************************************************************************
- '显示 X 坐标值
-
- For I = 0 To 18
- Pic4.CurrentX = I * (5 * XSTEP) + 5 * XSTEP - Pic4.TextWidth(Str(I * 20)) / 1.5
- Pic4.CurrentY = WDEND + 3 * WDSTEP
- Pic4.Print I * 20
- 'Pic4.PSet (I * (5 * XSTEP) + 5 * XSTEP, WDEND + 3 * WDSTEP) ' 确认X坐标是否准确(调试用)
- Next I
- Pic4.CurrentX = 19 * (5 * XSTEP) + 1 * XSTEP
- Pic4.CurrentY = WDEND + 3 * WDSTEP
- Pic4.Print "(ms)"
- '显示 Y 坐标值
- For I = 1 To 5
- Pic4.CurrentX = 500
- Pic4.CurrentY = (WDEND - I * 5 * WDSTEP) - Pic4.TextWidth(I) * 0.5
- Pic4.Print Format(I * 2.5, "0.00")
- ' Pic4.PSet (4 * XSTEP, WDEND - I * 10 * WDSTEP) ' 确认Y坐标是否准确(调试用)
- Next I
- Pic4.CurrentX = 500
- Pic4.CurrentY = (WDEND - 29.5 * WDSTEP)
- Pic4.Print "(V)"
-
- '*************************************************************************************
- '绘制网格线
-
- ' 要点 :X 坐标 = X*0.5*XSTEP + 5*XSTEP ' 5*XSTEP 处是X轴的0点,距左边5个步长(即5个刻度)
- ' Y 坐标 = WDEND - Y * 10 * WDSTEP ,10个小格步进值表示1度(10 * WDSTEP )
-
- Pic4.DrawStyle = 2 '点线
- For I = 1 To 19
- Pic4.Line (5 * I * XSTEP + 5 * XSTEP, WDEND)-(5 * I * XSTEP + 5 * XSTEP, 0) ' 垂直线
- Next
- For I = 1 To 5
- Pic4.Line (5 * XSTEP, WDEND - I * 5 * WDSTEP)-(95 * XSTEP + 5 * XSTEP, WDEND - I * 5 * WDSTEP) ' 水平线
- Next
-
- End Sub
- Private Sub Form_Load()
- If MSComm1.PortOpen = False Then
- MSComm1.PortOpen = True
-
- End If
-
- 'If MSComm2.PortOpen = False Then
- ' MSComm2.PortOpen = True
- ' End If
-
- ' If MSCommAD1.PortOpen = False Then '绘制波形部分(左边)
- ' MSCommAD1.PortOpen = True
- ' End If
- ' Start_A = False
- ' DianHaoAZ = 1
- ' DianHaoZ = 1
-
- 'If MSCommAD2.PortOpen = False Then '绘制波形部分(右边)
- ' MSCommAD2.PortOpen = True
- 'End If
- 'Start_B = False
- 'DianHaoAY = 1
- 'DianHaoY = 1
-
- Data1.DatabaseName = App.Path + "\水流量传感器.mdb"
- Data2.DatabaseName = App.Path + "\水流量传感器.mdb"
-
- Data3.DatabaseName = App.Path + "\水流量传感器.mdb"
- Data3.RecordSource = "左流量记录表"
- Data3.Refresh
- Do While Data3.Recordset.EOF = False
- Data3.Recordset.Delete
- Data3.Recordset.MoveNext
- Loop
-
- Data4.DatabaseName = App.Path + "\水流量传感器.mdb"
- Data4.RecordSource = "右流量记录表"
- Data4.Refresh
- Do While Data4.Recordset.EOF = False
- Data4.Recordset.Delete
- Data4.Recordset.MoveNext
- Loop
-
-
- '定义X,Y方向调整比例系数
- Dim ScaleFactorX As Single, ScaleFactorY As Single
- '定义设计初始采用的屏幕显示分辨率FORM的尺寸,以像素点为单位
-
- DesignX = 1440
- DesignY = 900
- RePosForm = True '定位FORM的标志
- DoResize = False 'RESIZE事件的标志
- '设置屏幕的缇(twips)值
- Xtwips = Screen.TwipsPerPixelX
- Ytwips = Screen.TwipsPerPixelY
- '设置屏幕X,Y方向的像素分辨率
- Xpixels = Screen.Width / Xtwips
- Ypixels = Screen.Height / Ytwips
- '定义调整比例系数
- ScaleFactorX = Xpixels / DesignX
- ScaleFactorY = Ypixels / DesignY
- Me.ScaleMode = vbTwips
- '调用重新调整子程序
- Resize_For_Resolution ScaleFactorX, ScaleFactorY, Me
- '保存当前屏幕的尺寸
- MyForm.Width = Me.Width
- MyForm.Height = Me.Height
-
- End Sub
- Private Sub Form_Resize()
- Dim ScaleFactorX As Single, ScaleFactorY As Single
- '避免死循环
- If Not DoResize Then
- DoResize = True
- Exit Sub
- End If
- RePosForm = False
- ScaleFactorX = Me.Width / MyForm.Width
- ScaleFactorY = Me.Height / MyForm.Height
- '重新调用调整子程序
- ' Resize_For_Resolution ScaleFactorX, ScaleFactorY, Me
- '保存当前屏幕的尺寸
- MyForm.Width = Me.Width
- MyForm.Height = Me.Height
-
-
- Pic2.ScaleWidth = 200000
- Pic2.ScaleHeight = 35000
-
-
- Pic4.ScaleWidth = 200000
- Pic4.ScaleHeight = 35000
-
- End Sub
- Private Sub MSCommAD1_OnComm() 'AD专用通道1
- Dim buffer() As Byte
- Static ReFlag As Boolean
- Dim DianYa As Single '存放当前的AD转换电压值
-
- Static DianYaA As Single '存放AD转换的旧电压值
-
- 'On Error GoTo err
-
- Select Case MSCommAD1.CommEvent
- Case comEvReceive
- buffer = MSCommAD1.Input
- If ReFlag = False Then
- If buffer(0) = &H55 Then
- 'Debug.Print "&0x55"
- MSCommAD1.InputLen = 470
- MSCommAD1.RThreshold = 470
- ReFlag = True
- End If
- Exit Sub
- End If
- Pic2.Cls
- Call DRAWQX_A
- For I = 0 To UBound(buffer)
- DianYa = buffer(I) * 29.99 / 255 '29.99是单片机基准电压需根据实际修改
-
- If DianHaoAZ = 1 Then
- Pic2.DrawStyle = 0
- Pic2.DrawWidth = 2
- Pic2.Line (DianHaoAZ * 0.2 * XSTEP + 5 * XSTEP, WDEND - DianYa * WDSTEP)-(DianHaoAZ * 0.2 * XSTEP + 5 * XSTEP, WDEND - DianYa * WDSTEP), RGB(0, 0, 255)
- Else
- Pic2.DrawStyle = 0
- Pic2.DrawWidth = 2
- Pic2.Line (DianHaoAZ * 0.2 * XSTEP + 5 * XSTEP, WDEND - DianYaA * WDSTEP)-(DianHaoZ * 0.2 * XSTEP + 5 * XSTEP, WDEND - DianYa * WDSTEP), RGB(0, 0, 255)
- End If
- '****************************************************************************
- DianYaA = DianYa '记录电压
- DianHaoAZ = DianHaoZ '记录点号
- DianHaoZ = DianHaoZ + 1
- Next
- DianHaoAZ = 1
- DianHaoZ = 1
- ReFlag = False
- timerz = 0
- MSCommAD1.InputLen = 0
- buffer = MSCommAD1.Input
- MSCommAD1.InputLen = 1
- End Select
- err:
- End Sub
- Private Sub MSCommAD2_OnComm() 'AD专用通道2
- Dim buffer() As Byte
- Static ReFlag As Boolean
- Dim DianYa As Single '存放当前的AD转换电压值
-
- Static DianYaA As Single '存放AD转换的旧电压值
-
- On Error GoTo err
-
- Select Case MSCommAD2.CommEvent
- Case comEvReceive
- buffer = MSCommAD2.Input
- If ReFlag = False Then
- If buffer(0) = &H55 Then
-
- MSCommAD2.InputLen = 470
- MSCommAD2.RThreshold = 470
- ReFlag = True
- End If
- Exit Sub
- End If
- Pic4.Cls
- Call DRAWQX_B
- For I = 0 To UBound(buffer)
- DianYa = buffer(I) * 29.99 / 255 '29.99是单片机基准电压需根据实际修改
-
- If DianHaoAY = 1 Then
- Pic4.DrawStyle = 0
- Pic4.DrawWidth = 2
- Pic4.Line (DianHaoAY * 0.2 * XSTEP + 5 * XSTEP, WDEND - DianYa * WDSTEP)-(DianHaoAY * 0.2 * XSTEP + 5 * XSTEP, WDEND - DianYa * WDSTEP), RGB(0, 0, 255)
- Else
- Pic4.DrawStyle = 0
- Pic4.DrawWidth = 2
- Pic4.Line (DianHaoAY * 0.2 * XSTEP + 5 * XSTEP, WDEND - DianYaA * WDSTEP)-(DianHaoY * 0.2 * XSTEP + 5 * XSTEP, WDEND - DianYa * WDSTEP), RGB(0, 0, 255)
- End If
- '****************************************************************************
- DianYaA = DianYa '记录电压
- DianHaoAY = DianHaoY '记录点号
- DianHaoY = DianHaoY + 1
- Next
- DianHaoAY = 1
- DianHaoY = 1
- ReFlag = False
- timery = 0
- MSCommAD2.InputLen = 0
- buffer = MSCommAD2.Input
- MSCommAD2.InputLen = 1
- End Select
- err:
- End Sub
- Private Sub Option1_Click()
- Pic4.ScaleWidth = 50000
- End Sub
- Private Sub Option2_Click()
- Pic4.ScaleWidth = 100000
- End Sub
- Private Sub Option3_Click()
- Pic2.ScaleWidth = 50000
- End Sub
- Private Sub Option4_Click()
- Pic2.ScaleWidth = 100000
- End Sub
- Private Sub Option5_Click()
- Pic2.ScaleWidth = 200000
- End Sub
- Private Sub Option6_Click()
- Pic4.ScaleWidth = 200000
- End Sub
- Private Sub Timer1_Timer()
- timerz = timerz + 1
- If timerz >= 10 Then
- timerz = 0
- Pic2.Cls
- Call DRAWQX_A
-
- Text1(0).Text = "" '更换下一个传感器中途清空数据
- Text2(2).Text = ""
- Text2(3).Text = ""
- Text4.Text = ""
-
- Text8.Text = ""
- Text9.Text = ""
- Text10.Text = ""
-
-
- Text3(0).Text = ""
- Text3(1).Text = ""
- Text3(2).Text = ""
- Text3(3).Text = ""
- Text3(0).BackColor = &H80000005
- Text3(1).BackColor = &H80000005
- Text3(2).BackColor = &H80000005
- Text3(3).BackColor = &H80000005
-
-
- Text1(0).BackColor = &HFFFFFF
- QianLiuLiang_Z = 0
-
-
- Zuo1_Liu = 0 '1L额外处理(左用户流量)
- Zuo1_Pin = 0 '1L额外处理(左用户频率)
-
- End If
- End Sub
- Private Sub Timer2_Timer()
- timery = timery + 1
- If timery >= 10 Then
- timery = 0
- Pic4.Cls
- Call DRAWQX_B
-
- Text1(1).Text = "" '更换下一个传感器中途清空数据
- Text2(4).Text = ""
- Text2(5).Text = ""
- Text6.Text = ""
-
- Text7.Text = ""
- Text11.Text = ""
- Text12.Text = ""
-
- Text5(0).Text = ""
- Text5(1).Text = ""
- Text5(2).Text = ""
- Text5(3).Text = ""
- Text5(0).BackColor = &H80000005
- Text5(1).BackColor = &H80000005
- Text5(2).BackColor = &H80000005
- Text5(3).BackColor = &H80000005
-
- Text1(1).BackColor = &HFFFFFF
- QianLiuLiang_Y = 0
-
- You1_Liu = 0 '1L额外处理(右用户流量)
- You1_Pin = 0 '1L额外处理(右用户频率)
-
- End If
- End Sub
- Private Sub YouXuanZe_Click()
-
- End Sub
- Private Sub ZuoXianZe_Click()
- End Sub
- Private Sub MSComm1_OnComm()
- Static CiShu As Integer '记录取平均值次数
- Static Shape_Flag(0 To 3) As Boolean
- Static XianShiCS As Integer '显示次数控制
- Dim buffer() As Byte
- Dim ZhouQuZhi As Long '真实周期值
- Dim PinLiZhi As Single '真实频率值
- Dim ZhouQu(5) As Long '接收到的频率字节
-
- 'On Error GoTo err
- Select Case MSComm1.CommEvent
- Case comEvReceive
- buffer = MSComm1.Input
- MSComm1.InBufferCount = 0
- MSComm1.OutBufferCount = 0
- ZhouQu(0) = buffer(0) '单片机通道信息(0-3)
- ZhouQu(1) = buffer(1)
- ZhouQu(2) = buffer(2)
- ZhouQu(3) = buffer(3)
- ZhouQu(4) = buffer(4)
-
- '************************************************************************************
- '-------------在这里加入校验
- '*************************************************************************************
-
-
- '************************************************************************************
- '接收到的字节转换成真实值
- '*************************************************************************************
- ZhouQuZhi = ZhouQu(1) * 16777216 + ZhouQu(2) * 65536 + ZhouQu(3) * 256 + ZhouQu(4)
- ZhouQuZhi = ZhouQuZhi * 12 / 11.0592 '单位:uS
- PinLiZhi = 1000000 / ZhouQuZhi
-
- If Shape_Flag(ZhouQu(0)) = False Then
- Shape_Flag(ZhouQu(0)) = True
- Shape1(ZhouQu(0)).FillColor = RGB(0, 255, 0)
- Else
- Shape_Flag(ZhouQu(0)) = False
- Shape1(ZhouQu(0)).FillColor = RGB(255, 255, 255)
- End If
- '*******************************************************************************
- '显示取平均值
- Data3.RecordSource = "左流量记录表"
- Data3.Refresh
-
- If PinLiZhi > 0 Then
- If CiShu Mod 4 = 0 Then
- Data3.Recordset.AddNew
- Data3.Recordset.Fields(ZhouQu(0)) = Format(PinLiZhi, "0.00")
- ElseIf CiShu < 4 Then
- Data3.Recordset.MoveFirst
- Data3.Refresh
- Data3.Recordset.Edit
- Data3.Recordset.Fields(ZhouQu(0)) = Format(PinLiZhi, "0.00")
- ElseIf CiShu > 4 And CiShu < 8 Then
- Data3.Recordset.MoveFirst
- Data3.Recordset.Move 1
- Data3.Recordset.Edit
- Data3.Recordset.Fields(ZhouQu(0)) = Format(PinLiZhi, "0.00")
-
- ElseIf CiShu > 8 And CiShu < 12 Then
- Data3.Recordset.MoveFirst
- Data3.Recordset.Move 2
- Data3.Recordset.Edit
- Data3.Recordset.Fields(ZhouQu(0)) = Format(PinLiZhi, "0.00")
-
- ElseIf CiShu > 12 And CiShu < 16 Then
- Data3.Recordset.MoveFirst
- Data3.Recordset.Move 3
- Data3.Recordset.Edit
- Data3.Recordset.Fields(ZhouQu(0)) = Format(PinLiZhi, "0.00")
- End If
- Data3.Recordset.Update
-
- CiShu = CiShu + 1
- If CiShu >= 4 * 4 Then '4行(4条记录)4列
- CiShu = 0
- Data3.RecordSource = "select avg(JiaoZhun10) as JZ10 FROM 左流量记录表 WHERE JiaoZhun10>0"
- Data3.Refresh
- Text2(0).Text = Format(Data3.Recordset("JZ10"), "0.00") & " Hz"
-
- Data3.RecordSource = "select avg(JiaoZhun50) as JZ50 FROM 左流量记录表 WHERE JiaoZhun50>0"
- Data3.Refresh
- Text2(1).Text = Format(Data3.Recordset("JZ50"), "0.00") & " Hz"
-
- Data3.RecordSource = "select avg(YongHu) as YH FROM 左流量记录表 WHERE YongHu>0"
- Data3.Refresh
- Text2(2).Text = Format(Data3.Recordset("YH"), "0.00") & " Hz"
-
- Data3.RecordSource = "select avg(LiuLiang) as LL FROM 左流量记录表 WHERE LiuLiang>0"
- Data3.Refresh
- Text2(3).Text = Format(Data3.Recordset("LL"), "0.00") & " Hz"
-
- Text4.Text = Format(60 * Val(Text2(3).Text) / 1609.954 * 0.94, "0.00") '显示当前流量值
-
-
- Data3.RecordSource = "左流量记录表"
- Data3.Refresh
- Data3.Recordset.MoveFirst
- Data3.Recordset.Delete
-
- CiShu = 12
-
- XianShiCS = XianShiCS + 1 '显示快慢控制
- If XianShiCS >= 3 Then
- XianShiCS = 0
- Text9.Text = Text2(2).Text
- Text10.Text = Text4.Text
- Text8.Text = Format(3600 * Val(Text2(3).Text) / 1609.954 * 0.94, "0.00") & " L/H"
- End If
- End If
- End If
-
- '*****************************************************************************************
-
- '合格判断
- '****************************************************************************************
- '界面上
- ' Abs(当前流量- 标准流量) <= 标准流量 * 2% And (Abs(当前流量- 标准流量) < Abs(前次流量-标准流量)) Then
- If Abs(Val(Text4.Text) - 1) <= 1 * 0.2 And (Abs(Val(Text4.Text) - 1) < Abs(QianLiuLiang_Z - 1)) Then
- Text3(0).Text = Text2(2).Text '1L/H
- End If
- If Abs(Val(Text4.Text) - 3) <= 3 * 0.03 And (Abs(Val(Text4.Text) - 3) < Abs(QianLiuLiang_Z - 3)) Then
- Text3(1).Text = Text2(2).Text '3L/H
- End If
- If Abs(Val(Text4.Text) - 6) <= 6 * 0.025 And (Abs(Val(Text4.Text) - 6) < Abs(QianLiuLiang_Z - 6)) Then
- Text3(2).Text = Text2(2).Text '5L/H
- End If
- If Abs(Val(Text4.Text) - 15) <= 15 * 0.015 And (Abs(Val(Text4.Text) - 15) < Abs(QianLiuLiang_Z - 15)) Then
- Text3(3).Text = Text2(2).Text '15L/H
- End If
-
- QianLiuLiang_Z = Val(Text4.Text) '记录本次流量值
-
- '********************************************************************************
-
- If Combo1.Text <> "请选择型号" Then
-
- Data1.RecordSource = Combo1.Text
- Data1.Refresh
-
- '1L/min额外处理
- If (Val(Text3(0).Text) > Data1.Recordset.Fields(0) And Val(Text3(0).Text) < Data1.Recordset.Fields(1)) = False Then
- If ZhouQu(0) = 2 Then '用户传感器左
- Zuo1_Pin = PinLiZhi
- End If
-
- If ZhouQu(0) = 3 Then '用户流量左
- Zuo1_Liu = Format(60 * PinLiZhi / 1609.954 * 0.94, "0.00")
- End If
- If Zuo1_Pin > 1 And Zuo1_Liu < 1.2 Then
- Text3(0).Text = Format(Zuo1_Pin, "0.00")
- If Val(Text4.Text) = 0 Then Text4.Text = Format(Zuo1_Liu, "0.00")
- End If
- End If
-
-
- '合格判断
- If Val(Text3(0).Text) <> 0 And Val(Text3(1).Text) <> 0 And Val(Text3(2).Text) <> 0 And Val(Text3(3).Text) <> 0 Then
- If Val(Text3(0).Text) > Data1.Recordset.Fields(0) And Val(Text3(0).Text) < Data1.Recordset.Fields(1) And _
- Val(Text3(1).Text) > Data1.Recordset.Fields(2) And Val(Text3(1).Text) < Data1.Recordset.Fields(3) And _
- Val(Text3(2).Text) > Data1.Recordset.Fields(4) And Val(Text3(2).Text) < Data1.Recordset.Fields(5) And _
- Val(Text3(3).Text) > Data1.Recordset.Fields(6) And Val(Text3(3).Text) < Data1.Recordset.Fields(7) Then
-
- Text1(0).Text = "合 格"
- Text1(0).BackColor = RGB(0, 255, 0)
- Text3(0).BackColor = &H80000005
- Text3(1).BackColor = &H80000005
- Text3(2).BackColor = &H80000005
- Text3(3).BackColor = &H80000005
- Else
- Text1(0).Text = "不 合 格"
- Text1(0).BackColor = RGB(255, 0, 0)
- If (Val(Text3(0).Text) > Data1.Recordset.Fields(0) And Val(Text3(0).Text) < Data1.Recordset.Fields(1)) = False Then Text3(0).BackColor = RGB(255, 0, 0)
- If (Val(Text3(1).Text) > Data1.Recordset.Fields(2) And Val(Text3(1).Text) < Data1.Recordset.Fields(3)) = False Then Text3(1).BackColor = RGB(255, 0, 0)
- If (Val(Text3(2).Text) > Data1.Recordset.Fields(4) And Val(Text3(2).Text) < Data1.Recordset.Fields(5)) = False Then Text3(2).BackColor = RGB(255, 0, 0)
- If (Val(Text3(3).Text) > Data1.Recordset.Fields(6) And Val(Text3(3).Text) < Data1.Recordset.Fields(7)) = False Then Text3(3).BackColor = RGB(255, 0, 0)
-
- End If
- End If
- End If
-
- '********************************************************************************
-
-
- End Select
- 'err: Exit Sub
- End Sub
- Private Sub MSComm2_OnComm()
- Static CiShu As Integer '记录取平均值次数
- Static Shape_Flag(0 To 3) As Boolean
- Static XianShiCS As Integer '显示次数控制
- Dim buffer() As Byte
- Dim ZhouQuZhi As Long '真实周期值
- Dim PinLiZhi As Single '真实频率值
- Dim ZhouQu(5) As Long '接收到的频率字节
-
- On Error GoTo err
- Select Case MSComm2.CommEvent
- Case comEvReceive
- buffer = MSComm2.Input
- MSComm2.InBufferCount = 0
- MSComm2.OutBufferCount = 0
- ZhouQu(0) = buffer(0) '单片机通道信息(0-3)
- ZhouQu(1) = buffer(1)
- ZhouQu(2) = buffer(2)
- ZhouQu(3) = buffer(3)
- ZhouQu(4) = buffer(4)
-
- '************************************************************************************
- '-------------在这里加入校验
- '*************************************************************************************
-
- '************************************************************************************
- '接收到的字节转换成真实值
- '*************************************************************************************
- ZhouQuZhi = ZhouQu(1) * 16777216 + ZhouQu(2) * 65536 + ZhouQu(3) * 256 + ZhouQu(4)
- ZhouQuZhi = ZhouQuZhi * 12 / 11.0592 '单位:uS
- PinLiZhi = 1000000 / ZhouQuZhi
-
- If Shape_Flag(ZhouQu(0)) = False Then
- Shape_Flag(ZhouQu(0)) = True
- Shape2(ZhouQu(0)).FillColor = &HFF80FF 'RGB(200, 200, 0)
- Else
- Shape_Flag(ZhouQu(0)) = False
- Shape2(ZhouQu(0)).FillColor = RGB(255, 255, 255)
- End If
-
- '*******************************************************************************
- '显示取平均值
- Data4.RecordSource = "右流量记录表"
- Data4.Refresh
-
- If PinLiZhi > 0 Then
- If CiShu Mod 4 = 0 Then
- Data4.Recordset.AddNew
- Data4.Recordset.Fields(ZhouQu(0)) = Format(PinLiZhi, "0.00")
-
- ElseIf CiShu < 4 Then
- Data4.Recordset.MoveFirst
- Data4.Refresh
- Data4.Recordset.Edit
- Data4.Recordset.Fields(ZhouQu(0)) = Format(PinLiZhi, "0.00")
-
- ElseIf CiShu > 4 And CiShu < 8 Then
- Data4.Recordset.MoveFirst
- Data4.Recordset.Move 1
- Data4.Recordset.Edit
- Data4.Recordset.Fields(ZhouQu(0)) = Format(PinLiZhi, "0.00")
-
- ElseIf CiShu > 8 And CiShu < 12 Then
- Data4.Recordset.MoveFirst
- Data4.Recordset.Move 2
- Data4.Recordset.Edit
- Data4.Recordset.Fields(ZhouQu(0)) = Format(PinLiZhi, "0.00")
-
- ElseIf CiShu > 12 And CiShu < 16 Then
- Data4.Recordset.MoveFirst
- Data4.Recordset.Move 3
- Data4.Recordset.Edit
- Data4.Recordset.Fields(ZhouQu(0)) = Format(PinLiZhi, "0.00")
- End If
- Data4.Recordset.Update
-
- CiShu = CiShu + 1
- If CiShu >= 4 * 4 Then '4行(4条记录)4列
- CiShu = 0
- Data4.RecordSource = "select avg(JiaoZhun10) as JZ10 FROM 右流量记录表 WHERE JiaoZhun10>0"
- Data4.Refresh
- Text2(6).Text = Format(Data4.Recordset("JZ10"), "0.00") & " Hz"
-
- Data4.RecordSource = "select avg(JiaoZhun50) as JZ50 FROM 右流量记录表 WHERE JiaoZhun50>0"
- Data4.Refresh
- Text2(7).Text = Format(Data4.Recordset("JZ50"), "0.00") & " Hz"
-
- Data4.RecordSource = "select avg(YongHu) as YH FROM 右流量记录表 WHERE YongHu>0"
- Data4.Refresh
- Text2(4).Text = Format(Data4.Recordset("YH"), "0.00") & " Hz"
-
- Data4.RecordSource = "select avg(LiuLiang) as LL FROM 右流量记录表 WHERE LiuLiang>0"
- Data4.Refresh
- Text2(5).Text = Format(Data4.Recordset("LL"), "0.00") & " Hz"
-
-
- Text6.Text = Format(60 * Val(Text2(5).Text) / 1608.987 * 0.94, "0.00") '显示当前流量值
- ' Text7.Text = Format(3600 * Val(Text2(5).Text) / 1608.987 * 0.94, "0.00") & " L/H"
-
- Data4.RecordSource = "右流量记录表" '前面Data4.RecordSource<>"右流量记录表",所有必须有此句
- Data4.Refresh '
- Data4.Recordset.MoveFirst
- Data4.Recordset.Delete
- CiShu = 12
-
- XianShiCS = XianShiCS + 1 '显示快慢控制
- If XianShiCS >= 3 Then
- XianShiCS = 0
- Text11.Text = Text2(4).Text
- Text12.Text = Text6.Text
- Text7.Text = Format(3600 * Val(Text2(5).Text) / 1608.987 * 0.94, "0.00") & " L/H"
- End If
- End If
- End If
- '*****************************************************************************************
- '合格判断
- '****************************************************************************************
-
- '界面下
- If Abs(Val(Text6.Text) - 1) <= 1 * 0.2 And (Abs(Val(Text6.Text) - 1) < Abs(QianLiuLiang_Y - 1)) Then Text5(0).Text = Text2(4).Text '1L/H
- If Abs(Val(Text6.Text) - 3) <= 3 * 0.03 And (Abs(Val(Text6.Text) - 3) < Abs(QianLiuLiang_Y - 3)) Then Text5(1).Text = Text2(4).Text '3L/H
- If Abs(Val(Text6.Text) - 6) <= 6 * 0.025 And (Abs(Val(Text6.Text) - 6) < Abs(QianLiuLiang_Y - 6)) Then Text5(2).Text = Text2(4).Text '5L/H
- If Abs(Val(Text6.Text) - 15) <= 15 * 0.015 And (Abs(Val(Text6.Text) - 15) < Abs(QianLiuLiang_Y - 15)) Then Text5(3).Text = Text2(4).Text '15L/H
-
- QianLiuLiang_Y = Val(Text6.Text) '记录本次流量值
-
- '********************************************************************************
-
-
- If Combo2.Text <> "请选择型号" Then
- Data2.RecordSource = Combo2.Text
- Data2.Refresh
- '1L/min额外处理
- If (Val(Text5(0).Text) > Data2.Recordset.Fields(0) And Val(Text5(0).Text) < Data2.Recordset.Fields(1)) = False Then 'Text5(0).Text为空或平均值不合格
- If ZhouQu(0) = 2 Then '用户传感器
- You1_Pin = PinLiZhi
- End If
-
- If ZhouQu(0) = 3 Then '用户流量
- You1_Liu = Format(60 * PinLiZhi / 1608.987 * 0.94, "0.00")
- End If
-
- If You1_Pin > 1 And You1_Liu < 1.2 Then
- Text5(0).Text = Format(You1_Pin, "0.00")
- If Val(Text6.Text) = 0 Then Text6.Text = Format(You1_Liu, "0.00") 'Text6(0).Text为空
- End If
- End If
-
- If Val(Text5(0).Text) <> 0 And Val(Text5(1).Text) <> 0 And Val(Text5(2).Text) <> 0 And Val(Text5(3).Text) <> 0 Then
- If Val(Text5(0).Text) > Data2.Recordset.Fields(0) And Val(Text5(0).Text) < Data2.Recordset.Fields(1) And _
- Val(Text5(1).Text) > Data2.Recordset.Fields(2) And Val(Text5(1).Text) < Data2.Recordset.Fields(3) And _
- Val(Text5(2).Text) > Data2.Recordset.Fields(4) And Val(Text5(2).Text) < Data2.Recordset.Fields(5) And _
- Val(Text5(3).Text) > Data2.Recordset.Fields(6) And Val(Text5(3).Text) < Data2.Recordset.Fields(7) Then
-
- Text1(1).Text = "合 格"
- Text1(1).BackColor = RGB(0, 255, 0)
- Text5(0).BackColor = &H80000005
- Text5(1).BackColor = &H80000005
- Text5(2).BackColor = &H80000005
- Text5(3).BackColor = &H80000005
- Else
- Text1(1).Text = "不 合 格"
- Text1(1).BackColor = RGB(255, 0, 0)
- If (Val(Text5(0).Text) > Data2.Recordset.Fields(0) And Val(Text5(0).Text) < Data2.Recordset.Fields(1)) = False Then Text5(0).BackColor = RGB(255, 0, 0)
- If (Val(Text5(1).Text) > Data2.Recordset.Fields(2) And Val(Text5(1).Text) < Data2.Recordset.Fields(3)) = False Then Text5(1).BackColor = RGB(255, 0, 0)
- If (Val(Text5(2).Text) > Data2.Recordset.Fields(4) And Val(Text5(2).Text) < Data2.Recordset.Fields(5)) = False Then Text5(2).BackColor = RGB(255, 0, 0)
- If (Val(Text5(3).Text) > Data2.Recordset.Fields(6) And Val(Text5(3).Text) < Data2.Recordset.Fields(7)) = False Then Text5(3).BackColor = RGB(255, 0, 0)
-
- End If
- End If
- End If
-
- '********************************************************************************
-
- End Select
- err: Exit Sub
- End Sub
复制代码
|
|