////////////////////////////////////////////////////////////////////////////
// AO "NPF Micran" 11.03.2020
// This is an example for G7M-50 with SCPI commands
// Setting fixed power and 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);
			ViSession rm, rsrc;
			ViStatus status;
			status = Visa.GetDefaultRM(out rm);
			Console.WriteLine("Enter device address: ");
			string addr = Console.ReadLine();
			string session = string.Format("TCPIP::{0}::5025::SOCKET", addr);
			// Open conection
			status = Visa.Open(rm, session, ViAccessMode.VI_EXCLUSIVE_LOCK, 10000, 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 timeout
			MiVisa.ViAttr VI_ATTR_TMO_VALUE = (MiVisa.ViAttr)0x3FFF001A;
			status = Visa.SetAttribute(rsrc, VI_ATTR_TMO_VALUE, 5000);
			
			// Send parameters
			// Device ID
			status = Visa.Queryf(rsrc, "*IDN?\n", "%T", __arglist(buff));
			System.Console.Out.WriteLine("IDN: " + buff);
			// Send "Preset" command
			status = Visa.Printf(rsrc, "SYSTem:PRESet\n");
			System.Threading.Thread.Sleep(500);
			// Send "Clear status" command
			status = Visa.Printf(rsrc, "*CLS\r\n");
			status = Visa.Printf(rsrc, "SOURce:FREQuency:MODE FIXed\n");
			status = Visa.Printf(rsrc, "SOURce:POWer:MODE FIXed\n");
			status = Visa.Printf(rsrc, "SOURce:FREQuency:CW 500000000\n");
			status = Visa.Printf(rsrc, "SOURce:POWer:LEVel 0\n");
			status = Visa.Printf(rsrc, "OUTPut:STATe ON\n");
			// Check errors
			status = Visa.Queryf(rsrc, "SYSTem:ERRor?\n", "%T", __arglist(buff));
			System.Console.Out.WriteLine("Error: " + buff);

			
			// Wait for user action
			System.Console.ReadKey();
			// Close session
			status = Visa.Close(rsrc);
			// Close resource manager
			Visa.Close(rm);
		}
	}
}