RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About ArchestrAnaut Blog
  • About Our Authors
  • Email Subscription
  •  

    Splitting a CSV into an IAS Array

    July 25th, 2011

    A fairly common activity if you are interacting with external systems is to exchange data with the external system via a CSV.  The primary reason people do this is to avoid having to create a large number of registers or data exchange elements for an unknown quantity of values. 

    Think about the following simple example.  Say you have a robot stacking boxes on a pallet.  For each job the robot does it has a list of legal boxes that can go on that pallet.  The data from your MES system might look like this.

    J1234, L1, L2, L3, L4

    This string may be saying a new job is “J1234”, and the list of acceptable lots/boxes that can go on this particular pallet is “L1, L2, L3, L4”

    One approach might be to run down the string character by character, peeling off characters and sticking them in some holding register then doing something with that value when you get to a delimiter.  It works but it’s really inefficient and quite messy.

    Regular expressions to the rescue!

    The first thing you need to figure out is how to write a regular expression to pick apart a CSV.  I’ve seen, and used, about 10 different approaches but I’ll start you off with the most simple one.

    [^,]+

    According to my trusty RegexBuddy explanation tool here is a technical explanation

    Match any character that is NOT a “,” between one and unlimited times, as many times as possible, giving back as needed (greedy)

    In english that means match all the characters up until you find a comma.  It doesn’t matter if it’s one comma or multiple commas.  Then start your next match when you find the next non-comma.  What’s neat about this approach is that you don’t have to worry about skipping over blanks.  It works automatically.  If you did care about blanks you could use a ? in place of the +.  This indicates you only want 1 match at a time.  Play with your favorite regex design tool (my favorite is Regex Buddy) and see what you get.

    So, on to the actual code.  It’s pretty short and commented so I’ll just do a dump here.

    ‘Dimension your variables

    Dim RegexObj as System.Text.RegularExpressions.Regex;
    Dim MatchResults As System.Text.RegularExpressions.MatchCollection;
    Dim Match As System.Text.RegularExpressions.Match;

    Dim Index as Integer;

    ‘Setup the Regular Expression with the configured Regex
    RegexObj = New System.Text.RegularExpressions.Regex(Me.Regex,System.Text.RegularExpressions.RegexOptions.IgnoreCase);

    ‘Use the regular expression to compare against the CSV
    MatchResults = RegexObj.Matches(Me.csv);

    ‘Redim the array to match the number of results
    Me.Array.Dimension1 = MatchResults.Count;

    ‘Loop through the matches and copy them into the array
    for each Match in MatchResults[]
         
    Index = Index + 1;
         
    Me.Array[Index] = Match;
    next;

    One of the neat tricks you’ll see is that I redim my array on the fly to the count of actual matches.  One piece of code I left out was clearing the array before populating it, but that is really oustide the scope of what we’re doing here.

    Another option you could use is the .Net split function or the Regex split functionality.  Either way there are lots of options for splitting CSV’s so try out a few and figure out what works best for you.

    - Andy


    Regular Expressions for Fun and Profit (Part 2 of 2)

    February 9th, 2011

    If you read our first post in the series( http://archestranaut.wordpress.com/2011/02/01/regular-expressions-for-fun-and-profit-part-1-of-2/) you are no doubt excited about the world of possibilities you open up when you combine Archestra with Regular Expressions.   In this post we’ll look at a real world example that we use with our clients to solve real world issues.

    Keeping time in System Platform is absolutely critical.  When I work with a client that is having connectivity and deployment issues this is one of the first areas I look at.  We’ve found that using standard NTP works just fine (except on your Historian) as far as an accuracy level but what if something happens and you start drifting?  How do you know you are drifting.  I guess you could do some fancy math in the historian while historizing timestamps on platforms but that gets messy.  Let’s put our new found skills to work.

    Read the rest of this entry »


    Bringing a PLC into the ActiveDirectory Fold

    November 5th, 2010

    Most of our IAS systems have a lot of vendor provided pieces of equipment (aka skids).  Those skids usually have their own PLC and HMI.  The HMI’s usually have some function to login and they apply security based on the users group or security level.  Most of them have common or shared accounts that everybody knows the password for (even the disgruntled guy you just fired).

    The other way that shows up a lot is having individual user account & privileges being role based.  If you have 15 users and 30 skids, that becomes a management nightmare trying to disable users, remember passwords, etc.  You could draw the same corollary to a bunch of Windows PCs’.  Security would be much easier to manage from a single location.  Our IAS system are always built on top of a Windows ActiveDirectory (AD) Domain.  This at least simplifies all of the SCADA security to be managed in one place:  the domain controller (DC).  Wouldn’t it be nice to use that to manage the skids too?

    Read the rest of this entry »


    Using .NET in IAS Scripting

    October 29th, 2010

    IAS is as flexible as or more flexible than any other platform out on the market in terms of the breadth of programming possibilities. The concept of inheritance makes it possible to make changes at a template level and all of its children (templates & instances) will be changed too. Equally as powerful is IAS’s inclusion of .NET in their QuickScript language.

    Read the rest of this entry »