Siemens Wincc read line by line on CSV

Afonsoreis

Member
Join Date
Sep 2023
Location
portugal
Posts
8
Hello guys,


Im using a siemens with WINCC in a computer and need to import to tag´s data fom a specific row in a SCV file,



i have a working script but always read the last row



Sub VBFunction_2()

' Read_data_from_file()
Dim fso, f, field, MyZf
FileName = "C:\data.csv"

' Catch errors
On Error Resume Next

' Create file object
Set fso = CreateObject("Scripting.FileSystemObject")
If Err.Number <> 0 Then
ShowSystemAlarm "Error #" & CStr(Err.Number) & " " & Err.Description
Err.Clear
Exit Sub
End If

Set f = fso_OpenTextFile(FileName, 1, 0, -2)
If Err.Number <> 0 Then
ShowSystemAlarm "Error #" & CStr(Err.Number) & " " & Err.Description
Err.Clear
Exit Sub
End If

' Read values till end of file
Do While Not f.AtEndOfStream
MyZf = f.ReadLine
Loop

field = Split(MyZf, ";")

Read_Batchid = field(0)
Read_Temp = field(1)
Read_Presure = field(2)

' Tidy up
fso.Close
Set f = Nothing
Set fso = Nothing
ShowSystemAlarm "Readout of the data was successful!"


End Sub


What a need is to define in a tag a specific row, and import the data in that row.


Thank you all for your help.
 
You either need to create an array of MyZf to store every line of data, or...

currentRow = 0
targetRow = 5

' Read values till end of file
Do While (Not f.AtEndOfStream) and (currentRow < targetRow)
currentRow = currentRow +1
MyZf = f.ReadLine
Loop

In the line "targetRow = 5" you can replace 5 with the name of the tag you want.
 
Hello guys,


Im using a siemens with WINCC in a computer and need to import to tag´s data fom a specific row in a SCV file,



i have a working script but always read the last row



Sub VBFunction_2()

' Read_data_from_file()
Dim fso, f, field, MyZf
FileName = "C:\data.csv"

' Catch errors
On Error Resume Next

' Create file object
Set fso = CreateObject("Scripting.FileSystemObject")
If Err.Number <> 0 Then
ShowSystemAlarm "Error #" & CStr(Err.Number) & " " & Err.Description
Err.Clear
Exit Sub
End If

Set f = fso_OpenTextFile(FileName, 1, 0, -2)
If Err.Number <> 0 Then
ShowSystemAlarm "Error #" & CStr(Err.Number) & " " & Err.Description
Err.Clear
Exit Sub
End If

' Read values till end of file
Do While Not f.AtEndOfStream
MyZf = f.ReadLine
Loop

field = Split(MyZf, ";")

Read_Batchid = field(0)
Read_Temp = field(1)
Read_Presure = field(2)

' Tidy up
fso.Close
Set f = Nothing
Set fso = Nothing
ShowSystemAlarm "Readout of the data was successful!"


End Sub


What a need is to define in a tag a specific row, and import the data in that row.


Thank you all for your help.


You don't have any code to select a specific line. You just have the code below that reads in the whole file line by line until MyZF holds the value of the last line of the CSV.



Code:
' Read values till end of file 
Do While Not f.AtEndOfStream 
    MyZf = f.ReadLine
Loop
 
You either need to create an array of MyZf to store every line of data, or...

currentRow = 0
targetRow = 5

' Read values till end of file
Do While (Not f.AtEndOfStream) and (currentRow < targetRow)
currentRow = currentRow +1
MyZf = f.ReadLine
Loop

In the line "targetRow = 5" you can replace 5 with the name of the tag you want.


Thats it! Works perfect!



Thank you so much!
 
Hello guys,



sorry to bother you again, but I'm trying to count the total number of lines I have in my file.

I needed to count the number of lines in my csv and to be able to start from the last to the first.



Anyone could you help me please, i tried many ways but just didnt work.



Thanks a lot for the help!
 
As far as I know there is no way to read lines backwards directly using the standard file access functions.
However, the way to do it is to read the lines into an array then read them out in reverse, you will already have the number of lines in the variable "current lines".
For example:
Read the lines into an array larger than expected number of lines the file will ever have, Then just do another loop

