예제 #1
0
        /// <summary>
        /// Get SCPI identity.
        /// </summary>
        /// <returns>SCPIIdentity object</returns>
        public async Task <SCPIIdentity> GetIdnAsync()
        {
            string response = "";

            // workaround: first request does not work sometimes, so retry once in this case
            for (int retry = 0; retry < 2; retry++)
            {
                try
                {
                    response = await scpiProtocol.RequestAsync("*IDN");

                    // dissect IDN string
                    SCPIIdentity identity = new SCPIIdentity(response);

                    // save identity object
                    Identity = identity;

                    break;
                }
                catch (Exception)
                {
                    if (retry == 1)
                    {
                        throw;
                    }
                }
            }

            return(Identity);
        }
예제 #2
0
        /// <summary>
        /// Find all connected Active Load devices.
        /// </summary>
        /// <returns>Array of COM ports</returns>
        public async Task <string[]> FindDevicesAsync()
        {
            string[]      portNames     = SerialPort.GetPortNames();
            List <string> listPortNames = new List <string>();

            foreach (string portName in portNames)
            {
                try
                {
                    Open(portName);

                    // Probe device using SCPI request *IDN?
                    SCPIIdentity identity = await GetIdnAsync();

                    if (identity.Model.Contains("Active Load"))
                    {
                        Debug.WriteLine("Found device at: " + portName);

                        listPortNames.Add(portName);
                    }

                    Close();
                }
                catch (Exception)
                {
                    // Continue with next device
                    Close();
                }
            }

            return(listPortNames.ToArray());
        }
예제 #3
0
        /// <summary>
        /// Connect to a specific device (if given) or to the first found Active Load device.
        /// </summary>
        /// <returns></returns>
        public async Task <SCPIIdentity> OpenAsync()
        {
            if (PortName == null)
            {
                // Try to find first device and open it
                string[] portNames = await FindDevicesAsync();

                if (portNames.Length == 0)
                {
                    // Nothing found
                    throw new DeviceNotFoundException("No device could be found.");
                }

                PortName = portNames[0];
            }

            Open(PortName);

            // send an empty line to flush buffers
            try
            {
                await scpiProtocol.SendAwaitResponseAsync("", 100);
            }
            catch (Exception)
            {
                // ignore errors in this case, they are expected
            }

            // get SCPI identity object
            SCPIIdentity identity = await GetIdnAsync();

            // check if device is calibrated
            await GetCalibrationAsync();

            return(identity);
        }