2017-11-10 2 views
0

모든 프로세스 변수를 주기적으로 보관하기 위해 SCADA 시스템 (WinCC RT Professional)에서 프로세스 데이터를 내보내는 VBscript를 만들려고합니다. 데이터는 연결 팩을 통해 액세스 할 수있는 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 ("for 0 to 4"의 "i")이 있습니까?

"보관 파일"테이블에서 유효한 모든 ValueID가 발견되며 모든 프로젝트에서 "1"로 시작합니다. SQL Management Studio에서 간단하게 볼 수 있습니다. 때로는 0이 존재할 수도 있습니다.

모든 값을 내보내려면 먼저 "보관"테이블을 쿼리 한 다음 반환되는 값 ID를 사용하여 루프에서 데이터를 요청하십시오.

// PerD

관련 문제