Long term archiving script in WinCC Advanced V13

Traloch

Member
Join Date
Feb 2013
Location
Stuttgart
Posts
29
Hi guys,

I'm not very good when it comes to VB scripting, trying to learn it by doing at the moment, so I'd appreciate some help!

I took a sample script from Siemens and made some small changes to implement some long term archiving in WinCC Advanced V13, where the customer can select Year,Month,Day and load the archive into a trend. It works fine the first time I call the script, but when I change the date and run it again it seems to not fully complete the script. I've looked in the folder where logs are recorded and can see that the last two files were never deleted.

Below is the original script taken from Siemens:

Code:
Sub Load_Archive(ByRef StoragePath)
Dim fso, FName, Variable_Date, Variable_Time 
Dim ValueYear, ValueMonth, ValueDay, ValueHour, FileName, DataFileName


'Hilfsvariablen um nachfolgend den Archivierungspfad zusammen zu stellen
'Auxiliary variable to combine the storage path 
ValueYear = SmartTags("Selection_Year")	
ValueMonth = SmartTags("Selection_Month")	
ValueDay = SmartTags("Selection_Day")	
ValueHour = SmartTags("Selection_Hour")& "0.csv"	


'Archivierungspfad auswählen
'Compose storage path
FName = StoragePath & "Year_" & ValueYear & "\" & "Month_" & ValueMonth & "\" & "Day_" & ValueDay & "\"


'Name der Datei, die geladen werden soll
'Name of the data file to be loaded
FileName = "Press_01_" & ValueHour 


'Dateizugriff 
'File access
Set fso = CreateObject("Scripting.FileSystemObject") 


'Überprüfen ob der vorgegebene Archivname vorhanden ist
'Check if the predefined archiv exists 
DataFileName = FName & FileName


If Not fso.FileExists(DataFileName) Then
	
	'Datei nicht vorhanden -> Systemmeldung ausgeben / Projektierte "Textbox" einblenden 
	'Data file not available -> show system alarm / show "Textbox" 
	
	'ShowSystemAlarm "Bitte überprüfen Sie das vorgegebene Archivdatum" 
	'ShowSystemAlarm "Recheck the specific date of the Archive data file"
	SetBit "TrendView_Visible"	'Im "Bild 2" wird ein Meldetext eingeblendet / In "Screen 2" there will be show a message text
	
Else
	
	'Start des Archivs "Data_Logs_Trend_View" 
	'Start of the archive "Data_Logs_Trend_View"
	StartLogging hmiDataLog, "Data_Logs_Trend_View"
	
		
	'Solange das Folgearchiv "Data_Logs_Trend_View1.csv" nicht vorhanden ist -> Warten!
	'Waiting until the subsequent archive "Data_Logs_Trend_View1.csv" is available
	While Not fso.FileExists (StoragePath & "Data_Logs_Trend_View1.csv") 

	Wend	
	
	'Folgearchiv "Data_Logs_Trend_View1.csv" in das Archiv "Temp_Data_Logs_Trend_View.csv" kopieren
	'Copy the subsequent archives "Data_Logs_Trend_View1.csv" to the archive "Temp_Data_Logs_Trend_View.csv"
	fso.CopyFile StoragePath & "Data_Logs_Trend_View1.csv", StoragePath & "Temp_Data_Logs_Trend_View.csv"
	
	
	'Solange das Archiv "Temp_Data_Logs_Trend_View.csv" nicht vorhanden ist -> Warten!
	'Waiting until the archive "Temp_Data_Logs_Trend_View.csv" is available!
	While Not fso.FileExists (StoragePath & "Temp_Data_Logs_Trend_View.csv")
	
	Wend
	
	'Archivierung des Archivs "Data_Logs_Trend_View" stoppen.
	'Stop logging to the archive "Data_Logs_Trend_View"
	StopLogging  hmiDataLog, "Data_Logs_Trend_View"
	
	
	'Alle Archive schließen
	'Close all logs
	CloseAllLogs 
	
	
	'Für die Initialisierung der Archive wird eine gewisse Zeit benötigt. Es kann ansonsten zu einem Zugriffehler kommen,
	'wenn das System auf das Archiv zugreifen will. -> Wartezeit
	'A certain waiting time is required for the initialization of the archives 
	'to avoid access conflicts
	Dim Counter1
	Counter1 = 0
	While Counter1 < 60000
		Counter1 = Counter1 + 1
	Wend
	
	
	'Das Archiv "Data_Logs_Trend_View0.csv", "Data_Logs_Trend_View1.csv" und "Temp_Data_Logs_Trend_View.csv" 
	'kann jetzt gelöscht werden. 
	'The archives "Data_Logs_Trend_View0.csv", "Data_Logs_Trend_View1.csv" and "Temp_Data_Logs_Trend_View.csv"
	'can be deleted now
	fso.DeleteFile StoragePath & "Data_Logs_Trend_View0.csv"  
	fso.DeleteFile StoragePath & "Data_Logs_Trend_View1.csv"
	fso.DeleteFile StoragePath & "Temp_Data_Logs_Trend_View.csv"
	
	
	'Wartezeit
	'Waiting time
	Dim Counter2
	Counter2 = 0
	While Counter2 < 60000
		Counter2 = Counter2 + 1
	Wend
	
	
	'Kopiert den Inhalt des angewählten Archivs in das Archiv für die Trendkurve
	'Copy the content from the selected archive to the archive for the trend view
	fso.CopyFile DataFileName , StoragePath & "Data_Logs_Trend_View0.csv", True 
	
	
	'Alle Archive wieder öffnen
	'Open all logs
	OpenAllLogs 
	
		
	'Starten des Archivs "Archive_01"
	'Start logging to archive "Archive_01"
	StartLogging hmiDataLog, "Archive_01"
	
