////////////////////////////////////////////////////////////////////////////
// AO "NPF Micran" 04.10.2016
// This is an example for PLS06
// Example 1.
// PLS SCPI programming Average mode
////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using MiVisa;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			StringBuilder buff = new StringBuilder(1000);
			ViStatus status;
			ViSession rm, rsrc;
			string addr, PID;
			status = Visa.GetDefaultRM(out rm);

			System.Console.Out.WriteLine("Enter PLS serial number:");
			addr = System.Console.In.ReadLine();
			System.Console.Out.WriteLine("Select a model (1 - PLS-06, 2 - PLS-26, 3 - PLS-50):");
			PID = System.Console.In.ReadLine();

			// Open connection
			status = Visa.Open(rm, string.Format("USB0::0x2b70::0x002{0}::{1}::0::INSTR", int.Parse(PID) - 1, addr), ViAccessMode.VI_EXCLUSIVE_LOCK, 5000, out rsrc); // use PIDs: 0x0020 for PLS-06, 0x0021 for PLS-26, 0x0022 for PLS-50
			if (status != ViStatus.VI_SUCCESS)
			{
				Visa.StatusDesc(rm, status, buff); // Get status description
				System.Console.Out.WriteLine("viOpen: " + buff);
				System.Console.ReadKey();
				return;
			}

			// Set timeout [ms]
			status = Visa.SetAttribute(rsrc, ViAttr.VI_ATTR_TMO_VALUE, 3000);

			// Send command "Clear status"
			status = Visa.Printf(rsrc, "*CLS\n");			
			
			// Send command "Reset"  (all settings to «default»)
			status = Visa.Printf(rsrc, "*RST\n");
			
			// Read ID device information
			status = Visa.Queryf(rsrc, "*IDN?\n", "%T", __arglist(buff));
			System.Console.Out.WriteLine("IDN: " + buff);

			// Set to Average mode
			status = Visa.Printf(rsrc, "SENSe:DETector:FUNCtion AVERage\n");

			// Set manual averaging
			status = Visa.Printf(rsrc, "SENSe:AVERage:COUNt:AUTO OFF\n");
			status = Visa.Printf(rsrc, "SENSe:AVERage:COUNt 10\n");
			
			// Set frequency
			status = Visa.Printf(rsrc, "SENSe:FREQuency 50MHz\n");
		
			// Get measurement units
			status = Visa.Queryf(rsrc, "UNIT:POWer?\n", "%T", __arglist(buff));
			System.Console.Out.WriteLine("units: " + buff);

			// Set triggered measurements
			status = Visa.Printf(rsrc, ":INITiate:CONTinuous OFF\n");

			// Read error
			status = Visa.Queryf(rsrc, "SYSTem:ERRor?\n", "%T", __arglist(buff));
			System.Console.Out.WriteLine("Error: " + buff);
		
			// Measure and get average result 5 times
			for (int i = 0; i < 5; i++)
			{
				status = Visa.Printf(rsrc, ":INITiate:IMMediate\n");
				status = Visa.Printf(rsrc, "*OPC?\n");
				do
				{
					status = Visa.Scanf(rsrc, "%T", buff);
				}
				while (status == ViStatus.VI_ERROR_TMO);
				status = Visa.Printf(rsrc, "FETCh:SCALar:POWer:AC?\n");
				status = Visa.Scanf(rsrc, "%T", buff);
				System.Console.Out.WriteLine("power = " + buff);
			}
			
			// Read error
			status = Visa.Queryf(rsrc, "SYSTem:ERRor?\n", "%T", __arglist(buff));
			System.Console.Out.WriteLine("Error: " + buff);
			
			// Wait for user action
			System.Console.Out.WriteLine("Press any key to exit...");
			System.Console.ReadKey();

			// Close session
			status = Visa.Close(rsrc);

			// Close resource manager
			Visa.Close(rm);
		}
	}
}