VB上位机温度采集(共5页).doc
精选优质文档-倾情为你奉上VB程序Option ExplicitDim NowX As Integer Dim MaxPlotNo As Long Dim PreValue As Single Private Sub cmbCOM_Click() If cmbCOM.ListIndex + 1 = MSComm1.CommPort Then Exit Sub Timer1.Enabled = False TimeDelay 100 If MSComm1.PortOpen ThenMSComm1.PortOpen = False End If lblMsg.Caption = "已停止检测并关闭通讯端口" cmdOpenCOM.Enabled = True End SubPrivate Sub cmdEnd_Click() EndEnd SubPrivate Sub cmdOpenCOM_Click() If cmbCOM.ListIndex >= 0 And cmbCOM.ListIndex <= 16 Then MSComm1.CommPort = 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 SubcomErr: MsgBox "打开通信端口时发生错误!请确定通信端口存在且正常。", vbCritical + vbOKOnly, "系统信息"End SubPrivate Sub cmdStart_Click() Timer1.Enabled = Not Timer1.Enabled If Timer1.Enabled Then cmdStart.Caption = "停止检测" Else cmdStart.Caption = "开始检测" lblMsg.Caption = "已停止检测" End IfEnd SubPrivate 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) picVoltage.Scale (0, 50)-(MaxPlotNo, 0) picVoltage.DrawWidth = 2 '使用两个像素宽度的画笔End SubPrivate Sub Label2_Click()End SubPrivate Sub lblValue_Click()End SubPrivate Sub MSComm1_OnComm()End SubPrivate 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 > PreValue + 0.01 Then picVoltage.Line -(NowX, ValueStr), RGB(255, 0, 0) Else picVoltage.Line -(NowX, ValueStr), RGB(0, 0, 255) End If End If PreValue = ValueStr NowX = NowX + 1 If NowX > MaxPlotNo Then NowX = 0 End Sub专心-专注-专业