Click or drag to resize

How to discover Alpaca devices asynchronously

This topic describes the asynchronous discovery methods that the library provides. Please see Alpaca devices and ASCOM devices for information on the Alpaca device / ASCOM device terminology.

Task Based Asynchronous Methods

The purpose of discovery is to identify devices that can be controlled through the Alpaca protocol and the library provides this information as generic lists of AlpacaDevices and AscomDevices

These two static async methods return .NET Tasks that can be awaited or manipulated with Task methods:

Each method initiates a discovery when called and immediately returns an awaitable Task, enabling the calling method to continue processing while the discovery process runs. When discovery completes, the Task result is updated with a generic list of AlpacaDevice or AscomDevice as appropriate.

Both asynchronous methods have parameters to customise discovery, however, all of these are optional and have default values, except for the DeviceTypes parameter in the AlpacaDiscovery.GetAscomDevicesAsync(DeviceTypes? deviceType) method, which is a required parameter.

These examples show how to use the static asynchronous methods:

Using the asynchronous methods
 1// Get a list of Alpaca devices using default values
 2List<AlpacaDevice> alpacaDevices1 = await AlpacaDiscovery.GetAlpacaDevicesAsync();
 3Console.WriteLine($"Discovered {alpacaDevices1.Count} Alpaca devices.");
 4
 5// Get a list of ASCOM devices of a specified device type using default values
 6List<AscomDevice> ascomTelescopeDevicesAsync1 = await AlpacaDiscovery.GetAscomDevicesAsync(DeviceTypes.Telescope);
 7Console.WriteLine($"Discovered {ascomTelescopeDevicesAsync1.Count} ASCOM Telescope devices.");
 8
 9// Get a list of ASCOM devices of all types using default values
10List<AscomDevice> allAscomDevicesAsync = await AlpacaDiscovery.GetAscomDevicesAsync(null);
11Console.WriteLine($"Discovered a total of {allAscomDevicesAsync.Count} ASCOM devices.");
12
13// Get a list of ASCOM Telescope devices by issuing 2 discovery packets with an interval of 200ms, on port 32227, with a discovery duration of 2.5 seconds,
14// without DNS name resolution and using IPv4 and IPv6 over HTTPS with no operational message logger.
15List<AscomDevice> devices = await AlpacaDiscovery.GetAscomDevicesAsync(DeviceTypes.Telescope, 2, 200, 32227, 2.5, false, true, true, ServiceType.Https, null);
16Console.WriteLine($"Discovered {devices.Count} ASCOM Telescope devices.");

Named parameters are helpful when only a few parameters require non-default values:

Using named parameters
1// Get a list of Alpaca devices using default values, apart from enabling discovery of IPv6 devices
2List<AlpacaDevice> alpacaDevices2 = await AlpacaDiscovery.GetAlpacaDevicesAsync(useIpV6: true);
3Console.WriteLine($"Discovered {alpacaDevices2.Count} Alpaca devices.");

If required, you can also work directly with the tasks returned by these methods:

Working with discovery tasks
 1// Get a list of Alpaca devices asynchronously
 2Task<List<AlpacaDevice>> alpacadevicesTask = AlpacaDiscovery.GetAlpacaDevicesAsync();
 3
 4// Control returns quickly from AlpacaDiscovery.GetAlpacaDevicesAsync() and there is the opportunity to do other work
 5DoWork();
 6
 7// Eventually, wait for the discovery task to complete
 8Task.WaitAll(alpacadevicesTask);
 9
10// Retrieve the list of discovered Alpaca devices
11List<AlpacaDevice> alpacaDevices = alpacadevicesTask.Result;
12Console.WriteLine("AsynchronousTask", $"Discovered {alpacaDevices.Count} Alpaca devices.");
See Also