using System; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; using NationalInstruments.VisaNS; // Add Reference->NationalInstruments.VisaNS & NationalInstruments.common by installing NI-VISA .NET4 files from its Development Support // Change Project Example Properties Applicaiton Target fromwork to ".Net Framework 4" namespace Example3_PA { public partial class Form1 : Form { private MessageBasedSession mbSession; //Instrument VISA address string resourceString = "USB0::0x0699::0x4001::100010200013::INSTR"; public Form1() { InitializeComponent(); } private void Form1_Load_1(object sender, EventArgs e) { string[] data = null; string[] WFM = null; //Open connection to instrument mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open(resourceString); // Initial operating Conditions init(); // Wait for new data PAready(); // read all data data = PAread(); // return just Harmonic data data = HarValues(data); // Convert Harmonic data into a string array which represents a waveform WFM = SumHarmonics(data); // Plot the string array PAWFMplot(WFM); //Close instrument connection mbSession.Dispose(); } private void init() { mbSession.Timeout = 3000; mbSession.SendEndEnabled = true; mbSession.Write(":HMX:VLT:SEQ 0"); // 0 for odd and even Harmonic mbSession.Write(":HMX:VLT:RNG 7"); // Return all harmonics from 1 to 7 mbSession.Write(":SEL:VHM"); // Add Voltage Harmonic Measurement } private void PAready() { string DSR = null; mbSession.Write(":DSE 2"); // This enables the NDV (New Data Available) bit while (DSR != "2\n") { DSR = mbSession.Query(":DSR?"); } } private string[] PAread() { string[] FRDs = null; string reply = null; mbSession.Write(":FRD?"); reply = mbSession.ReadString(); // Read Data FRDs = reply.Split(','); return FRDs; } private string[] HarValues(string[] values) { string[] FRFs = null; string reply = null; mbSession.Write(":FRF?"); reply = mbSession.ReadString(); FRFs = reply.Split(','); int[] index = new int[FRFs.Length]; int j = 1; for (int i = 0; i < FRFs.Length; i++) { // Brute force way to read Voltage Magnitude values and place them in another array if (FRFs[i] == "V" + j + "m") { index[j - 1] = i - 2; j++; } } string[] HarValues = new string[j]; for (int i = 0; i < j - 1; i++) { HarValues[i] = values[index[i]]; } return HarValues; } private string[] SumHarmonics(string[] HarValues) { int Points = 100; double omega = 2 * Math.PI / Points; double[] Ymag = new double[Points]; string[] WFM = new string[Points]; for (int i = 0; i < Points; i++) { for (int j = 0; j < HarValues.Length; j++) { double stuff = (j + 1) * (i + 1) * omega; Ymag[i] = Ymag[i] + Math.Sin(stuff) * Convert.ToDouble(HarValues[j]); WFM[i] = Convert.ToString(Ymag[i]); } } return WFM; } private void PAWFMplot(string[] WFM) { try { foreach (var Series in chart1.Series) { Series.Points.Clear(); } for (int i = 0; i < WFM.Length; i++) { chart1.Series["Series1"].Points.AddXY (i, WFM[i]); } } catch { } chart1.Series["Series1"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine; } } }