using System;
using System.Collections.Generic;
using System.Text;
using TVA;
using TVA.PhasorProtocols;

namespace GTosPMUtoData
{
  /// <summary>
  /// Sample Program adapted for the GridTrak Open Source SynchroPhasor PMU
  /// from openPDC sample: Device to Data in 5 Easy Steps.
  /// References:  http://openpdc.codeplex.com and http://gtospmu.codeplex.com
  /// 
  /// Mar 28, 2011  Updated
  /// </summary>
  class Program
  {
    static MultiProtocolFrameParser parser;
    static long frameCount;

    static void Main(string[] args)
    {
      Console.WriteLine("GTosPMU to Data for openPDC Test");
      // Create a new protocol parser
      parser = new MultiProtocolFrameParser();

      // Attach to desired events
      parser.ConnectionAttempt += parser_ConnectionAttempt;
      parser.ConnectionEstablished += parser_ConnectionEstablished;
      parser.ConnectionException += parser_ConnectionException;
      parser.ParsingException += parser_ParsingException;
      parser.ReceivedConfigurationFrame += parser_ReceivedConfigurationFrame;
      parser.ReceivedDataFrame += parser_ReceivedDataFrame;

      // Define the connection string
      parser.ConnectionString = "phasorProtocol=IeeeC37_118V1; transportProtocol=Serial; accessID=2; "
                                + "port=COM1; baudrate=115200; parity=None; stopbits=One; databits=8; "
                                + "dtrenable=False; rtsenable=False; autoStartDataParsingSequence = false;";

      // Start frame parser
      parser.AutoStartDataParsingSequence = true;
      parser.Start();

      // Hold the console open until the operator hits the <Enter> key
      Console.ReadLine();
    }


    static void parser_ReceivedDataFrame(object sender, EventArgs<IDataFrame> e)
    {
      // Increase the frame count each time a frame is received
      frameCount++;

      // Print information each time we receive 60 frames (every 2 seconds @ 30 fps)
      // Also check to make sure we have at least one protocol cell in the DataFrame; else ignore processing it.
      if ((frameCount % 60 == 0) && (e.Argument.Cells.Count > 0))
      {
        IDataCell device = e.Argument.Cells[0];
        Console.WriteLine("Received data frames so far =  " + frameCount.ToString());
        Console.WriteLine("  Last frequency =  ", device.FrequencyValue.Frequency.ToString("00.0000") + " Hz");
        for (int ii= 0; ii < device.PhasorValues.Count; ii++)
        {
          Console.WriteLine("  Last phase angle[" + ii.ToString() + "] =  " 
                            + device.PhasorValues[ii].Angle.ToString("##0.0000") + " rad");

          Console.WriteLine("  Last magnitude[" + ii.ToString() + "] =  " 
                            + device.PhasorValues[ii].Magnitude.ToString("##0.000") + " {device units}");
        }
        Console.WriteLine("    Last Timestamp =  " +((DateTime)device.Timestamp).ToString("yyyy-MM-dd HH:mm:ss.fff"));
      }
    }


    static void parser_ReceivedConfigurationFrame(object sender, EventArgs<IConfigurationFrame> e)
    {
      // Notify the user when a configuration frame is received
      Console.WriteLine("Received configuration frame with {0} device(s)", e.Argument.Cells.Count);
    }


    static void parser_ParsingException(object sender, EventArgs<Exception> e)
    {
      // Output the exception to the user
      Console.WriteLine("Parsing exception: {0}", e.Argument);
    }


    static void parser_ConnectionException(object sender, EventArgs<Exception, int> e)
    {
      // Display which connection attempt failed and the exception that occurred
      Console.WriteLine("Connection attempt {0} failed due to exception: {1}", e.Argument2, e.Argument1);
    }

    static void parser_ConnectionEstablished(object sender, EventArgs e)
    {
      // Notify the user when the connection is established
      Console.WriteLine("Initiating {0} {1} based connection...",
                        parser.PhasorProtocol.GetFormattedProtocolName(),
                        parser.TransportProtocol.ToString().ToUpper());
    }

    static void parser_ConnectionAttempt(object sender, EventArgs e)
    {
      // Let the user know we are attempting to connect
      Console.WriteLine("Attempting connection...");
    }

  }
}

Last edited Mar 29, 2011 at 2:20 AM by ajstadlin, version 1

Comments

No comments yet.