Keysight VISA.NET Help
Polling Sample

Polling Sample

This code sample illustrates an asynchronous read operation with polling.

Polling Sample Details

After returning from the BeginRead method, the sample code checks the IsCompleted property of the IVisaAsyncResult refererence returned by BeginRead every 10 milliseconds. When the property is true (which means that the read succeeded or a timeout occurred), the data that was read is available, and the sample calls EndReadString to complete the operation. Finally, the sample closes the session.

In the sample, variables are kept with the section in which they are first used.

This sample uses the System.Threading namespace for the call to Sleep.

using Statement
Copy Code
using System.Threading;

Initialize

To set the stage, the sample opens a session to a GPIB instrument with the alias MyInstr. Then it sends a *IDN? command to the instrument. The instrument will respond with its identification string. Refer to the IO Libraries Suite Help for details on using aliases.

Initialize
Copy Code
IGpibSession session = null;
session = (IGpibSession)GlobalResourceManager.Open("MyInstr", Ivi.Visa.AccessModes.None, 2000);
session.RawIO.Write("*IDN?\n");

Start the Asynchronous Read

Next the sample code calls BeginRead. If it returns a null, then the attempt to begin the read did not succeed, and the sample throws an exception. Without a valid IVisaAsyncResult reference, nothing more can be done.

Note that in this case there is a suitable .NET Framework exception available.

Start the Read
Copy Code
IVisaAsyncResult asyncResult = null;
var buffer = new byte[1000];
var expectedState = new object();

asyncResult = session.RawIO.BeginRead(buffer, expectedState);

if (asyncResult == null)
{
throw new NullReferenceException("BeginRead returned a null reference - could not start asynchronous read.");
}

Poll

Now the sample code starts to check every 10 milliseconds to see if the read has completed. A read operation may complete because it successfully reads what was expected, or because it did not successfully read what was expected within the timeout period.

In this case, what is expected is a standard identification string followed by a termination character with an END attached – probably considerably less than the 1000 characters allocated for the response's byte array.

Note that in this case the code uses VisaException rather than a .NET Framework exception.

Poll
Copy Code
var startTime = DateTime.Now;
var completed = false;
var timedOut = false;

while (!completed)
{
Thread.Sleep(10);
completed = asyncResult.IsCompleted;
TimeSpan elapsedSinceStart = DateTime.Now.Subtract(startTime);
timedOut = !completed && (elapsedSinceStart.TotalMilliseconds >= 1000);
}

if (timedOut)
{
throw new VisaException("Asynchronous read with poll timed out.");
}

End the Asynchronous Read

Next the sample code calls EndRead. Remember that this is necessary for the asynchronous operation to clean up after itself.

End the Read
Copy Code
var response = string.Empty;
object resultState = null;

response = session.RawIO.EndRead(asyncResult);
resultState = asyncResult.AsyncState;

Close the Session

Finally, the sample closes the session.

Close the Session
Copy Code
session.Dispose();
session = null;

Complete Sample

Here is the complete polling code sample:

Polling Sample
Copy Code
using System;
using System.Threading;
using Ivi.Visa;
using Keysight.Visa;
 
namespace IdnSample
{
  class IdnSample
  {
    static void Main(string[] args)
    {
      IGpibSession session = null;
      session = (IGpibSession)GlobalResourceManager.Open("MyInstr",
                                                     Ivi.Visa.AccessModes.None,
                                                         2000);
 
      session.RawIO.Write("*IDN?\n");
 
      IVisaAsyncResult asyncResult = null;
      var buffer = new byte[1000];
      var expectedState = new object();
 
      asyncResult = session.RawIO.BeginRead(buffer, expectedState);
 
      if (asyncResult == null)
      {
        throw new NullReferenceException("BeginRead returned a null reference.");
      }
 
      var startTime = DateTime.Now;
      var completed = false;
      var timedOut = false;
 
      while (!completed)
      {
        Thread.Sleep(10);
        completed = asyncResult.IsCompleted;
        TimeSpan elapsedSinceStart = DateTime.Now.Subtract(startTime);
        timedOut = !completed &&
        (elapsedSinceStart.TotalMilliseconds >= 1000);
      }
 
      if (timedOut)
      {
        Console.WriteLine("Completed: {0}", asyncResult.IsCompleted.ToString());
        throw new ApplicationException ("Asynchronous read with poll timed out.");
      }
 
      var response = string.Empty;
      object resultState = null;
 
      response = session.RawIO.EndReadString(asyncResult);
      resultState = asyncResult.AsyncState;
 
      Console.WriteLine("Completed: {0}", asyncResult.IsCompleted.ToString());
      Console.WriteLine("Response:  {0}", response.TrimEnd(new char[] { '\n' }));
 
      session.Dispose();
      session = null;
      Console.WriteLine("Press any key to end...");
      Console.ReadKey();
    }
  }
}

 

 


© Keysight Technologies 2015-2025