Basic
In the Connect section it is shown how to connect to a sensor resulting in a valid _sensor
instance - in this example to a Trimble SSeries instruments.
After the sensor is created and a valid connection established its best practice to:
- Create the interfaces used most often
- Register to events within those interfaces
- Create targets and such in case of optical instruments.
This allows to reuse the corresponding interfaces.
Note that its also possible to re-init an interface all the time - but typically this ends up in more code and hence more complexity to maintain.
Simple interfaces
In the following we assume the _sensor
instance is created and connected.
In the following the _sensor
is asked if its support the ISsiPower
interface.
If yes the interface is acquired via GetInterface<TSsiInterface>()
.
Afterwards several events are registered - OnPowerSourceChanged
- which fire on every change of the power source meaning a change from 98% to 97% for example.
Because its important to differentiate between internal and external power sources there is also a specific _battery
instancing containing details of the battery state.
protected void InitInterfacesAndSubscribeToSystemEvents()
{
//the removal of the event ensures we are only registered once.
_sensor.ConnectionStateChanged -= OnConnectionStateChanged;
_sensor.ConnectionStateChanged += OnConnectionStateChanged;
if (_sensor.IsSupported<ISsiPower>())
{
_power = _sensor.GetInterface<ISsiPower>();
_power.CurrentPowerSourceChanged -= OnPowerSourceChanged;
_power.CurrentPowerSourceChanged += OnPowerSourceChanged;
if (_power.CurrentPowerSource is IExternalPower)
SensorProperties.Battery = 100;
else if (_power.CurrentPowerSource is IInternalBattery)
{
_battery = _power.CurrentPowerSource as IBattery;
}
}
if (_sensor.IsSupported<ISsiSensorProperties>())
_sensorProperties = _sensor.GetInterface<ISsiSensorProperties>();
if (_sensor.IsSupported<ISsiCorrectionValues>())
_correctionValues = _sensor.GetInterface<ISsiCorrectionValues>();
if (_sensor.IsSupported<ISsiPpmCorrectionParameter>())
_ppmParameter = _sensor.GetInterface<ISsiPpmCorrectionParameter>();
}
The other interfaces are typical getter interfaces - they provide details on the instrument or the PPM correction parameter.
Note that its good to de-register to any event before registering. If the application in charge supports multiple instrument connections at a time this makes unexpected events a lot simpler to deal with.