0

I have a USB spectrum analyzer that I would like to control programmatically. Luckily, the website comes with a sample Visual Studio solution that hooks into their driver. I was able to compile and run the sample code they provided, but the sample code seems to generate a custom GUI display that still requires the user to click buttons and such.

I want a CLI that I can run and everything will be done automatically. However, it doesn't seem like there is an easy way to do this. I peeked into the DLL metadata, and this is what is exposed to me:

namespace SA0314Ctrl
{
    public class SA0314Ctrl : Component
    {
        public List<PointF> swpData;

        public SA0314Ctrl();
        public SA0314Ctrl(IContainer container);

        public event EventHandler OnDeviceRemoved;
        public event EventHandler OnDeviceReady;
        public event EventHandler OnDataAvailable;
        public event EventHandler OnSweepComplete;

        public byte GetBw();
        public string GetDeviceSn();
        public void ParseMessages(ref Message m);
        public void RegisterHandle(IntPtr Handle);
        public void SetBw(byte val);
        public void SetIfGain(byte val);
        public void StartSweep(double startFreq, double stepFreq, ushort numPts);
        public void StopSweep();
        public void SweepSingle(double startFreq, double stepFreq, ushort numPts);
        public bool UnregisterHandle();
        protected override void Dispose(bool disposing);
    }
}

I tried using the parameterless constructor, but then the events never fire (if I assign handlers) and none of the actual APIs work (they all return NullPointerException). It seems like I have to use a Form so I can pass an IContainer to the constructor and a IntPtr to RegisterHandle. But I don't want a GUI that someone has to click! The whole point of this is to automate the collection of frequency domain measurements with StartSweep and StopSweep.

Is it possible to create a Form, but then hook into it programmatically instead of waiting for user input?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gillespie
  • 5,780
  • 3
  • 32
  • 54
  • 3
    You almost surely need Application.Run() to get those events fired. An example of another component that needs it [is here](https://stackoverflow.com/a/4271581/17034). – Hans Passant Jan 02 '20 at 16:51
  • That was indeed the issue. I also had to call `RegisterHandle(new IntPtr())` for some reason, but after doing that everything is working in CLI the same as it works in the GUI! – Gillespie Jan 02 '20 at 17:31

0 Answers0