25.使用Wincc V7.3 VBS做一个秒表的功能
2016-04-08 11:17阅读:
西门子技术论坛上,有人发帖子询问如何在Wincc上面做一个秒表功能,按下开始按钮秒表计时,按下停止功能停止计时,按下复位功能,计数回零。自己测试了一下,这个还是比较容易实现的,现在把实现的过程分享一下。
一 准备工作
1. 建立变量
打开wincc,新建一个工程,建立以下内部变量:
MyHour
32位无符号数;
MyMinute
32位无符号数;
MySecond 32位无符号数;
Start
二进制变量;
Timers
文本变量8位字符集;
2. 画面准备
新建一幅画面main.pdl,上面放置一个输入输出域控件,选择输出类型,绑定变量Timers,显示计时信息;再放置上个按钮,分别是“开始”、“停止”、“复位”。
3. 系统准备
wincc项目管理器-计算机属性,启动选项卡勾选全局脚本。
二 脚本
1. 全局脚本
新建一个stopwatch的VBS动作,循环触发器,频率1秒,脚本如下:
Option Explicit
Function action
Dim fmtHour,fmtMinute,fmtSecond
If HMIRuntime.Tags('Start').Read =1 Then
If HMIRuntime.Tags('MySecond').read<59 Then
HMIRuntime.Tags('MySecond').Write HMIRuntime.Tags('MySecond').read
+ 1
Else
If
HMIRuntime.Tags('MyMinute').read<59 Then
HMIRuntime.Tags('MyMinute').Write HMIRuntime.Tags('MyMinute').read
+ 1
Else
HMIRuntime.Tags('MyMinute').Write 0
HMIRuntime.Tags('MyHour').Write HMIRuntime.Tags('MyHour').read +
1
End If
HMIRuntime.Tags('MySecond').Write
0
End If
'If HMIRuntime.Tags('MyMinute').read=60 Then
' HMIRuntime.Tags('MyHour').Write
HMIRuntime.Tags('MyHour').read + 1
' HMIRuntime.Tags('MyMinute').Write 0
'End If
If HMIRuntime.Tags('MySecond').read<10 Then
fmtSecond='0' &
CStr(HMIRuntime.Tags('MySecond').read)
Else
fmtSecond=
CStr(HMIRuntime.Tags('MySecond').read)
End If
If HMIRuntime.Tags('MyMinute').read<10 Then
fmtMinute='0' &
CStr(HMIRuntime.Tags('MyMinute').read)
Else
fmtMinute=
CStr(HMIRuntime.Tags('MyMinute').read)
End If
If HMIRuntime.Tags('MyHour').read<10 Then
fmtHour='0' &
CStr(HMIRuntime.Tags('MyHour').read)
Else
fmtHour=
CStr(HMIRuntime.Tags('MyHour').read)
End If
HMIRuntime.Tags('Timers').Write fmtHour & ':' &
fmtMinute & ':' & fmtSecond
End If
End Function
2. 启动按钮鼠标点击事件将常数1复制给内部变量Start
3. 停止按钮鼠标点击事件脚本
Sub OnClick(ByVal Item)
HMIRuntime.Tags('Start').Write 0
End Sub
4. 复位按钮鼠标点击事件脚本
Sub OnClick(Byval Item)
HMIRuntime.Tags('Start').Write 0
HMIRuntime.Tags('MyHour').Write 0
HMIRuntime.Tags('MyMinute').Write 0
HMIRuntime.Tags('MySecond').Write 0
HMIRuntime.Tags('Timers').Write
'00:00:00'
End Sub
做完上述工作保存运行就可以看到效果了,如果想加入毫秒信息,那么需要在项目属性更新周期选项卡中修改一个用户周期为100ms,这是wincc最小的频率,然后修改相应的全集脚本,就可以把毫秒值加入了,这里就不再详细阐述了。