步骤
1. 创建名为“Tag_1”的 WinCC 变量。
2.创建名为“ArTags”的过程值归档。将 WinCC 变量“Tag_1”与过程值归档连接起来。
3.创建 Visual Basic 项目。将 MS Windows Common Controls 6.0 的“ListView 控件”与名称“ListView1”连接起来。ListView 中的各列由脚本创建。
将“视图”属性设置为 ListView 控件的“3 - lvwReport”选项。
4.创建“CommandButton”。从下面的“实例脚本”一节中复制脚本。
5.在脚本中,将 WinCC 运行系统数据库的名称“CC_OpConPac_04_10_01_10_36_36R”更改为自己的项目。
数据库名称可在“SQL 企业管理器”中的“SQL Server 组”>“<计算机名称>\WinCC”>“数据库”>“<数据库名称_R>”处找到。
6.激活 WinCC 运行系统并启动 VB 应用程序。
3.单击“CommandButton”。
Option Explicit
Const NMAX = 100
Private Sub Command1_Click()
Dim sPro As String
Dim sDsn As String
Dim sSer As String
Dim sCon As String
Dim sSql As String
Dim conn As Object
Dim oRs As Object
Dim oCom As Object
Dim oItem As ListItem
Dim m, n, s, nRec
Dim strDateTime As String
Dim iMS As Long
'1.1 Make connection string for ADODB
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=CC_OpConPac_04_10_01_10_36_36R;"
sSer = "Data Source=.\WinCC"
sCon = sPro + sDsn + sSer
' 1.2 Define command text in sSql (relative time)
sSql = "TAG:R,'ArTags\Tag_1','0000-00-00 00:10:00.000','0000-00-00 00:00:00.000'"
MsgBox "Open with:"& vbCr & sCon & vbCr & sSql & vbCr
' 2.1 Make connection
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
' 2.2 Use command text for query
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Set oCom.ActiveConnection = conn
oCom.CommandText = sSql
' 2.3 Fill the recordset
Set oRs = oCom.Execute
m = oRs.Fields.Count
nRec = NMAX
' 3.0 Fill standard listview object with recordset
ListView1.ColumnHeaders.Clear
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(1).Name), 2500 ' DateTime
ListView1.ColumnHeaders.Add , , "MS", 1500 ' Milisecond
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(0).Name), 1500 ' DateTime
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(2).Name), 1500 ' DateTime
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(3).Name), 1500 ' DateTime
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(4).Name), 1500 ' DateTime
If (m > 0) Then
oRs.MoveFirst
n = 0
Do While (Not oRs.EOF And n < nRec)
n = n + 1
SplitDateTimeAndMs oRs.Fields(1).Value, strDateTime, iMS 'Split Milisecond from DateTime
s = FormatDateTime(strDateTime, 2) & " " & FormatDateTime(strDateTime, 3)
Set oItem = ListView1.ListItems.Add()
oItem.Text = s
oItem.SubItems(1) = iMS
oItem.SubItems(2) = oRs.Fields(0).Value
oItem.SubItems(3) = FormatNumber(oRs.Fields(2).Value, 4)
oItem.SubItems(4) = Hex(oRs.Fields(3).Value)
oItem.SubItems(5) = Hex(oRs.Fields(4).Value)
oRs.MoveNext
Loop
oRs.Close
Else
End If
Set oRs = Nothing
conn.Close
Set conn = Nothing
End Sub
Sub SplitDateTimeAndMs(dtDateTimeWithMs_in, strDateTime_out, strMs_out)
Dim diff
Dim dtTemp
Dim lTemp As Long
lTemp = 86400000 '24 * 60 * 60 * 1000
diff = (CDbl(dtDateTimeWithMs_in) - CDbl(CDate(CStr(dtDateTimeWithMs_in)))) * lTemp
If (diff < 0) Then
diff = 1000 + diff
dtTemp = CDbl(dtDateTimeWithMs_in) - diff / lTemp
strDateTime_out = CDate(dtTemp)
Else
strDateTime_out = CDate(dtDateTimeWithMs_in)
End If
strMs_out = "00" & Round(diff)
strMs_out = Right(strMs_out, 3)
End Sub