Do While (Current Lines > 0)
My_ZF = My_Array(Current Lines)
Current Lines = Current Lines -1
Loop
Off hand you may need to sub 1 off the Current Lines as I assume that it will increment at EOF, i.e. if there are 20 lines the Current Lines variable may end up as 21.
 
What I thought of doing was counting the total number of lines, and then decreasing it in the code I have that allows me to read a specific line.
 
That would probably not work the
Myzf = f.Readln only works sequentially in a stream there is no way as far as I know to read them backwards is not supported, the way it works is it starts at 0 and then increments the line number, the function does not support reading a particular line it increments it's own pointer to point to the next line & I don't think there is a way to force it to read backwards. the other option is to have an index number within the file then do many loops to find the last one, but then you would have to loop again to find the next index number in the file & so on, that would require an itteration of the number of lines x the number of lines which is enormous so not even worth thinking about.
 
good morning,

I apologize, I think I didn't express myself well enough.


I want, every time I run the script, it makes me read a specific line in the CSV. This is already working with the code:


currentRow = 0
targetRow = 5

' Read values till end of file
Do While (Not f.AtEndOfStream) and (currentRow < targetRow)
currentRow = currentRow +1
MyZf = f.ReadLine
Loop

In the line "targetRow = 5" you can replace 5 with the name of the tag you want.


But now what i need is to know the number of lines in that file, so i can create a tag with that information, and then decrease n-1 every time a part is made, ex:


Nr of lines: 145
targetRow = 145



next part ( execute the script again with new targetRow nr)



Nr of lines: 145
targetRow = 144


....and so on until
Nr of lines: 145
targetRow = 1




Thanks all for the help
 
From your description it sounds like every time you want to run the script get the last -1
So:
It will depend if the file is always 145 lines long, if not then you will have to run a function first time only to find the number of lines in the file
Function Get_Length () as integer
Dim Dummy * create a dummy variable to read the line in
TargetRow = 0 *Set the TargetRow to 0 *Note: this has to be a global variable so it can be used elsewhere*
Do While (Not f.AtEndOfStream)
TargetRow = TargetRow +1
Dummy= f.ReadLine
Loop
End Function
Call the function once
TargetRow = Get_Length() Return the target row

So now you have the number of rows in the file in the targetRow i.e. 145

Now everytime you run the routine you have
Sub My_Sub()
Dim Temp * create a temp to read the line if not the one

Do While (Not f.AtEndOfStream) and (currentRow < targetRow)
If Currentrow = target row then
MyZf = f.ReadLine *so read the line into your variable*
TargetRow = TargetRow -1 *we have the target row so decrement the Target row
Exit Sub *if you have got to the line you want then exit
Else *otherwise you need to read the line into a temp so the internal pointer steps to next line
Temp = F.ReadLine
currentRow = currentRow +1
ENDIF
Loop

So in effect what is being done is initially we find the number of lines in the file so run the function Get_Length
Then every time you run the Sub to extract the line & decrement the TargetRow
Next time it will get the target row before the previous one until you are at the first line.
I hope I understand what you are trying to achieve
 
Thats it! its working.



Im using 2 diferent VBscritps, one for the total nr of rows (only executed once), and then the other for read individualy.



Then i decrease the row i want to pick with a ladder function.


Thank you all for the exelent help.


cheers
 
Glad we could help, sorry my VB is a little rusty so the code I showed may be a little flaky it's a few years since I used it.
 

Similar Topics

Hello colleagues, On a Siemens Multi panel (MP 377 12") of a machine (S7-300) at a customer's site, some parameters need to be changed that have...
Replies
7
Views
308
I thought I was nearly finished on this TIA Portal/s7-1212C project (famous last words)... Up until now, I'd developed the PLC/HMI such that the...
Replies
10
Views
1,566
Hi, I have a HMI which is a KTP1200 Basic. Let's suppose USER03 is logged in. How do I create a simple "Lock Screen" that appears if the screen...
Replies
2
Views
678
Hi, At the moment, If the user fails to provide a valid username/password, the image attached appears "Invalid password or user name. Logon has...
Replies
12
Views
1,580
On my HMI (KTP1200), I've got this button "Generate Report", which generates a report (CSV) when it's pressed. (see pic) While it's being...
Replies
1
Views
549
Back
Top Bottom