Returning Tagnames when querying Archive Database in WinCC RunTime

3.2k views Asked by At

I'm developing a VB Script for WinCC RunTime on a SIMATIC PC station to export the historical data of my project once a month. I'm setting up an ADO connection and querying the results into a recordset that I'm printing to a csv. I'm having several problems:

  1. The recordset returns a ValueID, I want to be able to find the tagname that corresponds to it and write it to the csv.
  2. I am limited to 20 tags per query, but I want to export 30 tags.
  3. Running a for loop of the query for each tag produces nothing.

My code currently looks like this:

Dim fso
Dim f
Dim ts
Dim path
Dim TimeStamp
Dim Pro
Dim DSN
Dim DS
Dim ConnString
Dim MachineNameRT
Dim DSNRT
Dim Conn
Dim RecSet
Dim Command
Dim CommandText

TimeStamp = localDateFormat(Now)
path = "C:\Logs\Test1_" & TimeStamp & ".csv"
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(path) Then
    fso.CreateTextFile(path)
Else
    MsgBox "File already exist:" & vbCrLf & path
Exit Sub
End If
Set f = fso.GetFile(path)
Set ts = f.OpenAsTextStream(2,-2)
Set MachineNameRT = HMIRuntime.Tags("@LocalMachineName")
Set DSNRT = HMIRuntime.Tags("@DatasourceNameRT") 
Pro="Provider=WinCCOLEDBProvider.1;"                
DSN="Catalog=" & DSNRT.Read & ";"                   
DS= "Data Source=" & MachineNameRT.Read & "\WinCC"   
ConnString = Pro + DSN + DS 
Set Conn = CreateObject("ADODB.Connection") 
Conn.ConnectionString = ConnString 
Conn.CursorLocation = 3
Conn.open
Set Command = CreateObject("ADODB.Command") 
Command.CommandType = 1 
Set Command.ActiveConnection = Conn
ts.WriteLine ("Tag-Name;ValueID;Date/Time;Process-Value")
CommandText="Tag:R,'Data_Log\TempTran','0000-01-00 00:00:00.000','0000-00-00 00:00:00.000'"
Command.CommandText=CommandText 
Set RecSet = Command.Execute 
RecSet.MoveFirst
Do While Not RecSet.EOF 
    ts.WriteLine ("TempTran;" & RecSet.Fields("ValueID").Value & ";" & RecSet.Fields("TimeStamp").Value & ";" & RecSet.Fields("RealValue").Value) 
    RecSet.MoveNext 
Loop 
ts.Close
RecSet.Close
Set RecSet=Nothing 
Set Command = Nothing   
Conn.Close
Set Conn = Nothing 
Set fso = Nothing 
Set f = Nothing 
Set ts = Nothing 

What I need as an end result is a CSV file that displays like this

Tag-Name;ValueID;Date/Time;Process-Value;
TempTran;1;dd/mm/yyyy hh:mm:ss;xxx.xxx;
TempTran;1;dd/mm/yyyy hh:mm:ss;xxx.xxx;
PresTran;2;dd/mm/yyyy hh:mm:ss;xxx.xxx;
.
.
.
.
LimitSwt;30;dd/mm/yyyy hh:mm:ss;xxx.xxx;
LimitSwt;30;dd/mm/yyyy hh:mm:ss;xxx.xxx;
LimitSwt;30;dd/mm/yyyy hh:mm:ss;xxx.xxx;
1

There are 1 answers

0
Per Dahlqvist On

Q1: The ValueID and tagname is in the Table "Archive" if the database is linked to connectivity pack.

SELECT [ValueID]
  ,[ValueName]
FROM [CC_ExternalBrowsing].[dbo].[Archive]

Otherwise you can find the valueId in the CS database Runtime is in this case "CC_GruppLar_15_05_06_10_35_08R", its the last 'R' that indicate "runtime", remove this and you are in the construction database(CS):

Varname = the given name

procvarname= the tag that is archived.

TLGTAGID = The ValueID

SELECT [VARNAME]
  ,[PROCVARNAME] 
  ,[TLGTAGID]   
 FROM [CC_GruppLar_15_05_06_10_35_08].[dbo].[PDE#TAGs]

Q3: perhaps this note about months in the fine help can help? or try to verify using that there is data present for this tag. I think its a good idea to test the SQL statements using the given tool "Microsoft SQL Manager Studio", that's faster then using excel...

Note Enter a relative period you want to query in a linked archive database using the following format: 0000-00-DD hh:mm:ss.msc

If you indicate the time frame in months, the content can be faulty, because a month can have 28 to 31 days.

Example for reference: Exporting Archive Data with the Aid of the SIMATIC WinCC/Connectivity Pack (OLE DB Provider)

https://support.industry.siemens.com/cs/se/en/view/38132261