2017-11-10 448 views
0

我正在嘗試創建VBscript以從SCADA系統(WinCC RT Professional)導出過程數據以定期存檔所有過程變量。數據存儲在可通過連接包訪問的SQL表中。我設法在導出一個標籤(過程變量)時使腳本工作,但我想循環系統中的所有標籤(大約60),將它們收集到另一個記錄集中,然後將此記錄集中的所有數據保存在一個csv-文件。我創建了RecSet,它收集一個標記(時間,過程變量等)的所有變量(字段),我只需要來自字段4的值(對於所有標記都是相同的字段)。然後,我想將此字段複製到另一個記錄集中 - RecSetColl從所有標記中收集所有必需數據(字段4),最後將它們保存在CSV文件中。非常感謝您的幫助。從SCADA系統導出數據

Sub DataExport() 

Dim fso   'FileSystemObject 
Dim f   'File 
Dim ts   'TextStream 
Dim path  'Path 
Dim ArchiveDate 'Archive date 

'Name of CSV-file 
ArchiveDate = ArchiveDate & Now 
ArchiveDate = Replace(ArchiveDate,"/","") 
ArchiveDate = Replace(ArchiveDate," ","") 
ArchiveDate = Replace(ArchiveDate,":","") 
ArchiveDate = "MDF_" & ArchiveDate 

'Path to the csv-file 
path = "D:\Historical_data\" & ArchiveDate & ".csv" 

'Create Filesystemobject and CSV-file if not exists: 
Set fso = CreateObject("Scripting.FileSystemObject") 
If Not fso.FileExists(path) Then 
    fso.CreateTextFile(path) 
Else 
    MsgBox "File already exists!" 
    Exit Sub 
End If 

'Create object and open it for writing 
Set f = fso.GetFile(path) 
Set ts = f.OpenAsTextStream(2,-2) 

ts.WriteLine("Tag-Name;ValueID;Date/Time;Process-Value") 'Header 

'Generate String for the CSV-Filename 
Dim Pro   'Provider 
Dim DSN   'Data Source Name 
Dim DS   'Data Source 
Dim ConnString 'Connection String 
Dim MachineNameRT 'Name of the PC from WinCC-RT 
Dim DSNRT  'Data Source Name from WinnCC-RT 

Dim Conn  'Connection to ADODB 
Dim RecSet  'RecordSet 
Dim RecSetColl 'RecordSet storing data to be saved to the CSV-file 
Dim Command  'Query 
Dim CommandText 'Command-Text 
Dim i 

'Read the name of the PC-Station and the DSN-Name from WinCC-RT 
Set MachineNameRT = HMIRuntime.Tags("@LocalMachineName") 
Set DSNRT = HMIRuntime.Tags("@DatasourceNameRT") 

'Preparing the Connection-String 
Pro = "Provider=WinCCOLEDBProvider.1;" 'First instance of WinCCOLEDB 
DSN = "Catalog=" & DSNRT.Read & ";"  'Name of Runtime-Database 
DS = "Data Source=" & MachineNameRT.Read & "\WinCC" 'Data Source 

'Build the complete String: 
ConnString = Pro + DSN + DS 

'Make Connection 
Set Conn = CreateObject("ADODB.Connection") 
Conn.ConnectionString = ConnString 
Conn.CursorLocation = 3 
Conn.open 

Set RecSetColl = CreateObject("ADODB.Recordset") 

With RecSetColl.Fields 
    .Append "Time1", adChar 
    .Append "AHU_RUN", adChar 
    .Append "Time2", adChar 
    .Append "TT01", adChar 
    .Append "TT02", adChar 
End With 


For i = 0 To 4 

    Set RecSet = CreateObject("ADODB.Recordset")     
    Set Command = CreateObject("ADODB.Command") 

    Command.CommandType = 1 

    Set Command.ActiveConnection = Conn 

    'Building the complete string 
    CommandText = "Tag:R," & i & ",'0000-00-00 12:00:00.000','0000-00-00 00:00:00.000'" 

    Command.CommandText = CommandText 

    Set RecSet = Command.Execute 

    RecSet.MoveFirst 

    RecSetColl.Fields(i) = RecSet.Fields(4) 'RecSet.Fields(4) stores a proces value 

    RecSet.Close 
    Set RecSet = Nothing 
    Set Command = Nothing 
Next 

'Writing recordsets to CSV-file 
Do While Not RecSetColl.EOF 
    ts.WriteLine (RecSetColl.Fields(0).Value & ";" & RecSetColl.Fields(1).Value & ";" & RecSetColl.Fields(2).Value & ";" & RecSetColl.Fields(3).Value & ";" & RecSetColl.Fields(4).Value & ";" & RecSetColl.Fields(5).Value) 
    RecSetColl.MoveNext 
Loop 

RecSetColl.Close 
Set RecSetColl = Nothing 
Conn.close 
Set Conn = Nothing 

ts.Close 
Set fso = Nothing 
Set f = Nothing 
Set ts = Nothing 

End Sub 

回答

0

我真的不知道什麼是不工作,但猜測;

是否ValueID = 0(「0」中的「i」)存在於您的項目中?

在「存檔」表中,您可以找到有效的ValueID,在我的所有項目中都以「1」開頭。在SQL Management Studio中很容易看到,可能有時候存在0。

要獲取所有導出的值,首先查詢「存檔」表,然後使用返回的任何ValueID請求循環中的數據。

// PerD