////////////////////////////////////////////////////////////////////////////
// ZAO "NPF Micran" 28.10.2011
// This is an example for G7M generator with SCPI commands
// C# example 1.
// Fixed power and fixed frequency 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::GNM", 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, "SOURce:FREQuency:MODE CW\n");
			// Set fixed power mode
			status = Visa.Printf(rsrc, "SOURce:POWer:MODE FIXed\n");
			// Set frequency value
			status = Visa.Printf(rsrc, "SOURce:FREQuency 10.005GHz\n");
			// Set power level value
			status = Visa.Printf(rsrc, "SOURce:POWer:LEVel -10DBM\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);
		}
	}
}