Quantcast
Channel: kristofdba – kristof dba
Viewing all articles
Browse latest Browse all 26

Powershell: Handling OSISOFT pi UFL interface files

$
0
0

Since PI UFL (the last resort “load my data interface” for CSV files) is a burden to manage if you have ‘big’ inerface .INI file i created some script to troubleshoot issues. They can be used to work with CSV files for all kinds of things and are based on using Arrays and counting in arrays to get specific positions of values.

An Example.
The UFL ini file dictates which CSV value contains which data using a notation as shown below.

from the 15 values in the CSV string the 9th value contains X .
*,*,*,*,*,*,*,*,(*),*,*,*,*,*,*

Real files sometimes have over 250 Values ( we even have + 1 K).
If you need to verify value 659 in 1000 files…You see how this can be taunting to troubleshoot.

A string with data looks like this where 258 is the value contained in the position (9th), as declared above (…*,(*),*…) .

123,165, 4,-392, 50, 4,1, 17, 258, 50, 6,1,-338,1,-315

Fields have been declared earlier in the file as a ‘tag’

FIELD(1).Name = “Tag1”
FIELD(2).Name = “Tag2”
FIELD(3).Name = “Tag3”
FIELD(4).Name = “Tag4”
FIELD(5).Name = “Tag5”
FIELD(6).Name = “Tag6”
FIELD(7).Name = “Tag7”
FIELD(8).Name = “Tag8”
FIELD(9).Name = “Tag9”
FIELD(10).Name = “Tag10”
FIELD(11).Name = “Tag11”
FIELD(12).Name = “Tag12”
FIELD(13).Name = “Tag13”
FIELD(14).Name = “Tag14”
FIELD(15).Name = “Tag15”

And thus positions are ‘marked’ like this

tag1 = [(*),*,*,*,*,*,*,*,*,*,*,*,*,*,*]
tag2 = [*,(*),*,*,*,*,*,*,*,*,*,*,*,*,*]
tag3 = [*,*,(*),*,*,*,*,*,*,*,*,*,*,*,*]
tag4 = [*,*,*,(*),*,*,*,*,*,*,*,*,*,*,*]
tag5 = [*,*,*,*,(*),*,*,*,*,*,*,*,*,*,*]
tag6 = [*,*,*,*,*,(*),*,*,*,*,*,*,*,*,*]
tag7 = [*,*,*,*,*,*,(*),*,*,*,*,*,*,*,*]

etc. …
Again, try to imagine how a file of + 250 positions would look like…

To the good stuff: Some code to dive into the CSV’s …
The example, we are looking for tag PUMP1PRESSURE in a file with about 300 lines.
we do not know the position in the file (yet) and instead of counting comma’s (,) we’ll have powershell counting the commas for us.

#First we need to get the position of the string we’re looking for.
#Copy the definition from the uFL file to the $positionstring variable and ditch the [‘s and the ]’s and all other noise.
#FYI => The first position in an array is always ‘0’ . so if you need value 4, use 3 for 9 use 8 etc. …
#since we are using a dynamic value based on the INI definition and the lines of the csv are also parsed as an array (Line.split) this maps 1 on 1.


$positionstring = "*,*,*,*,*,*,*,*,(*),*,*,*,*,*,*,"
$Separator = ","
$positionstringparts = $positionstring.split($separator)
$count =[array]::IndexOf($positionstringparts,'(*)')
#print the position on screen
echo $count
#Now open the csv file and look for the value(s) on position $count
#Get value on given place
$file= "D:\PLC_19993112130620.txt"
$String = get-content $file
Foreach ($line in $string)
{
$Separator = ","
$parts = $line.split($separator)
echo $parts[$count]

}

The result will be a list of the values like this:

168
168
167
168
167
167
168
168
168
168
168
167
168

If you have a timestamp in your strings, you can easily add id by adding

echo $parts[n]
where N is the position of your timestamp in the array, most of the time it’s the first value.
Being position .. 0 in the array 😉

Et voila!


Viewing all articles
Browse latest Browse all 26

Trending Articles