首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从SCADA系统导出数据

从SCADA系统导出数据
EN

Stack Overflow用户
提问于 2017-11-11 04:43:51
回答 1查看 1.8K关注 0票数 0

我正在尝试创建VBscript,以便从SCADA系统(WinCC RT专业版)导出过程数据,以便定期存档所有过程变量。数据存储在可通过连接包访问的SQL表中。我设法使脚本在导出一个标记(过程变量)时工作,但我想循环遍历系统中的所有标记(大约60个),将它们收集到另一个记录集中,然后将该记录集中的所有数据保存在一个csv文件中。我已经创建了收集一个标签(时间,过程变量等)的所有变量(字段)的RecSet,我只需要字段4中的值(所有标签的相同字段)。然后我想将这个字段复制到另一个记录集中- RecSetColl,它从所有标签中收集所有需要的数据(字段4),最后将它们保存在CSV文件中。非常感谢你的帮助。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-11-14 05:38:28

我真的不知道什么是不起作用的,只是一个猜测;

您的项目中是否存在ValueID =0( "for 0 to 4“中的"i”)?

在“存档”表中,您将在“我的所有项目”中找到以"1“开头的有效ValueID。在SQL Management Studio中很容易看到,有时可能存在0。

要获得所有导出的值,首先查询"Archive“表,然后使用返回的任何ValueID在循环中请求数据。

//PerD

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47230825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档