Sie sind auf Seite 1von 4

Sub VBScript_DataLogging()

'Tip:
' 1. Use the <CTRL+SPACE> or <CTRL+I> keystroke to open a list of all objects and
functions
' 2. Write the code using the HMIRuntime object.
' Example: HmiRuntime.Screens("Screen_1").
' 3. Use the <CTRL+J> keystroke to create an object reference.
'Write the code as of this position:
'----------------------------------------------------------------------------------
---------------
'Script Name : DataLogging
'Functions : 1. Write the data into a csv file (also create the csv file if it
doesnot exist).
' 2. Update the values in the GUI Components of Main Screen.
'----------------------------------------------------------------------------------
---------------

'--[Start]-- Temporary Variable Declaration ---


Dim Parameter_A
Dim Parameter_B
Dim Parameter_C
Dim Parameter_D
Dim Parameter_E
Dim Parameter_F
Dim Parameter_G
Dim Parameter_H

Dim PLC_Year
Dim PLC_Month
Dim PLC_Day
Dim PLC_Hour
Dim PLC_Minute
Dim PLC_Second

'--[ End ]-- Variable Declaration ---

'SmartTags("Parameter_A") = FormatNumber(Rnd,2,-1)
'SmartTags("Parameter_B") = FormatNumber(Rnd,2,-1)
'SmartTags("Parameter_C") = FormatNumber(Rnd,2,-1)
'SmartTags("Parameter_D") = FormatNumber(Rnd,2,-1)
'SmartTags("Parameter_E") = FormatNumber(Rnd,2,-1)
'SmartTags("Parameter_F") = FormatNumber(Rnd,2,-1)
'SmartTags("Parameter_G") = FormatNumber(Rnd,2,-1)
'SmartTags("Parameter_H") = FormatNumber(Rnd,2,-1)

'--[Start]-- PLC Tag Values Read ---

Parameter_A = FormatNumber(SmartTags("Parameter_A"),2,-1)
Parameter_B = FormatNumber(SmartTags("Parameter_B"),2,-1)
Parameter_C = FormatNumber(SmartTags("Parameter_C"),2,-1)
Parameter_D = FormatNumber(SmartTags("Parameter_D"),2,-1)
Parameter_E = FormatNumber(SmartTags("Parameter_E"),2,-1)
Parameter_F = FormatNumber(SmartTags("Parameter_F"),2,-1)
Parameter_G = FormatNumber(SmartTags("Parameter_G"),2,-1)
Parameter_H = FormatNumber(SmartTags("Parameter_H"),2,-1)

'--[ End ]-- PLC Tag Values Read ---


'--[Start]-- DataLogging - Data Write into csv file ---

Dim Folder_Path, File_Name, Storage_Path, File_Search_Object, File_Object, objFile,


WriteStream, AlarmText

'--[Start]-- Data Logging ---


'Folder_Path = "D:\home\IIA_Printer_1500\"
'Folder_Path = "D:\home\IIA Printer 1500\"
'Folder_Path = "\flash\IIA Report Task\"
Folder_Path = "\Storage Card USB\"
File_Name = "DataLog" + "_" + CStr(Year(Date))+ "_" + CStr(Month(Date)) + "_" +
CStr(Day(Date)) + ".csv"
Storage_Path = Folder_Path & File_Name

PLC_Year = CStr(Year(Date))
PLC_Month = CStr(Month(Date))
PLC_Day = CStr(Day(Date))
PLC_Hour = CStr(Hour(Time))
PLC_Minute = CStr(Minute(Time))
PLC_Second = CStr(Second(Time))

Set File_Search_Object = CreateObject("fileCtl.filesystem") 'Create File Object


for File IO
Set File_Object = CreateObject("fileCtl.file")

' File check (Open / Create & Open)


If File_Search_Object.dir(Storage_Path) = "" Then
File_Object.Open Storage_Path,8
File_Object.LinePrint
"Year/Month/Day,Hour:Minute:Second,Parameter_A,Parameter_B,Parameter_C,Parameter_D,
Parameter_E,Parameter_F,Parameter_G,Parameter_H"
Else
File_Object.Open Storage_Path,8
End If

