VB script csv file in winccflexible

Google open/close statements in vb, you should be able to find what you need to get you started.
 
Below is a sample script code that will log values in a CSV file.
There will be a header line with description of each column.
The log file will be restarted with a new file name if the date changes.

The values to be logged is in an tag array "MXLV".
You should add a separate tag that is counted up each time you want to log the values. Add the script as a function with Change_value trigger on the counting tag.

You can expand on the code to get further lines with more descriptions as you want it.

Code:
Dim fso, f, ts, DataSet 
Set fso = CreateObject("Scripting.FileSystemObject")
 
SmartTags("LOG_WriteFileName") = "C:\LOGS\LOG_" & CStr(DatePart("yyyy",Date)) & "_" & CStr(DatePart("m",Date)) & "_" & CStr(DatePart("d",Date)) & ".csv"
 
DataSet = CStr(Now) & "," & CStr(SmartTags("MXLV")(0)) & "," & CStr(SmartTags("MXLV")(1)) & "," & CStr(SmartTags("MXLV")(2)) & "," & CStr(SmartTags("MXLV")(3))& "," & CStr(SmartTags("MXLV")(4))& "," & CStr(SmartTags("MXLV")(5))& "," & CStr(SmartTags("MXLV")(6))& "," & CStr(SmartTags("MXLV")(7))& "," & CStr(SmartTags("MXLV")(8)) & "," & CStr(SmartTags("MXLV")(9))
 
If Not fso.FileExists(SmartTags("LOG_WriteFileName")) Then
fso.CreateTextFile SmartTags("LOG_WriteFileName")
Set f = fso.GetFile(SmartTags("LOG_WriteFileName"))
Set ts = f.OpenAsTextStream(8, -2)
ts.WriteLine "Timestamp , headertext 1 , headertext 2 , headertext 3 , headertext 4 , headertext 5 , headertext 6 , headertext 7 , headertext 8 , headertext 9 , headertext 10 " 
ts.WriteLine DataSet
ts.Close
Else
Set f = fso.GetFile(SmartTags("LOG_WriteFileName"))
Set ts = f.OpenAsTextStream(8, -2)
ts.WriteLine DataSet
ts.Close
End If
 
If SmartTags("LOG_DisplayFileName") = "" Then SmartTags("LOG_DisplayFileName") = SmartTags("LOG_WriteFileName") 
 
Set ts = Nothing
Set f = Nothing
Set fso = Nothing
 
Here is the code with some comments added.

Code:
[left]Dim fso, f, ts, DataSet 
Set fso = CreateObject("Scripting.FileSystemObject")
[/left]
[left]'build the string with filename.[/left]
[left]'example: "LOG_2007-10-02.csv" [/left]
[left]'you may have to modify the path and filename to your needs.
SmartTags("LOG_WriteFileName") = "C:\LOGS\LOG_" & CStr(DatePart("yyyy",Date)) & "_" & CStr(DatePart("m",Date)) & "_" & CStr(DatePart("d",Date)) & ".csv"
[/left]
[left]'build the string with the data.[/left]
[left]'there is an array tag MXLV with the data.[/left]
[left]'the data are REALs as you then have the decimal point in the right place.[/left]
[left]'modify MXLV to suit your needs.
DataSet = CStr(Now) & "," & CStr(SmartTags("MXLV")(0)) & "," & CStr(SmartTags("MXLV")(1)) & "," & CStr(SmartTags("MXLV")(2)) & "," & CStr(SmartTags("MXLV")(3))& "," & CStr(SmartTags("MXLV")(4))& "," & CStr(SmartTags("MXLV")(5))& "," & CStr(SmartTags("MXLV")(6))& "," & CStr(SmartTags("MXLV")(7))& "," & CStr(SmartTags("MXLV")(8)) & "," & CStr(SmartTags("MXLV")(9))
 [/left]
[left]'check if the file already exists[/left]
[left]'if it does not, create it, write the header line and then the first data line[/left]
[left]'if it does, then just write the data line[/left]
[left]'update the header line t suit your needs
If Not fso.FileExists(SmartTags("LOG_WriteFileName")) Then
fso.CreateTextFile SmartTags("LOG_WriteFileName")
Set f = fso.GetFile(SmartTags("LOG_WriteFileName"))
Set ts = f.OpenAsTextStream(8, -2)
ts.WriteLine "Timestamp , headertext 1 , headertext 2 , headertext 3 , headertext 4 , headertext 5 , headertext 6 , headertext 7 , headertext 8 , headertext 9 , headertext 10 " 
ts.WriteLine DataSet
ts.Close
Else
Set f = fso.GetFile(SmartTags("LOG_WriteFileName"))
Set ts = f.OpenAsTextStream(8, -2)
ts.WriteLine DataSet
ts.Close
End If
 [/left]
