////////////////////////////////////////////////////////////////////////////
// AO "NPF Micran" 26.04.2021
// This is an example for P2M with SCPI commands
// C# example 3.
// Power sweep with fixed frequency
////////////////////////////////////////////////////////////////////////////
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 fixed frequency mode
			status = Visa.Printf(rsrc, "SENSe:FREQuency:MODE CW\n");
			// Set frequency
			status = Visa.Printf(rsrc, "SENSe:FREQuency 10.005GHz\n");
			// Set power sweep mode
			status = Visa.Printf(rsrc, "SOURce:POWer:MODE SWEEp\n");
			// Set power range values
			status = Visa.Printf(rsrc, "SOURce:POWer:STARt -10DBM;STOP 10DBM\n");
      // Set points count
			status = Visa.Printf(rsrc, "SENSe:SWEEp:POINts 101\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);
		}
	}
}