End If


'Die Kurvenanzeige auf den gewählten Zeitraum setzen
'Set the "Trend view" to the selected time 
Variable_Date = SmartTags("Selection_Day") & " " & SmartTags("Selection_Month") & " " & SmartTags("Selection_Year")
Variable_Time = SmartTags("Selection_Hour") & ":30:00" ' Durch die Vorgabe der "Minute" (30) wird die Mitte der Stunde in der Kurvenanzeige angezeigt
SmartTags("TrendView_ExternalTime") = CDate(Variable_Date & " " & Variable_Time)


'Verwendeten Speicher wieder freigeben
'Free used storage
Set fso = Nothing
End Sub
 
The script with my changes

Code:
Sub Load_Archive_AB009(ByRef StoragePath)
Dim fso, FName, Variable_Date, Variable_Time 
Dim ValueYear, ValueMonth, ValueDay, FileName, DataFileName


'Hilfsvariablen um nachfolgend den Archivierungspfad zusammen zu stellen
'Auxiliary variable to combine the storage path 
ValueYear = SmartTags("Selection_Year")	
ValueMonth = SmartTags("Selection_Month")	
ValueDay = SmartTags("Selection_Day")& "0.csv"		


'Archivierungspfad auswählen
'Compose storage path
FName = StoragePath & "Year_" & ValueYear & "\" & "Month_" & ValueMonth & "\"


'Name der Datei, die geladen werden soll
'Name of the data file to be loaded
FileName = "Charge1_AB009" & ValueDay 


'Dateizugriff 
'File access
Set fso = CreateObject("Scripting.FileSystemObject")  

	
'Überprüfen ob der vorgegebene Archivname vorhanden ist
'Check if the predefined archiv exists 
DataFileName = FName & FileName


If Not fso.FileExists(DataFileName) Then
	
	'Datei nicht vorhanden -> Systemmeldung ausgeben / Projektierte "Textbox" einblenden 
	'Data file not available -> show system alarm / show "Textbox" 
	
	'ShowSystemAlarm "Bitte überprüfen Sie das vorgegebene Archivdatum" 
	'ShowSystemAlarm "Recheck the specific date of the Archive data file"
	SetBit "TrendView_Visible_AB009"	'Im "Bild 2" wird ein Meldetext eingeblendet / In "Screen 2" there will be show a message text
	
