| Using VISA.NET > Raw I/O > Asynchronous I/O > Blocking Wait Sample |
This code sample illustrates an asynchronous read operation with a blocking wait.
After returning from the BeginRead method, the sample code waits one time for the asyncResult.AsyncWaitHandle event to be signaled. If the WaitOne method returns true (which means that the read succeeded or a timeout did not occur), 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.
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"); | |
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.
| 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."); } | |
Now the sample code uses a WaitHandle returned by the BeginRead method to wait once for a completion event to be signaled. WaitOne returns true if the completion event was signaled because the operation completed successfully, and false if it was signaled because a timeout occurred.
| Wait |
Copy Code |
|---|---|
var completed = false; completed = asyncResult.AsyncWaitHandle.WaitOne(1000); if (!completed) {
throw new VisaException("Asynchronous read with blocking wait timed out."); } | |
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.EndReadString(asyncResult); resultState = asyncResult.AsyncState; | |
Finally, the sample closes the session.
| Close the Session |
Copy Code |
|---|---|
| session.Dispose(); session = null; | |
Here is the complete blocking wait code sample:
| Polling Sample |
Copy Code |
|---|---|
using System; 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 completed = false; completed = asyncResult.AsyncWaitHandle.WaitOne(1000); if (!completed) { 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(); } } } | |