[left]'if you need to display the same data from within the HMI, you can write the path+filename to another string.
If SmartTags("LOG_DisplayFileName") = "" Then SmartTags("LOG_DisplayFileName") = SmartTags("LOG_WriteFileName") 
 
Set ts = Nothing
Set f = Nothing
Set fso = Nothing[/left]
enjoy.
 
Thanks for quick reply

I have tried it.crate one button in Wincc and trigger the script
but there is no file and data at given location
I think i am confused at your description line
as "You shoould add a seperate tag that is counted up each time you want to log the values" Please explain it.
I have normal tags in my projest (i have no arry tags in my project)
Please explain how can i achive it with normal tags(real,Int bool)
What should be the type of the Tags
"LOG_WritefileName"
"LOG_DisplayfileName"
I am at the site for commissioning the project please request for the quick reply
Warm Regards
Nehe
 
I think i am confused at your description line
as "You shoould add a seperate tag that is counted up each time you want to log the values" Please explain it.
This is to trigger the datalogging from the PLC.
When there is an event that should mean that data should be logged, you simply increment the counter by 1 in the PLC. In the HMI there must be a change_value trigger on the counter tag, and this shall activate the script.
Remember to reset the counter if it goes above for example 10000.

I have normal tags in my projest (i have no arry tags in my project)
Please explain how can i achive it with normal tags(real,Int bool)
I recommend REALs, otherwise you have to worry about how to place a decimal point.
To use your own tags, just substitute ("MXLV")(n) with your own tag names. You dont have to put an (n) after the tag for non-array tags.

What should be the type of the Tags
"LOG_WritefileName"
"LOG_DisplayfileName"
STRING tags, and Internal tags only.

I am at the site for commissioning the project please request for the quick reply
If you didn't get help on this site, what would you have done then ? Made too many promises maybe ?

Anyway, I think the script is fairly simple, and is a good starting point. There are many ways to do this, and you can easily add functions such as header and footer lines when you realise that you simply write one line at a time.
 
Dim fso, f, ts, DataSet
Set fso = CreateObject("Scripting.FileSystemObject")

'build the string with filename.​

'example: "LOG_2007-10-02.csv"​

'you may have to modify the path and filename to your needs.
SmartTags("LOG_WriteFileName") = "C:\LOGS\LOG_" & CStr(DatePart("yyyy",Date)) & "_" & CStr(DatePart("m",Date)) & "_" & CStr(DatePart("d",Date)) & ".csv"---(LOG_WriteFileName-IS IT INTARNAL TAG?)

'build the string with the data.​

'there is an array tag MXLV with the data.​

'the data are REALs as you then have the decimal point in the right place.​

'modify MXLV to suit your needs.
DataSet = CStr(Now) & "," & CStr(SmartTags("MXLV")(0)) & "," & CStr(SmartTags("MXLV")(1)) & "," & CStr(SmartTags("MXLV")(2)) & "," & CStr(SmartTags("MXLV")(3))& "," & CStr(SmartTags("MXLV")(4))& "," & CStr(SmartTags("MXLV")(5))& "," & CStr(SmartTags("MXLV")(6))& "," & CStr(SmartTags("MXLV")(7))& "," & CStr(SmartTags("MXLV")(8)) & "," & CStr(SmartTags("MXLV")(9))
'(HOW? I HAVE TAG ARRY BUT THE NORMAL DESCRITE TAGS HOW CAN I EDIT IT HERE)

'check if the file already exists​

'if it does not, create it, write the header line and then the first data line​

'if it does, then just write the data line​

'update the header line t suit your needs
If Not fso.FileExists(SmartTags("LOG_WriteFileName")) Then
fso.CreateTextFile SmartTags("LOG_WriteFileName")
Set f = fso.GetFile(SmartTags("LOG_WriteFileName"))
Set ts = f.OpenAsTextStream(8, -2)
ts.WriteLine "Timestamp , headertext 1 , headertext 2 , headertext 3 , headertext 4 , headertext 5 , headertext 6 , headertext 7 , headertext 8 , headertext 9 , headertext 10 "
ts.WriteLine DataSet
ts.Close
Else
Set f = fso.GetFile(SmartTags("LOG_WriteFileName"))
Set ts = f.OpenAsTextStream(8, -2)(WHAT IS THE PURPOSE OF THIS STATEMENT )
ts.WriteLine DataSet
ts.Close
End If

'if you need to display the same data from within the HMI, you can write the path+filename to another string.
If SmartTags("LOG_DisplayFileName") = "" Then SmartTags("LOG_DisplayFileName") = SmartTags("LOG_WriteFileName")
(I NEED TO DISPLY THE REPORT ON ONE SCREEN OF WINCC WHICH I WILL CALL BY ONE BUTTON)​
Set ts = Nothing
Set f = Nothing
Set fso = Nothing​
 
Dim fso, f, ts, DataSet​
Set fso = CreateObject("Scripting.FileSystemObject")


'build the string with filename.​

'example: "LOG_2007-10-02.csv"​