Else
	
	'Start des Archivs "Charge1_AB009_Archiv" 
	'Start of the archive "Charge1_AB009_Archiv"
	StartLogging hmiDataLog, "Charge1_AB009_Archiv"
	
		
	'Solange das Folgearchiv "Data_Logs_Trend_View1.csv" nicht vorhanden ist -> Warten!
	'Waiting until the subsequent archive "Data_Logs_Trend_View1.csv" is available
	While Not fso.FileExists (StoragePath & "Charge1_AB009_Archiv1.csv") 

	Wend	
	
	'Folgearchiv "Data_Logs_Trend_View1.csv" in das Archiv "Temp_Data_Logs_Trend_View.csv" kopieren
	'Copy the subsequent archives "Data_Logs_Trend_View1.csv" to the archive "Temp_Data_Logs_Trend_View.csv"
	fso.CopyFile StoragePath & "Charge1_AB009_Archiv1.csv", StoragePath & "Temp_Charge1_AB009_Archiv.csv"
	
	
	'Solange das Archiv "Temp_Data_Logs_Trend_View.csv" nicht vorhanden ist -> Warten!
	'Waiting until the archive "Temp_Data_Logs_Trend_View.csv" is available!
	While Not fso.FileExists (StoragePath & "Temp_Charge1_AB009_Archiv.csv")
	
	Wend
	
	'Archivierung des Archivs "Data_Logs_Trend_View" stoppen.
	'Stop logging to the archive "Data_Logs_Trend_View"
	StopLogging  hmiDataLog, "Charge1_AB009_Archiv"
	
	
	'Alle Archive schließen
	'Close all logs
	CloseAllLogs 
	
	
	'Für die Initialisierung der Archive wird eine gewisse Zeit benötigt. Es kann ansonsten zu einem Zugriffehler kommen,
	'wenn das System auf das Archiv zugreifen will. -> Wartezeit
	'A certain waiting time is required for the initialization of the archives 
	'to avoid access conflicts
	Dim Counter1
	Counter1 = 0
	While Counter1 < 60000
		Counter1 = Counter1 + 1
	Wend
	
	
	'Das Archiv "Data_Logs_Trend_View0.csv", "Data_Logs_Trend_View1.csv" und "Temp_Data_Logs_Trend_View.csv" 
	'kann jetzt gelöscht werden. 
	'The archives "Data_Logs_Trend_View0.csv", "Data_Logs_Trend_View1.csv" and "Temp_Data_Logs_Trend_View.csv"
	'can be deleted now
	fso.DeleteFile StoragePath & "Charge1_AB009_Archiv0.csv"
	While fso.FileExists (StoragePath & "Charge1_AB009_Archiv0.csv")
	
	Wend
	fso.DeleteFile StoragePath & "Charge1_AB009_Archiv1.csv"
	While fso.FileExists (StoragePath & "Charge1_AB009_Archiv1.csv")
	
	Wend
	fso.DeleteFile StoragePath & "Temp_Charge1_AB009_Archiv.csv"
	While fso.FileExists (StoragePath & "Temp_Charge1_AB009_Archiv.csv")
	
	Wend
	
	'Wartezeit
	'Waiting time
	Dim Counter2
	Counter2 = 0
	While Counter2 < 60000
		Counter2 = Counter2 + 1
	Wend
	
	
	'Kopiert den Inhalt des angewählten Archivs in das Archiv für die Trendkurve
	'Copy the content from the selected archive to the archive for the trend view
	fso.CopyFile DataFileName , StoragePath & "Charge1_AB009_Archiv0.csv", True 
	
	
	'Alle Archive wieder öffnen
	'Open all logs
	OpenAllLogs 
	
		
	'Starten des Archivs "Archive_01"
	'Start logging to archive "Archive_01"
	StartLogging hmiDataLog, "Archive_01"
	StartLogging hmiDataLog, "Charge1_AB009"
	StartLogging hmiDataLog, "Charge2_AB010"
	StartLogging hmiDataLog, "Endkontrolle_AB016"
	StartLogging hmiDataLog, "Endkontrolle_AB020"
	StartLogging hmiDataLog, "Entgratung_AB007"
	StartLogging hmiDataLog, "KFP"
	StartLogging hmiDataLog, "Nachneutralisation_AB014"
	StartLogging hmiDataLog, "Nickel_AB003"
	StartLogging hmiDataLog, "VE_Wasser"
End If


'Die Kurvenanzeige auf den gewählten Zeitraum setzen
'Set the "Trend view" to the selected time 
Variable_Date = SmartTags("Selection_Day") & " " & SmartTags("Selection_Month") & " " & SmartTags("Selection_Year")
Variable_Time = SmartTags("Selection_Hour") & ":59:00" ' Durch die Vorgabe der "Minute" (00) wird die Start der Stunde in der Kurvenanzeige angezeigt
SmartTags("TrendView_ExternalTime_AB009") = CDate(Variable_Date & " " & Variable_Time)


'Verwendeten Speicher wieder freigeben
'Free used storage
Set fso = Nothing
End Sub
 
What I've ended up with.

I don't see the purpose of starting logging and creating these other extra files only to delete them straight after so I removed this part of the script and I'm now just deleting the file and copying to it new. It seems to work but there is some bug as most of the time I have to run the script twice before the changes will take place.

Code:
Sub Load_Archive_AB009_1(ByRef StoragePath)
Dim fso, FName, Variable_Date, Variable_Time 
Dim ValueYear, ValueMonth, ValueDay, FileName, DataFileName


'Hilfsvariablen um nachfolgend den Archivierungspfad zusammen zu stellen
'Auxiliary variable to combine the storage path 
ValueYear = SmartTags("Selection_Year")	
ValueMonth = SmartTags("Selection_Month")	
ValueDay = SmartTags("Selection_Day")& "0.csv"		


'Archivierungspfad auswählen
'Compose storage path
FName = StoragePath & "Year_" & ValueYear & "\" & "Month_" & ValueMonth & "\"


'Name der Datei, die geladen werden soll
'Name of the data file to be loaded
FileName = "Charge1_AB009" & ValueDay 


