Use Python Instrument from c# step

Is there an example of using a python instrument in a c# test step?

Currently I am integrating urjtag into my testplan in order to do some simple boundry scan testing.

urjtag already has python bindings, so I created a JTAG instrument to expose the urjtag API through its existing python bindings.

I am able to use this instrument fine from within python, but was hoping to be able to also make use of the instrument from c# steps. Is this currently practical or will I just need to keep the JTAG steps in python as well?

It’s really not a problem either way. I don’t mind having the associated test steps in python but wondered if it would be possible to mix instruments and steps this way.

Thanks,
Wittrock

Hi @wittrock,

This is definitely possible! Maybe you will find the best example if you use the project template in the python plugin.

tap python new-project --project-name TestModule --directory .

It contains a C# project with an interface, and your Python code can implement that interface. C# code can use that interface directly, and call into python that way.

e.g

1. Define the C# interface


    public interface PythonInstrument : IInstrument
    {
        double DoMeasurement();
    }

2. Implement the interface in Python

clr.AddReference("TestModule.Api")
from TestModule.Api import PythonInstrument

@attribute(OpenTap.Display("Test Instrument", "This class implements a shared Python API.", "TestModule"))
class TestInstrument(Instrument, PythonInstrument):
    def __init__(self):
        "Set up the properties, methods and default values of the instrument."
        super(TestInstrument, self).__init__() # The base class initializer must be invoked.
        self.count = 0
        
    #implement the DoMeasurement method required by ITestApi1.
    @method(Double)
    def DoMeasurement(self):
        """Example of a measurement method."""
        return 1.0

3. Use the interface from C#

 [Display("Step using instrument API", Group: "Example")]
    public class StepUsingInstrumentApi : TestStep
    {
        // this instrument is implemented in Python
        public PythonInstrument Instrument { get; set; }

        public override void Run()
        {
            var measurement = Instrument.DoMeasurement();
            Results.Publish("Measurement", new List<string>{"X"}, measurement);
            Log.Info("Measured: {0}", measurement);
        }
    }

1 Like

Thanks @rolf_madsen ,

Yes, this works great!
I should have looked at the generated template instead of just the Python examples.
Treating the JTAG interface as an instrument works well.

Thanks,
Wittrock