VB电机监测模拟程序
电机模拟程序
流程图:
关键代码: 1、在系统单击开始时,让计时器有效。 Private Sub cmdStart_Click() Call SetTimerEnable End Sub 设置“计时器”有效函数 Private Sub SetTimerUnenable() tmVoltage.Interval = Val(txtVoltageInterval.Text) tmVoltage.Enabled = False tmCurrent.Interval = Val(txtCurrentInterval.Text) tmCurrent.Enabled = False tmSpeed.Interval = Val(txtSpeedInterval.Text) tmSpeed.Enabled = False End Sub
2、在系统单击暂停时,让计时器无效。 Private Sub cmdStop_Click() Call SetTimerUnenable End Sub设置“计时器”无效函数: Private Sub SetTimerEnable() tmVoltage.Interval = Val(txtVoltageInterval.Text) tmVoltage.Enabled = True tmCurrent.Interval = Val(txtCurrentInterval.Text) tmCurrent.Enabled = True tmSpeed.Interval = Val(txtSpeedInterval.Text) tmSpeed.Enabled = True End Sub 3、系统退出时,保存数据到文件。 Private Sub cmdExit_Click() Call WriteToFile End Unload Me End Sub 保存数据到文件的函数。 Private Sub WriteToFile() Open "e:/a1.txt" For Output As #1 Open "e:/a2.txt" For Output As #2 Open "e:/a3.txt" For Output As #3
For i = 0 To lstVoltage.ListCount - 1 Write #1,lstVoltage.List(i) Next i
For i = 0 To lstCurrent.ListCount - 1 Write #2,lstCurrent.List(i) Next i
For i = 0 To lstSpeed.ListCount - 1 Write #3,lstSpeed.List(i) Next i
Close #1 Close #2 Close #3 End Sub 4、每个测试项目对应的函数。 Private Sub tmCurrent_Timer() '产生模拟电流值 Dim simuCurrent As Single
simuCurrent = 10 * Sqr(2) * Rnd
lblCurrent.Caption = simuCurrent
If simuCurrent > 10 Then lblIsCompensateCurr.Visible = False lblCurrent.ForeColor = RGB(255,0) End If
If simuVoltage < 7 Then compensation = 10 - simuCurrent simuCurrent = simuCurrent + Int(compensation) lblCurrent.Caption = Format(simuCurrent,"###.##") lblIsCompensateCurr.Visible = True lblCurrent.ForeColor = RGB(255,0) Else lblIsCompensateCurr.Visible = False lblCurrent.ForeColor = RGB(0,0) End If
lstCurrent.AddItem Format(simuCurrent,"###.##") & " " & DateTime.Time End Sub
Private Sub tmSpeed_Timer() '产生模拟转速值 Dim simuSpeed As Single
simuSpeed = 1000 * Sqr(2) * Rnd If simuSpeed < 800 Then simuSpeed = 1000 End If lblSpeed.Caption = simuSpeed
lstSpeed.AddItem Format(simuSpeed,"###.##") & " " & DateTime.Time End Sub
Private Sub tmVoltage_Timer() '产生模拟电压值 Dim simuVoltage As Single
simuVoltage = 220 * Sqr(2) * Rnd
lblVoltage.Caption = simuVoltage
If simuVoltage > 220 Then lblIsCompensation.Visible = False lblVoltage.ForeColor = RGB(255,0) End If
If simuVoltage < 200 Then compensation = 220 - simuVoltage simuVoltage = simuVoltage + Int(compensation) lblVoltage.Caption = Format(simuVoltage,"###.##") lblIsCompensation.Visible = True lblVoltage.ForeColor = RGB(255,0) Else lblIsCompensation.Visible = False lblVoltage.ForeColor = RGB(0,0) End If
lstVoltage.AddItem Format(simuVoltage,"###.##") & " " & DateTime.Time End Sub (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |