Jump to content
  • 0

Ostin Flores

Question

Hi,

So far I had fun playing with the Python Scripts specifically with the Network Analyzer, but I want to be able to run the same with a C# application. I did not see examples in the samples under WverFormsSDK. Is there any resources on how to run the Network Analyzer in C#? This is in the Analog Discovery 2.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Hi @attila,

I found a class containing calls to the functions that can be used in the Analog Discovery 2 under  C:\Program Files (x86)\Digilent\WaveFormsSDK\samples\cs. But I did not see a full example application such as the ones in the Python library. I guess my questions is if there are any examples such as the ones in the python scripts.

Link to comment
Share on other sites

  • 0

Hi All,

I did a simple console application based on some projects that I found on GibHub "https://github.com/Andrei-Errapart/WaveFormsSDK" My goal is to make a GUI that displays a resonance frequency when sweeping over a range of frequencies. I have not tried to plot the returned amplitude values as I am planning to do it in the GUI. You are welcome to take my project and put it out there as an example (You can also make changes or comments as you please). The application sweeps a sinusoidal 1Vp (2Vpp) between 10kHz-1MHz, it works nicely. This is Done in C#, I translated the code from "AnalogNetwork_Analyzer.py" found in "C:\Program Files (x86)\Digilent\WaveFormsSDK\samples\py". Again, my code does not include the plot part.

AnalogNetwork_Analyzer.zip

Link to comment
Share on other sites

  • 0
On 9/18/2023 at 1:09 PM, attila said:

Hi @Ostin Flores

You can find a C# wrapper in WF SDK/ samples/ cs/

Edit: I recommend using the newer version since if fixes a NA bug of 3.20.1

 

 

Hi @attila,

Running the python scrip provided in the samples and C# console application made by me (AnalogNetwork_Analyzer) both give me errors when returning some values. Specifically those in:

                    ........

                    dwf.FDwfAnalogImpedanceStatusInput(hdwf, 0, out gain1, out zero); // relative to FDwfAnalogImpedanceAmplitudeSet Amplitude/C1
                    dwf.FDwfAnalogImpedanceStatusInput(hdwf, 1, out gain2, out phase2); // relative to Channel 1, C1/C#
                    rgGaC1[i] = 1.0 / gain1;
                    rgGaC2[i] = 1.0 / gain2;
                    rgPhC2[i] = -phase2 * 180 / Math.PI;      

gain1; returns as infinity,

gain2 returns as NAN

phase2 returns as 0

This is a new computer that I am trying with Windows 11. I know that those returned values are wrong as there is sinosoidal at the input of channel 1. If I do the sweep in the Waveforms application I get the results that I want. Do you know if there is something that I am not considering?

Edit: I am using 3.2.1 is this what you are reffering to?

Code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Security.Policy;
using System.Text;
//using WaveFormsSDK;


namespace AnalogNetwork_Analyzer
{
    class AnalogNetwork_Analyzer
    {
        public static readonly System.IO.TextWriter cout = System.Console.Out;

        public static void Wait(double sec)
        {
            System.Threading.Thread.Sleep((int)(sec * 1000));
        }

        static void Main(string[] args)
        {
            try
            {
                int steps = 500;
                double zero;
                double start = 10e3;
                double stop = 1e6;
                double reference = 1e4;
                double amplitude = 1;
                double hz;
                byte none = 0;
                byte sts;

                double[] rgHz = new double[steps];
                double[] rgGaC1 = new double[steps];
                double[] rgGaC2 = new double[steps];
                double[] rgPhC2 = new double[steps];

                double gain1, gain2, phase2;

                int phdwf = 0;
                string version, szerr;
                dwf.FDwfGetVersion(out version);
                cout.WriteLine("Version: " + version);
                // open automatically the first available device
                var hdwf = dwf.FDwfDeviceOpen(-1, out phdwf);
                dwf.FDwfGetLastErrorMsg(out szerr);
                cout.WriteLine("Device Status: " + phdwf);

                //this option will enable dynamic adjustment of analog out settings like: frequency, amplitude...
                dwf.FDwfDeviceAutoConfigureSet(hdwf, 3);

                dwf.FDwfAnalogImpedanceReset(hdwf);
                dwf.FDwfAnalogImpedanceModeSet(hdwf, 0); // 0 = W1-C1-DUT-C2-R-GND, 1 = W1-C1-R-C2-DUT-GND, 8 = AD IA adapter
                dwf.FDwfAnalogImpedanceReferenceSet(hdwf, reference); // reference resistor value in Ohms
                dwf.FDwfAnalogImpedanceFrequencySet(hdwf, start); // frequency in Hertz
                dwf.FDwfAnalogImpedanceAmplitudeSet(hdwf, amplitude); // 1V amplitude = 2V peak2peak signal
                dwf.FDwfAnalogImpedanceConfigure(hdwf, 1); // start
                // it will run until stopped or device closed
                Wait(2);

                foreach (int i in Enumerable.Range(0, steps))
                {
                    hz = stop * Math.Pow(10.0, 1.0 * (1.0 * i / (steps - 1) - 1) * Math.Log10(stop / start));
                    rgHz[i] = hz;
                    dwf.FDwfAnalogImpedanceFrequencySet(hdwf, hz); // frequency in Hertz
                    Wait(0.01);
                    dwf.FDwfAnalogImpedanceStatus(hdwf, out none); // ignore last capture since we changed the frequency

                    while (true)
                    {
                        if (dwf.FDwfAnalogImpedanceStatus(hdwf, out sts) == 0)
                        {
                            dwf.FDwfGetLastErrorMsg(out szerr);
                            cout.WriteLine("Error: " + szerr);
                            // Console app
                            System.Environment.Exit(1);
                            // WinForms app
                            //System.Windows.Forms.Application.Exit();
                        }
                        if (sts == 2)
                        {
                            break;
                        }
                    }
                    dwf.FDwfAnalogImpedanceStatusInput(hdwf, 0, out gain1, out zero); // relative to FDwfAnalogImpedanceAmplitudeSet Amplitude/C1
                    dwf.FDwfAnalogImpedanceStatusInput(hdwf, 1, out gain2, out phase2); // relative to Channel 1, C1/C#
                    rgGaC1[i] = 1.0 / gain1;
                    rgGaC2[i] = 1.0 / gain2;
                    rgPhC2[i] = -phase2 * 180 / Math.PI;
                }

                dwf.FDwfAnalogImpedanceConfigure(hdwf, 0); 
                dwf.FDwfDeviceClose(hdwf);   // stop
                Array.ForEach(rgPhC2, Console.WriteLine);

                Console.ReadKey();
            }
            catch (Exception ex)
            {
                cout.WriteLine("Error: " + ex.Message);
            }
        }
    }
}
 

Edited by Ostin Flores
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...