Code Examples and Snippets

Table of Contents

  1. Initialization
    1. Delphi/Pascal
    2. C/C++
    3. Visual Basic
    4. Visual C#
  2. Read Power
    1. Delphi/Pascal
    2. C/C++
    3. Visual Basic
    4. Visual C#
  3. Referencing Return Loss
    1. Delphi/Pascal
    2. C/C++
    3. Visual Basic
    4. Visual C#

Note: Replace the "OPxxx_" prefix used in the examples with the appropriate prefix as needed.



Initialization

In general, device initialization is performed in two steps. First, we open the USB communication with a specific device using the OP710_OpenUSBDevice() call. This will result in a USB Handle. We then call the OP710_OpenDriver() function with the USB Handle from the previous step to initialize that device.

Delphi/Pascal

[Insert Delphi/Pascal Initialization example here]

C/C++

[Insert C/C++ Initialization example here]

Visual Basic

[Insert Visual Basic Initialization example here]

Visual C#

Below is an example in Visual C# showing how to look for devices by their description/model and building a look-up table of serial numbers to device numbers.


void LoadInstruments()
{
	int devCount = 0;
	IntPtr pUSBDesc = new IntPtr();
	IntPtr pUSBSN = new IntPtr();
	string tempDesc;

	// create a dictionary of <string, int>
	// to link serial number strings with device numbers
	_instruments = new Dictionary<string, int>();

	OP710_GetUSBDeviceCount(ref devCount);

	// get all OP415 devices and the serial number and device number dictionary
	for (int i = 0; i < devCount; i++)
	{
		// get description from the DLL
		OP710_GetUSBDeviceDescription(i, ref pUSBDesc);

		// this will copy the data that the pUSBDesc pointer is pointing to
		// to the tempDesc string
		tempDesc = Marshal.PtrToStringAnsi(pUSBDesc);

		// filter the devices by device description
		if (tempDesc.Contains("OP710"))
		{
			OP710_GetUSBSerialNumber(i, ref pUSBSN);
			_instruments.Add(Marshal.PtrToStringAnsi(pUSBSN), i);
		}
	}

	// the serial number can then be used to look up which device number is associated with it

}

We then use the look-up table to specify which serial number to initialize with OP710_OpenDriver().


void InitializeSelectedInstrument(string SelectedSerialNumber)
{
	int handle = 0;
	int status = 0;

	// close previous opened device
	// use a global flag to keep track of this
	// when controlling multiple devices, it is recommended to use a list of flags to keep track
	// of which devices (by serial number) are initialized.
	if (_instrumentConnectionOpen)
	{
		OP710_CloseDriver();
		_instrumentConnectionOpen = false;
	}

	if (string.IsNullOrEmpty(SelectedSerialNumber))
	{
		return;
	}

	// look up the serial number in the dictionary
	// use the device number to open the appropriate device
	// then use the handle to initialize the driver
	if (_instruments.TryGetValue(SelectedSerialNumber, out int devNum))
	{
		OP710_OpenUSBDevice(devNum, out handle);
		status = OP710_OpenDriver(handle);

		_instrumentConnectionOpen = true;
	}
}

The device number can also be used to select a specific device that is already opened using the OP710_SelectModule() function.



Read Power

Delphi/Pascal

[Insert Delphi/Pascal Read Power example here]

C/C++

[Insert C/C++ Read Power example here]

Visual Basic

[Insert Visual Basic Read Power example here]

Visual C#

In the example below, we are using an OP940 with 1310 and 1550 wavelengths. This measures power 1 channel at a time. This method is recommended for non-OP710 devices. The settling time of the source needed for the Sleep() depends on the test setup. This is usually 100-200 ms.


void MeasurePower()
{
	double power1310 = 0.0;
	double power1550 = 0.0;
	
	OP930_SetSourceMode(1);		// set source wavelength A (1310)
	OP930_SetWavelength(1310);
	
	System.Threading.Thread.Sleep(200);		// wait while the source settles
	OP930_ReadPower(out power1310);
	
	OP930_SetSourceMode(2);		// set source wavelength B (1550)
	OP930_SetWavelength(1550);	
	
	System.Threading.Thread.Sleep(200);		// wait while the source settles
	OP930_ReadPower(out power1550);
}

It is recommended to perform Insertion Loss references in software. This is done by measuring Absolute Power as shown above then saving this power as the reference power for that channel and wavelength. Ideally, we create a table of references for each wavelength and each channel we plan on measuring IL for. To measure Insertion Loss, measure the Absolute Power again then subtract the reference power associated with that wavelength and channel.


void ReferenceIL()
{
	
	OP930_SetSourceMode(1);		// set source wavelength A (1310)
	OP930_SetWavelength(1310);
	
	System.Threading.Thread.Sleep(200);		// wait while the source settles
	OP930_ReadPower(out refPower1310);
	
	OP930_SetSourceMode(2);		// set source wavelength B (1550)
	OP930_SetWavelength(1550);	
	
	System.Threading.Thread.Sleep(200);		// wait while the source settles
	OP930_ReadPower(out refPower1550);
}

void MeasureIL()
{
	OP930_SetSourceMode(1);		// set source wavelength A (1310)
	OP930_SetWavelength(1310);
	
	System.Threading.Thread.Sleep(200);		// wait while the source settles
	OP930_ReadPower(out tempPower);
	
	il1310 = refPower1310 - tempPower;
	
	OP930_SetSourceMode(2);		// set source wavelength B (1550)
	OP930_SetWavelength(1550);	
	
	System.Threading.Thread.Sleep(200);		// wait while the source settles
	OP930_ReadPower(out tempPower);
	
	il1550 = refPower1550 - tempPower;
}

It is also recommended to perform a stability-checking loop to ensure that the power measurement is stable before capturing the value. We take repeated measurements until we have two consecutive measurements that differ by less than the specified threshold or we reach the specified number of measurements. To use this method, replace the usual OP930_ReadPower() call with the StabilityLoop() call.


void StabilityLoop(out double power, double threshold, int loopCount)
{
	double tempPower = 0.0;
	double lastPower = 0.0;
	int i = 0;
	
	OP930_ReadPower(ref tempPower);
	
	while (i < loopCount)
	{
		lastPower = tempPower;
		System.Threading.Thread.Sleep(50);
		OP930_ReadPower(ref tempPower);
		
		if (Math.Abs(tempPower - lastPower) < threshold)
		{
			// 2 consecutive measurements within the threshold value
			// exit the loop
			power = tempPower;
			return;
		}
		
		i++;
	}
}

For OP710 devices, capturing power measurements from all channels simultaneously is possible. This is the recommended method for these devices when measuring power from multiple channels.


void MeasureAllChannels()
{
	double[] power = new power[24];
	
	OP710_SetWavelength(1310);	// set the active wavelength

	// read all channels simultaneously
	// this will save the power measurement from all 24 channels in an internal buffer
	OP710_GetChannelBuffer();
	
	// get the power for each channel from the captured buffer above
	for (int i = 0; i < 24; i++)
	{
		OP710_ReadChannelBuffer(i, out power[i]);
	}
}



Referencing Return Loss

Delphi/Pascal

[Insert Delphi/Pascal RL Reference example here]

C/C++

[Insert C/C++ RL Reference example here]

Visual Basic

[Insert Visual Basic RL Reference example here]

Visual C#

[Insert Visual C# RL Reference example here]
Generated by PasDoc 0.15.0.