WinCC控件编程技巧记录

  • 格式:doc
  • 大小:39.50 KB
  • 文档页数:4

WinCC控件编程技巧记录
1. PLC上传变量读写
变量读示例:HMIRuntime.Tags("CtrlPG/Perf_DataBuff.P02e_N").Read
变量写示例:HMIRuntime.Tags("CtrlPG/Perf_DataBuff.P02e").Write 3.2

2. 控件属性读写
VBScript程序:
ScreenItems("IOField4").OutputValue = h & ":" & m & ":" & s
解释:IOField控件的OutputValue属性显示运行时间

3. 生成Excel报表
VBScript程序:
Dim xls
Dim strDT
Dim RptTime

Set xls = CreateObject("Excel.Application")
xls.Workbooks.Open ("E:\Form\报表格式.xls")

xls.Cells(2, 2).Value = Year(Date) & "/" & Month(Date) & "/" & Day(Date)
xls.Cells(2, 5).Value = Hour(Time) & ":" & Minute(Time) & ":" &
Second(Time)

strDT = "(" & Year(Date) & "-" & Month(Date) & "-" & Day(Date) & " " &
Hour(Time) & "." & Minute(Time) & "." & Second(Time) & ")"
HMIRuntime.Tags("RptTime").Write strDT
RptTime = strDT

xls.Workbooks(1).SaveAs ("E:\报表\ " & "报表" & RptTime & ".xls")
xls.Workbooks(1).Close
xls.Quit
Set xls = Nothing

解释:脚本打开硬盘上excel报表格式,写入时间日期,写入数据(程序省略),
另存为文件名包含时间的excel报表。
4. 打开Excel报表
VBScript程序:
Dim CommDlg,xls
Set CommDlg = ScreenItems("控件1")
CommDlg.InitDir = "E:\报表"
CommDlg.DialogTitle = "请选择一个数据文件"
CommDlg.Filter = "xls文件(*.xls)|*.xls|所有文件(.*)|*.*"
CommDlg.FileName = ""
CommDlg.ShowOpen
Dim fileName
fileName = CommDlg.fileName
If Trim(fileName) = "" Then Exit Sub
Set xls = CreateObject("Excel.Application")
'xls.Visible = True
xls.Workbooks.Open (fileName)
xls.Application.Visible = True
xls.Workbooks(1).PrintPreview
xls.Workbooks(1).Close
xls.Quit
Set xls = Nothing

解释:首先WinCC画面需添加CommonDialog控件,从画面编辑,控件选项栏
添加。

5. 预览Excel报表
VBScript程序:
Dim xls
Dim RptTime

RptTime = HMIRuntime.Tags("RptTime").Read
Set xls = CreateObject("Excel.Application")
xls.Workbooks.Open ("E:\报表\报表" & RptTime & ".xls")

xls.Application.Visible = True
xls.Workbooks(1).PrintPreview
xls.Workbooks(1).Close
xls.Quit
Set xls = Nothing
解释:根据生成excel报表时保存的时间变量RptTime,预览最近一次保存的
excel报表。

6. 打印Excel报表
VBScript程序:
Dim xls
Dim RptTime

RptTime = HMIRuntime.Tags("RptTime").Read
Set xls = CreateObject("Excel.Application")
xls.Workbooks.Open ("E:\报表\报表" & RptTime & ".xls")

xls.Workbooks(1).PrintOut
xls.Workbooks(1).Close
xls.Quit
Set xls = Nothing

解释:根据生成excel报表时保存的时间变量RptTime,打印最近一次保存的
excel报表。

7 运行时间显示
VBScript程序:
Dim t,h,m,s
t = HMIRuntime.Tags("CtrlPG/CON_DB.TurbWorkTime").Read/20
h = Int(t/3600)
t = t Mod 3600
m = Int(t/60)
s = t Mod 60
ScreenItems("IOField4").OutputValue = h & ":" & m & ":" & s

解释:下位程序燃机运行时间累计周期为50ms,所以计数器要除以20。IOField
控件显示运行时间,格式为“时:分:秒”

8 采集记录周期
上位画面刷新周期最短为250ms
趋势记录最短周期为500ms
项目前期设计时需考虑HMI数据刷新记录周期是否满足客户要求。