| OPC Enabled Performance Counters | | Print | |
Making Windows Performance Counters available to OPCSource Code | OPC Foundation Runtime | Kepware
To solve this we have done a little trick using Kepware Simulation Server (http://www.kepware.com) and a C# application to copy the counter values to OPC. Kepware’s simulation server is a free (not supported) part of the Kepware package that allows you to define you own server tags without having to build a custom OPC server. If our application is already using Kepware, all you need to do is ensure that the Simulation Server is installed. To build the C# application you can use Visual Studio or the Visual Studio express. To communicate with the OPC services, download the OPC foundations runtime from http://www.opcfoundation.org . My first step was to author a quick application that would explorer your computer’s list of available performance counters. This application would allow me to generate a Kepware export CSV file that contains OPC tags that represent my performance counters. This utility application will also generate a tag configuration file that will be used by my other application that will read the performance counter and write the counter values to Kepware OPC. Finding Performance Counters Installed To get started, obtain a list of categories of counters installed on your PC. This will require the System.Diagnostics namespace to be included in your application. using System.Diagnostics;… PerformanceCounterCategory[] perfCounters = PerformanceCounterCategory.GetCategories(); Once you have a list of categories of counters, you can then query what types of counters exists. If your working with the category of LogicalDisk, you may see counter types of % Free Space C:, Disk Reads/Writes. To get the list of counters you will need to performanceCounterCategory.GetCounters(). PerformanceCounter[] counterList = perfCategory.GetCounters();Before you go calling GetCounters() on all of your categories, you will need to make sure that the category your working with does not have Instances of counters. If there are no instances of counters contained for the given category, then a simple call to performanceCounterCategory.GetCounters() will be fine. To determine if there are instances, then you will need to make the call to performanceCounterCategory.GetInstanceNames(). This will return a “string[]” of instance names. Use each instance name as a parameter to the performanceCounterCategory.GetCounters(name) to find all of the counters. Performance Counter to OPC Background ProcessingPerfMonToOPCUtil This utility will allow creating the configuration files required by our PerfMonToOPC process and the Kepware configuration server. Once you have generated a performance counter list, the counters will need to be converted to tags so they can be imported into Kepware. Some characters will need to be removed from the counter names for this to work correctly. For example, if you have Microsoft.NET installed, all of the “.Net * counters” would have trouble loading into Kepware because of the ‘.’ symbol. In a Kepware import file a dot ‘.’ will represent a new group rather than part of a itemname or group. To get around this, when I export available counters I replace the ‘.’ with the letters ‘dot’. Once my files are created, “kepware.csv” and “perflist.config” for kepware and PerfMonToOPC we are ready to run our application.
PerfMonToOPCUtil When our performance counter utility starts up, we first check to see if the perflist.config file exists. Next load the performance counter file in to a generic List<T>. Now that our counter list is ready, we will begin registering each corresponding tag we created in kepware and matching the Opc.Da.ItemValue with a PerformanceCounterDefinition. Our main processing loop will connect to the OPC Server, instantiate the performance counters and then finally loop forever while we read performance counter values and write to the OPC server. Example of our primary thread processing loop: public void ThreadProc() { Activate counter will execute the following code based on the condition if the counter is an instance counter or a single counter. if (_Instance == null) After we loop through all of our performance counters, we will write to the OPC Server with updated values. private void WritePerformanceCountersToOPC() {Opc.Da.ItemValue[] itemArray = new Opc.Da.ItemValue[Program._CounterList.Count]; for (int i = 0; i < Program._CounterList.Count; i++) { PerformanceCounterDefinition def = Program._CounterList.ElementAt(i); def.OPCItem.Value = (float)def.CounterValue; itemArray[i] = def.OPCItem; } _OpcServer.Write(itemArray); }
public Opc.Da.ItemValue OPCItem { get { if (_OPCItem == null) { _OPCItem = new Opc.Da.ItemValue(OPCTagName); _OPCItem.ClientHandle = Guid.NewGuid(); } _OPCItem.Value = CounterValue; return _OPCItem; } }
|

