VB上位机温度采集(含源程序)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
VB程序
Option Explicit
Dim NowX As Integer
Dim MaxPlotNo As Long
Dim PreV alue As Single
Private Sub cmbCOM_Click()
If cmbCOM.ListIndex + 1 = mPort Then Exit Sub
Timer1.Enabled = False
TimeDelay 100
If MSComm1.PortOpen Then
MSComm1.PortOpen = False
End If
lblMsg.Caption = "已停止检测并关闭通讯端口"
cmdOpenCOM.Enabled = True
End Sub
Private Sub cmdEnd_Click()
End
End Sub
Private Sub cmdOpenCOM_Click()
If cmbCOM.ListIndex >= 0 And cmbCOM.ListIndex <= 16 Then
mPort = cmbCOM.ListIndex + 1
Else
MsgBox "指定通信端口时发生错误!", vbCritical + vbOKOnly, "系统信息"
Exit Sub
End If
On Error GoTo comErr
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
cmdOpenCOM.Enabled = False
cmdStart.Enabled = True
lblMsg.Caption = "可单击【开始检测】按钮,执行检测的工作。"
Exit Sub
comErr:
MsgBox "打开通信端口时发生错误!请确定通信端口存在且正常。", vbCritical + vbOKOnly, "系统信息"
End Sub
Private Sub cmdStart_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
cmdStart.Caption = "停止检测"
Else
cmdStart.Caption = "开始检测"
lblMsg.Caption = "已停止检测"
End If
End Sub
Private Sub Form_Load()
Dim i%
MaxPlotNo = 100
cmbCOM.Clear
cmbCOM.AddItem "COM1"
cmbCOM.AddItem "COM2"
cmbCOM.AddItem "COM3(USB)"
cmbCOM.AddItem "COM4(USB)"
cmbCOM.AddItem "COM5"
cmbCOM.AddItem "COM6"
cmbCOM.AddItem "COM7"
cmbCOM.AddItem "COM8"
cmbCOM.AddItem "COM9"
cmbCOM.AddItem "COM10"
cmbCOM.AddItem "COM11"
cmbCOM.AddItem "COM12"
cmbCOM.AddItem "COM12"
cmbCOM.AddItem "COM14"
cmbCOM.AddItem "COM15"
cmbCOM.AddItem "COM16"
cmbCOM.ListIndex = 0
cmdStart.Enabled = False
'以下设定绘图范围,(Xmin,YMax)-(XMax,YMin) picV oltage.Scale (0, 50)-(MaxPlotNo, 0)
picV oltage.DrawWidth = 2 '使用两个像素宽度的画笔End Sub
Private Sub Label2_Click()
End Sub
Private Sub lblValue_Click()
End Sub
Private Sub MSComm1_OnComm()
End Sub
Private Sub Timer1_Timer()
Dim Buf$, ValueStr As Single, Pos1%
Buf$ = Buf$ + MSComm1.Input
TimeDelay 2150
ValueStr = Val(Mid(Buf, Pos1 + 1, 7))
lblValue.Caption = Format(ValueStr, "00.0000") & "℃" If NowX = 0 Then
picVoltage.Cls
picVoltage.PSet (0, ValueStr)
Else
If ValueStr > PreV alue + 0.01 Then
picVoltage.Line -(NowX, ValueStr), RGB(255, 0, 0) Else
picVoltage.Line -(NowX, ValueStr), RGB(0, 0, 255) End If
End If
PreV alue = ValueStr
NowX = NowX + 1
If NowX > MaxPlotNo Then NowX = 0
End Sub