'Dateizugriff 
'File access
Set fso = CreateObject("Scripting.FileSystemObject")  

	
'Überprüfen ob der vorgegebene Archivname vorhanden ist
'Check if the predefined archiv exists 
DataFileName = FName & FileName


If Not fso.FileExists(DataFileName) Then
	
	'Datei nicht vorhanden -> Systemmeldung ausgeben / Projektierte "Textbox" einblenden 
	'Data file not available -> show system alarm / show "Textbox" 
	
	'ShowSystemAlarm "Bitte überprüfen Sie das vorgegebene Archivdatum" 
	'ShowSystemAlarm "Recheck the specific date of the Archive data file"
	SetBit "TrendView_Visible_AB009"	'Im "Bild 2" wird ein Meldetext eingeblendet / In "Screen 2" there will be show a message text
	
Else
	
	'Alle Archive schließen
	'Close all logs
	CloseAllLogs 
	
	
	'Für die Initialisierung der Archive wird eine gewisse Zeit benötigt. Es kann ansonsten zu einem Zugriffehler kommen,
	'wenn das System auf das Archiv zugreifen will. -> Wartezeit
	'A certain waiting time is required for the initialization of the archives 
	'to avoid access conflicts
	Dim Counter1
	Counter1 = 0
	While Counter1 < 60000
		Counter1 = Counter1 + 1
	Wend
	
	
	'Das Archiv "Data_Logs_Trend_View0.csv", "Data_Logs_Trend_View1.csv" und "Temp_Data_Logs_Trend_View.csv" 
	'kann jetzt gelöscht werden. 
	'The archives "Data_Logs_Trend_View0.csv", "Data_Logs_Trend_View1.csv" and "Temp_Data_Logs_Trend_View.csv"
	'can be deleted now
	fso.DeleteFile StoragePath & "Charge1_AB009_Archiv0.csv"
	While fso.FileExists (StoragePath & "Charge1_AB009_Archiv0.csv")
	
	Wend
		
	'Kopiert den Inhalt des angewählten Archivs in das Archiv für die Trendkurve
	'Copy the content from the selected archive to the archive for the trend view
	fso.CopyFile DataFileName , StoragePath & "Charge1_AB009_Archiv0.csv", True 
	
	'Wartezeit
	'Waiting time
	Dim Counter2
	Counter2 = 0
	While Counter2 < 60000
		Counter2 = Counter2 + 1
	Wend
	
	'Alle Archive wieder öffnen
	'Open all logs
	OpenAllLogs 
	
		
	'Starten des Archivs "Archive_01"
	'Start logging to archive "Archive_01"
	StartLogging hmiDataLog, "Archive_01"
	StartLogging hmiDataLog, "Charge1_AB009"
	StartLogging hmiDataLog, "Charge2_AB010"
	StartLogging hmiDataLog, "Endkontrolle_AB016"
	StartLogging hmiDataLog, "Endkontrolle_AB020"
	StartLogging hmiDataLog, "Entgratung_AB007"
	StartLogging hmiDataLog, "KFP"
	StartLogging hmiDataLog, "Nachneutralisation_AB014"
	StartLogging hmiDataLog, "Nickel_AB003"
	StartLogging hmiDataLog, "VE_Wasser"
End If


'Die Kurvenanzeige auf den gewählten Zeitraum setzen
'Set the "Trend view" to the selected time 
Variable_Date = SmartTags("Selection_Day") & " " & SmartTags("Selection_Month") & " " & SmartTags("Selection_Year")
Variable_Time = SmartTags("Selection_Hour") & ":59:00" ' Durch die Vorgabe der "Minute" (00) wird die Start der Stunde in der Kurvenanzeige angezeigt
SmartTags("TrendView_ExternalTime_AB009") = CDate(Variable_Date & " " & Variable_Time)


'Verwendeten Speicher wieder freigeben
'Free used storage
Set fso = Nothing
End Sub
 
Counters

I've also tried playing with the values for the counters by increasing them, but then it never worked. What is the max value the counter can count to? and what is the value 60000? (I thought that it might be 60000ms but this would be too long!)
 

Similar Topics

Hello!, Im having some problems with the function, "How do you do long-term archiving with WinCC flexible?". Im using Siemens standard project...
Replies
10
Views
2,842
I have Allen Bradley plcs, I have had Circuit breakers and other automation equipment in the past. There's no solid buyers local. How much do you...
Replies
2
Views
200
Using Allen Bradley PLCs, What are some easy ways to save process data from the PLC to a computer ? Would I have to start getting into SQL type...
Replies
18
Views
10,745
1752-L72S ControlLogix Controller. I've been asked to store data on recipes. It will be a simple cumulative weight for each recipe which may...
Replies
2
Views
1,144
Has anyone used Beckhoff or any other Windows based PAC long term (life and run time?). I'm wondering how long Windows embedded can run...
Replies
8
Views
3,796
Back
Top Bottom