'you may have to modify the path and filename to your needs.​
SmartTags("LOG_WriteFileName") = "C:\LOGS\LOG_" & CStr(DatePart("yyyy",Date)) & "_" & CStr(DatePart("m",Date)) & "_" & CStr(DatePart("d",Date)) & ".csv"---(LOG_WriteFileName-IS IT INTARNAL TAG?)


'build the string with the data.​

'there is an array tag MXLV with the data.​

'the data are REALs as you then have the decimal point in the right place.​

'modify MXLV to suit your needs.​
DataSet = CStr(Now) & "," & CStr(SmartTags("MXLV")(0)) & "," & CStr(SmartTags("MXLV")(1)) & "," & CStr(SmartTags("MXLV")(2)) & "," & CStr(SmartTags("MXLV")(3))& "," & CStr(SmartTags("MXLV")(4))& "," & CStr(SmartTags("MXLV")(5))& "," & CStr(SmartTags("MXLV")(6))& "," & CStr(SmartTags("MXLV")(7))& "," & CStr(SmartTags("MXLV")(8)) & "," & CStr(SmartTags("MXLV")(9))
'(HOW? I HAVE TAG ARRY BUT THE NORMAL DESCRITE TAGS HOW CAN I EDIT IT HERE)

'check if the file already exists​

'if it does not, create it, write the header line and then the first data line​

'if it does, then just write the data line​

'update the header line t suit your needs​
If Not fso.FileExists(SmartTags("LOG_WriteFileName")) Then
fso.CreateTextFile SmartTags("LOG_WriteFileName")
Set f = fso.GetFile(SmartTags("LOG_WriteFileName"))
Set ts = f.OpenAsTextStream(8, -2)
ts.WriteLine "Timestamp , headertext 1 , headertext 2 , headertext 3 , headertext 4 , headertext 5 , headertext 6 , headertext 7 , headertext 8 , headertext 9 , headertext 10 "
ts.WriteLine DataSet
ts.Close
Else
Set f = fso.GetFile(SmartTags("LOG_WriteFileName"))
Set ts = f.OpenAsTextStream(8, -2)(WHAT IS THE PURPOSE OF THIS STATEMENT )
ts.WriteLine DataSet​
ts.Close
End If


'if you need to display the same data from within the HMI, you can write the path+filename to another string.​
If SmartTags("LOG_DisplayFileName") = "" Then SmartTags("LOG_DisplayFileName") = SmartTags("LOG_WriteFileName")
(I NEED TO DISPLY THE REPORT ON ONE SCREEN OF WINCC WHICH I WILL CALL BY ONE BUTTON)
Set ts = Nothing​
Set f = Nothing
Set fso = Nothing
 
See my previous reply.

About "(I NEED TO DISPLY THE REPORT ON ONE SCREEN OF WINCC WHICH I WILL CALL BY ONE BUTTON)":
Siemens have made a sample project where you can read a CSV file and display it on a screen.
However I do not recommend it. It is very cumbersomly programmed, and the last time I tried to use it, the script refused to run with no error messages whatsoever - it just didnt want to run.

I strongly recommend to avoid making the scripts too big or complicated.

If there is no Excel on the PC, then try to use a CSV viewer such as the free DiamondCS View:
http://www.diamondcs.com.au/freeutilities/csview.php
 
DEAR SIR
THANKS A LOT
CAN YOU ANSWER ONLY ONE QUIRY PLEASE
1.Set ts=f.OpenAsTextStream(8 , -2)---????????
2.Where is the trigger tag in the script?

This is the situation beause i beleve on the siemens inbuilt report function.that client is not accepting.

your help is GOD help for me
Thanks once again
Warm regards
Nehe
 
1.Set ts=f.OpenAsTextStream(8 , -2)---????????
Just dont change it.
When the data has to be written to the file, the file must be opened, the data is written, and the file is finally closed.

2.Where is the trigger tag in the script?
It is the other way around. The trigger tag triggers the script, so the tag is not in the script. Follow my previous suggestion to achieve that the script is activated from the PLC.
 

Similar Topics

Hello everyone, because I'm not familiar with Archestra-Scripts, I have a question. I want to create a file in which a time stamp, and behind...
Replies
0
Views
1,357
Hello guys. I need some help. Is there anyone who can provide sample script for importing csv file to the user archive of wincc. Thank you very much.
Replies
0
Views
3,518
Hi I'm trying to import and export datasets of a project in CSV files, using a USB key. The OP is Siemens MP277. The user must be able to export...
Replies
0
Views
5,875
Hello, I am learning to create shapes and VB Scripts in HMIWeb Display Builder. I wanted to change color of my rounded rectangle by script. I...
Replies
0
Views
95
Hello, I have a quick question. I have been using scripts to change the color of buttons. The reason, I am usually using multiple hmiruntime.tags...
Replies
1
Views
92
Back
Top Bottom