Querying CPU data
This example demonstrates access to IPC diagnostic data via a C# program. CPU data (clock frequency and load) are read as examples.
The library MdpNetApi.dll required for this purpose can be found in the installation directory of the IPC diagnostics (default directory: C:\Program Files (x86)\Beckhoff\IPC-Diagnostics).
Program code:
using System.Text;
using System.IO; // namespace for the Memory Stream, Stream- und Binary-Reader
using MdpNetApi; // namespace of the MDP .Net API
namespace MdpCsharpHwRead
{
class Program
{
static void Main(string[] args)
{
////////////////////////////////////////////////////////////////////////////////////////////////
// initialization
// opens a connection to the MDP
MdpNetMethods.MDPOpen();
// create Memory Stream, Stream- and Binary-Reader for handeling data
MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(1);
BinaryReader binReader = new BinaryReader(memoryStream);
StreamReader strReader = new StreamReader(memoryStream);
// title/header of console output
Console.WriteLine("Hardware Information (via MDP Csharp Sample)\n==========================================");
Console.WriteLine("");
////////////////////////////////////////////////////////////////////////////////////////////////
// Read module information
// Get CPU information (CPU frequency & load)
memoryStream.Position = 0;
// create a variable containing the base address of a specific module type
ushort nBaseAddress = GetAdressOfModuleType(MdpModuleType.CPU)[0];
MdpStatusCode statusCode = MdpNetMethods.MDPSyncRead((ushort)(nBaseAddress + 1), 1, 0, ref memoryStream);
Console.WriteLine("CPU Frequenzy: " + binReader.ReadUInt32());
// actual CPU Usage in Percent
memoryStream.Position = 0;
statusCode = MdpNetMethods.MDPSyncRead((ushort)(nBaseAddress + 1), 2, 0, ref memoryStream);
Console.WriteLine("CPU Usage: " + binReader.ReadUInt16() + "%");
Console.WriteLine("");
////////////////////////////////////////////////////////////////////////////////////////////////
// closing
// close Memory Stream, Stream- and Binary-Reader
strReader.Close();
binReader.Close();
memoryStream.Close();
// close the connection to the MDP
MdpNetMethods.MDPClose();
// prevent console of closing
Console.ReadLine();
}
// returns all adresses of the given module type
static ushort[] GetAdressOfModuleType(MdpModuleType type)
{
// create Memory Stream, Stream- and Binary-Reader for handeling data
MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(1);
BinaryReader binReader = new BinaryReader(memoryStream);
StreamReader strReader = new StreamReader(memoryStream);
// read operation on "Device Type List" to get maximum count of modules and all module types
MdpStatusCode statusCode = MdpNetMethods.MDPSyncRead(0xF010, 0, 1 /*flag to read the whole table*/, ref memoryStream);
// get count of modules
byte nCount = binReader.ReadByte();
memoryStream.Position++;
// create temporary list
List<ushort> adresses = new List<ushort>();
// check every module on its type
for (int i = 0; i < nCount; i++)
{
// move MemoryStream to correct position
memoryStream.Position += 2;
ushort deviceType = binReader.ReadUInt16();
// check if actual device type is desired
if (deviceType == (ushort)type)
// add it to the list
adresses.Add(((ushort)((0x8000) + (0x0010 * i))));
}
return adresses.ToArray();
}
}
}