////////////////////////////////////////////////////////////////////////////
// AO "NPF Micran" 04.10.2016
// This is an example for PLS06
// Example 2.
// PLS SCPI programming sample mode
////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
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 PLS-06 serial number: ");
			addr = System.Console.In.ReadLine();

			// Open connection
			status = Visa.Open(rm, string.Format("USB0::0x2b70::0x0020::{0}::0::INSTR", addr), 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 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 Sample mode
			status = Visa.Printf(rsrc, "SENSe:DETector:FUNCtion SAMPLe\n");
			
			// Set frequency
			status = Visa.Printf(rsrc, "SENSe:FREQuency 50 MHz\n");

			// Set high resolution
			status = Visa.Printf(rsrc, "SENSe:TRACe:RESolution HRESolution\n");		
			
			// Set internal trigger
			status = Visa.Printf(rsrc, "TRIGger:SEQuence:SOURce INTernal\n");

			// Set trigger level
			status = Visa.Printf(rsrc, "TRIGger:SEQuence:LEVel -62.4\n");

			// Set continuous measurements
			status = Visa.Printf(rsrc, "INIT:CONTinuous ON\n");		
			
			// Set measurement time
			status = Visa.Printf(rsrc, "SENSe:TRACe:TIME 0.001\n");
			
			// Read data
			if (status == ViStatus.VI_SUCCESS)
				status = Visa.Printf(rsrc, "TRACe:DATA? HRES\n");
			
			// Parse IEEE 488.2 binary block	
			byte[] bytes = new byte[2];
			uint retCnt = 0;
			status = Visa.Read(rsrc, bytes, 2, out retCnt); // #n - number of bytes of size record
			if ((int)status < 0)
				return;
			uint numBytes = Convert.ToUInt32(Encoding.ASCII.GetString(bytes, 1, 1));
			if (numBytes == 0)
				return;
			bytes = new byte[numBytes];
			status = Visa.Read(rsrc, bytes, numBytes, out retCnt); // reading size of data
			if ((int)status < 0)
				return;
			uint dataSize = Convert.ToUInt32(Encoding.ASCII.GetString(bytes, 0, (int)numBytes));
			if (dataSize == 0)
				return;
			bytes = new byte[dataSize + 1];
			status = Visa.Read(rsrc, bytes, dataSize + 1, out retCnt);
			if ((int)status < 0)
				return;
			int points = (int)dataSize / 4;
			float[] fdata = new float[dataSize];
			for (int i = 0; i < points; i++)
			{
				try
				{
					byte[] point = new byte[4];
					// The first byte is most significant, need to revert every 4 bytes
					for (int j = 0; j < 4; j++)
						point[3 - j] = bytes[4 * i + j];
					// Conversion to float
					fdata[i] = BitConverter.ToSingle(point, 0);
					// Show element of array
					System.Console.Out.Write(fdata[i] + " ");
				}
				catch
				{
					System.Console.Out.WriteLine("Incorrect data");
				}
			}
			
			// Sample result
			System.Console.Out.Write("\nResult: ");
			status = Visa.Printf(rsrc, "FETCh?\n");
			status = Visa.Scanf(rsrc, "%T", buff);
			System.Console.Out.WriteLine(buff);
			
			// Read error status
			status = Visa.Queryf(rsrc, "SYSTem:ERRor?\n", "%T", __arglist(buff));
			System.Console.Out.WriteLine(buff);

			// Close session
			status = Visa.Close(rsrc);
			
			// Wait for user action
			System.Console.Out.WriteLine("Press any key to exit...");
			System.Console.ReadKey();

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