////////////////////////////////////////////////////////////////////////////
// AO "NPF Micran" 26.04.2021
// This is an example for P2M with SCPI commands
// C# example 4.
// Power list and frequency list 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;
			
			status = Visa.GetDefaultRM(out rm);

			System.Console.Out.WriteLine("Enter device address:");
			addr = System.Console.In.ReadLine();

			// Open conection through MICS class
			status = Visa.Open(rm, "TCPIP::" + addr + "::8888::SOCKET::SNM", ViAccessMode.VI_EXCLUSIVE_LOCK, 5000, out rsrc);
			if (status != ViStatus.VI_SUCCESS)
			{
				Visa.StatusDesc(rm, status, buff); // Get status description
				System.Console.Out.WriteLine("viOpen: " + buff);
				System.Console.ReadKey();
				return;
			}
			// Set operation timeout (ms)
			Visa.SetAttribute(rsrc, ViAttr.VI_ATTR_TMO_VALUE, 3000);
			// Send commands
			// Send "Reset" command
			status = Visa.Printf(rsrc, "*RST\n");
			// Send "Clear status" command
			status = Visa.Printf(rsrc, "*CLS\n");
			// Set frequency list values
			status = Visa.Printf(rsrc, "SENSe:LIST:FREQuency 10MHz,100MHz,200MHz,500MHz,1000MHz,2GHz,5GHz,10GHz,15GHz,20GHz\n");
			// Set frequency list mode
			status = Visa.Printf(rsrc, "SENSe:FREQuency:MODE LIST\n");
			// Set power list values
			status = Visa.Printf(rsrc, "SOURce:LIST:POWer -10,-9,-8,-7,-6,-5,-4,-3,-2,-1\n");
			// Set power list mode
			status = Visa.Printf(rsrc, "SOURce:POWer:MODE LIST\n");
			// Set trigger automatic mode
			status = Visa.Printf(rsrc, "INITiate:CONTinuous ON\n");
			// Set power out on
			status = Visa.Printf(rsrc, "OUTPut:STATe ON\n");
			// Check errors
			status = Visa.Printf(rsrc, "SYSTem:ERRor?\n");
			status = Visa.Scanf(rsrc, "%T", buff);
			System.Console.Out.WriteLine(buff);

			// Wait for user action
			System.Console.Out.WriteLine("Press any key to exit...");
			System.Console.ReadKey();
			
			// Set power out off
			status = Visa.Printf(rsrc, "OUTPut:STATe OFF\n");
			status = Visa.Queryf(rsrc, "OUTPut:STATe?\n", "%T", __arglist(buff));
			
			// Close session
			status = Visa.Close(rsrc);

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