WriteStream = ( _
PLC_Year & "/" & PLC_Month & "/" & PLC_Day & "," & _
PLC_Hour & ":" & PLC_Minute & ":" & PLC_Second & "," & _
Parameter_A & "," & _
Parameter_B & "," & _
Parameter_C & "," & _
Parameter_D & "," & _
Parameter_E & "," & _
Parameter_F & "," & _
Parameter_G & "," & _
Parameter_H)

Dim Alarmparameter_A, Alarmparameter_B, Alarmparameter_C, Alarmparameter_D,


Alarmparameter_E, Alarmparameter_F, Alarmparameter_G, Alarmparameter_H

If Parameter_A < 10 Then


Alarmparameter_A = "00" & CStr(Parameter_A)
ElseIf ((Parameter_A >= 10) And (Parameter_A < 100)) Then
Alarmparameter_A = "0" & CStr(Parameter_A)
Else
Alarmparameter_A = CStr(Parameter_A)
End If
If Parameter_B < 10 Then
Alarmparameter_B = "00" & CStr(Parameter_B)
ElseIf ((Parameter_B >= 10) And (Parameter_B < 100)) Then
Alarmparameter_B = "0" & CStr(Parameter_B)
Else
Alarmparameter_B = CStr(Parameter_B)
End If

If Parameter_C < 10 Then


Alarmparameter_C = "00" & CStr(Parameter_C)
ElseIf ((Parameter_C >= 10) And (Parameter_C < 100)) Then
Alarmparameter_C = "0" & CStr(Parameter_C)
Else
Alarmparameter_C = CStr(Parameter_C)
End If

If Parameter_D < 10 Then


Alarmparameter_D = "00" & CStr(Parameter_D)
ElseIf ((Parameter_D >= 10) And (Parameter_D < 100)) Then
Alarmparameter_D = "0" & CStr(Parameter_D)
Else
Alarmparameter_D = CStr(Parameter_D)
End If

If Parameter_E < 10 Then


Alarmparameter_E = "00" & CStr(Parameter_E)
ElseIf ((Parameter_E >= 10) And (Parameter_E < 100)) Then
Alarmparameter_E = "0" & CStr(Parameter_E)
Else
Alarmparameter_E = CStr(Parameter_E)
End If

If Parameter_F < 10 Then


Alarmparameter_F = "00" & CStr(Parameter_F)
ElseIf ((Parameter_F >= 10) And (Parameter_F < 100)) Then
Alarmparameter_F = "0" & CStr(Parameter_F)
Else
Alarmparameter_F = CStr(Parameter_F)
End If

If Parameter_G < 10 Then


Alarmparameter_G = "00" & CStr(Parameter_G)
ElseIf ((Parameter_G >= 10) And (Parameter_G < 100)) Then
Alarmparameter_G = "0" & CStr(Parameter_G)
Else
Alarmparameter_G = CStr(Parameter_G)
End If

If Parameter_H < 10 Then


Alarmparameter_H = "00" & CStr(Parameter_H)
ElseIf ((Parameter_H >= 10) And (Parameter_H < 100)) Then
Alarmparameter_H = "0" & CStr(Parameter_H)
Else
Alarmparameter_H = CStr(Parameter_H)
End If

AlarmText = ( " " & _


Alarmparameter_A & " " & _
Alarmparameter_B & " " & _
Alarmparameter_C & " " & _
Alarmparameter_D & " " & _
Alarmparameter_E & " " & _
Alarmparameter_F & " " & _
Alarmparameter_G & " " & _
Alarmparameter_H)

SmartTags("Alarm_Text") = AlarmText

If SmartTags("TRIGGER_2") = 1 Then
SmartTags("TRIGGER_2") = 0
Else
SmartTags("TRIGGER_2") = 1
End If

File_Object.LinePrint WriteStream
File_Object.Close

Set File_Object = Nothing


Set File_Search_Object = Nothing

'--[ End ]-- Data Logging ---


End Sub