| Using VISA.NET > Raw I/O > Asynchronous I/O > Callback Sample |
This code sample illustrates an asynchronous read operation with a callback.
This sample uses the System.Threading namespace for the OS event that signals the main routine that it is OK to to close the session after the callback has been handled.
| using Statement |
Copy Code |
|---|---|
using System.Threading;
| |
The main routine in this sample calls the BeginRead method, then leaves the rest of the processing of the asynchronous read operation up to the callback. The only other thing the main routine must do is wait for a signal that the read operation has completed so that it can close the session to the resource.
In the sample, variables are kept with the section in which they are first used, except for two static, non-local variables that are shared between the main routine and the callback.
Two variables are shared between the main routine and the callback. One is the session, since BeginRead and EndReadString must be called on the same session. The other is an event that the callback can use to signal that it is done with EndReadString call, so that the main routine knows that it is OK to close the session.
| Shared Variables |
Copy Code |
|---|---|
| |
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 |
|---|---|
session = ( Ivi.Visa.2000); session.RawIO.Write( | |
Next the sample code calls BeginRead. The call includes the callback method as an argument, so that the BeginRead method knows what method to call after the read operation completes.
If BeginRead returns a null, then the attempt to begin the read did not succeed, and the sample throws an exception.
| Start the Read |
Copy Code |
|---|---|
IVisaAsyncResult asyncResult = null; var buffer = new byte[1000]; var expectedState = new object(); completeEvent = new ManualResetEvent(false); asyncResult = session.RawIO.BeginRead(buffer, AsyncIOCallback, expectedState); if (asyncResult == null) {
throw new NullReferenceException("BeginRead returned a null reference - could not start asynchronous read."); } | |
The main method waits until the callback has finished using the session, then closes the session.
| Close the Session |
Copy Code |
|---|---|
session.Dispose(); session = | |
The callback method is called when the read operation is complete. It checks for a timeout and cleans up after itself, both by calling EndRead. Finally, it signals the main routine that it has finished its work.
Note that the callback method's signature matches the signature of the callback delegate VisaAsyncCallback.
| Callback Method |
Copy Code |
|---|---|
static void AsyncIOCallback(IVisaAsyncResult asyncResult) {
var response = string.Empty; object resultState = null; try {
response = session2.RawIO.EndReadString(asyncResult); resultState = asyncResult.AsyncState; } catch (Exception ex) {
// EndReadString may incur an exception due to a // timeout. If/when this happens, timeout handling // starts here. In this case, we rethrow the exception. throw ex; } finally {
// Set the state of the event to signaled so that // anything waiting on the callback can proceed. completeEvent.Set(); } } | |
Here is the complete callback code sample:
| Callback Sample |
Copy Code |
|---|---|
using System; using System.Threading; using Ivi.Visa; using Keysight.Visa; namespace IdnSample {
class IdnSample {
static IGpibSession session = null; static ManualResetEvent completeEvent = null; static void Main(string[] args) {
AsyncIOCallbackExample(); Console.WriteLine("Press any key to end..."); Console.ReadKey(); } static void AsyncIOCallback(IVisaAsyncResult asyncResult) { var response = string.Empty; object resultState = null; try { response = session.RawIO.EndReadString(asyncResult); resultState = asyncResult.AsyncState; } catch (Exception ex) { Console.WriteLine("Completed: {0}", asyncResult.IsCompleted.ToString()); throw new ApplicationException ("Error reading results."); } finally { // Set the state of the event to signaled so that // anything waiting on the callback can proceed. Console.WriteLine("Completed: {0}", asyncResult.IsCompleted.ToString()); Console.WriteLine("Response: {0}", response.TrimEnd(new char[] { '\n' })); completeEvent.Set(); } } static void AsyncIOCallbackExample() { 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(); completeEvent = new ManualResetEvent(false); asyncResult = session.RawIO.BeginRead(buffer, AsyncIOCallback, expectedState); if (asyncResult == null) { throw new NullReferenceException("BeginRead returned a null reference."); } var completed = completeEvent.WaitOne(); session.Dispose(); session = null; } } } | |