Blog

Custom Data Logging using a Siemens Comfort Panel and VB Script

Custom Data Logging using a Siemens Comfort Panel and VB Script

I recently worked on a project that required the use of data logging. However, the formatting of Siemen's data logging was not ideal for this implementation. So, I made my own version of data logging using Visual Basic scripts on my Comfort Panel that create and append to a CSV file.

One of the first things to think about is where you want to store these logs. For Comfort Panels, you can access the onboard USB drive and the SD card slot via VBScript. You can also access a network shared drive if the HMI has been correctly networked. For this example, we'll just be saving the logs to a USB flash drive inserted into the comfort panel, but if you'd prefer saving to a network folder, Siemens has a post that explains how to correctly network, set up user administration, and share a folder with the HMI.

Step 1: Determine Folder Location and File Name

Open up an empty Visual Basic script in TIA Portal and determine the folder location and file name that you would like to use. "\Storage Card USB\" and "\Storage Card SD\" will be the locations for the onboard USB port and SD card, respectively. I'll be creating a file name that includes the current date. Including the current date in the file name will allow us to have a different file for each day, to limit the file size, and also help organize the data a bit better.

Dim fileName, folderName, fileLocation
fileName = Year(Now()) & "_" & Month(Now()) & "_" & Day(Now()) & ".csv"
folderName = "\Storage Card USB\Logs\"
fileLocation = folderName & filename

Step 2: Create a New Object & Folder Structure

Create a new FileCtl.FileSystem object. Now we can create the required folder structure if there is one. We use the Dir method to check if our folder currently exists. If not, then we use the MkDir method to create the folder. 

I added some error handling to my script so that if there is a problem executing this command, it will get displayed on an alarm window that I have in my project.

'Continue even if there is an error
On Error Resume Next
                
                'Create File System Object
                Set fso = CreateObject("FileCtl.FileSystem")
                'Check if the folder already exists
                If (fso.Dir(folderName)) = "" Then
                                fso.MkDir folderName
                End If
                                
                If Err.Number <> 0 Then
                                ShowSystemAlarm "Error # " & CStr(Err.Number)& " " & Err.Description
                                Err.Clear
                End If

Step 3: CreateObject Type FileCtl.File

Now that the folder structure exists, we will again use the “CreateObject” function, this time of type FileCtl.File. You'll want to use the Open method, passing in the location and name of the file. The “8” indicates that we are opening this file for appending. This command will open the file if it currently exists, and create a new file if it does not.

'Create FileCtl.File object
                Set fctl = CreateObject("FileCtl.File")
                
                'Open our csv file for appending
                fctl.Open fileLocation, 8
                
                If Err.Number <> 0 Then
                                ShowSystemAlarm "Error # " & CStr(Err.Number)& " " & Err.Description
                                Err.Clear
                End If

Step 4: Format the CSV File

To format the CSV file with some column headers, you can use the “Loc” and “Seek” commands to cleverly determine if this is an empty CSV file. “Loc” will return the current position that is being read/written to, and “Seek” will return the next position to be read/written to. If both of these are “1”, then we have opened the file for the first time. If this is the case, you can use the LinePrint method to add comma separated column headers to the top of your file.

currentPosition = fctl.Loc
                nextPosition = fctl.Seek
                
                'If we just created the file, write the header
                If currentPosition = 1 And nextPosition = 1 Then
                                fctl.LinePrint "Time, Data Point 1, Data Point 2, Data Point 3, Data Point 4, Data Point 5"
                End If

Step 5: Concatenate Data

Now you can concatenate all of your data into a comma-separated format and use the LinePrint method again to write to the CSV file. In my example, I'm using different time functions to create a timestamp for all of my data values to make each one unique.

currentTime = Hour(Time) & ":" & Minute(Time) & ":" & Second(Time)
                'Create our comma separated string of data values
                dataValues = currentTime & _
                                SmartTags("DataPoint1") & ", " & _
                                SmartTags("DataPoint2") & ", " & _
                                SmartTags("DataPoint3") & ", " & _
                                SmartTags("DataPoint4") & ", " & _
                                SmartTags("DataPoint5")

fctl.LinePrint dataValues
                fctl.Close

Step 6: Trigger the Script

Lastly, you just need something that will trigger the script to run. You have multiple options that provide varying levels of control.

  • Scheduled Tasks can be used to trigger the script automatically across longer time periods (1 minute - 1 year).
  • HMI tags can also be used to trigger a VBScript if there is a specific program event that should cause information to be logged.
  • Lastly, a button event can also be used to call the script.

Find out more about DMC's Siemens expertise.

Comments

There are currently no comments, be the first to post one.

Post a comment

Name (required)

Email (required)

CAPTCHA image
Enter the code shown above: