We have a Dut that’s also a ScpiInstrument, meaning we would like to show up under the DUT tab in the editor, yet also have all the attributes that a ScpiInstrument possesses.
The issue is that if we inherit from Dut and add a private (or public, doesn’t matter) member of type ScpiInstrument it does get treated as a Dut, but the Scpi Settings do not show up.
All right so the “solution” we have implemented right now is
public class MyDut : ScpiInstrument, IDut
and then, all the code for the Dut class is copied over into this class in a non-DRY way. We are sure there’s a better way to do this without copying over code, maybe using composition. Any tips or code snippets would be appreciated.
I’m glad you found a solution, but since ScpiInstrument itself inherits from IInstrument, MyDut is going to show up in both the Add DUT menu and the Add Instrument menu, even if it is only intended to be used as a DUT.
If you want a solution using composition instead of using inheritance, you can use the [EmbedProperties] attribute. This feature is also useful for including a collection of settings in a test step without inheriting from some base step.
Here is an example of embedding the functionality of ScpiInstrument in a DUT:
public class MyScpiDut : IDut
{
[EmbedProperties(PrefixPropertyName = false)]
public ScpiInstrument ScpiInstrument { get; set; } = new ScpiInstrument();
public event PropertyChangedEventHandler PropertyChanged;
public void Open()
{
ScpiInstrument.Open();
}
public void Close()
{
ScpiInstrument.Close();
}
public string Name { get => ScpiInstrument.Name; set => ScpiInstrument.Name = value; }
public bool IsConnected => ScpiInstrument.IsConnected;
}
In this implementation, the implementation of the IDut interface is delegated to the ScpiInstrument member. This is a bit more code than the inheritance solution, but this solution may be preferrable if you do not want your DUT implementation to also be considered an instrument.
That’s a great solution, I did know about the attributes via Attributes | OpenTAP but didn’t go into the details of EmbedProperties, this will do the trick, we can also inherit from Dut instead of IDut and thus achieve the objective of displaying this as a DUT but not as an Instrument
Thank you!