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, 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, 0)

    Else

        lblIsCompensateCurr.Visible = False

        lblCurrent.ForeColor = RGB(0, 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, 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, 0)

    Else

        lblIsCompensation.Visible = False

        lblVoltage.ForeColor = RGB(0, 0, 0)

    End If

   

    lstVoltage.AddItem Format(simuVoltage, "###.##") & "  " & DateTime.Time

End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值