Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

AsyncWrite never returns in C# .NET application

Hello, I am having an issue in which it appears that calls to RawIO.BeginWrite never return or perform a supplied callback. I have about a dozen visa instruments connected to one host pc, performing frequent measurements. The system can run continuously for days, but inevitably fails. When it does, it seems to hang in the RawIO.BeginWrite method. Below is my AsyncWrite code. Operations perform inside a semaphore ensuring no simultaneous operations are occurring, and use the .NET async framework with a task completion source set in the callback. Read is implemented similarly. 

 

 

public async Task AsyncWrite(string cmd)
{
    if (await _semaphore.WaitAsync(60000).ConfigureAwait(false))
    {

        try
        {
            VisaLogger.Debug("Async writing with command: {Command}", cmd);
            await PerformAsyncWrite(cmd).ConfigureAwait(false);
        }
        finally
        {
            _semaphore.Release();
        }
    }
    else
    {
        throw new TimeoutException("Timeout waiting for write semaphore");
    }
}
private async Task PerformAsyncWrite(string cmd)
 {

     var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
     VisaLogger.Verbose("Starting async write");

     string message = cmd.EndsWith((char)session.TerminationCharacter) ? cmd : cmd + (char)session.TerminationCharacter;
     IVisaAsyncResult handle = session.RawIO.BeginWrite(
         message,
         new VisaAsyncCallback(asyncResult => WriteCallback(asyncResult, tcs)),
         null
     );
     VisaLogger.Verbose("Begin write kicked off");
     await tcs.Task.ConfigureAwait(false);
 }

 private void WriteCallback(IVisaAsyncResult asyncResult, TaskCompletionSource<bool> tcs)
 {
     try
     {
         VisaLogger.Verbose("Ending Async Write");
         session.RawIO.EndWrite(asyncResult);
         VisaLogger.Verbose("Async write completed successfully");
         tcs.SetResult(true);
     }
     catch (Exception ex)
     {
         tcs.TrySetException(ex);
     }
 }

 

 

 A parsed log (logfile.txt) that captures the failure is attached. Regular queries occur every 5 seconds, with many successful operations included in the file. However at the end, the AsyncWrite never completes. Also attached is a trimmed text version of an NI IO Trace capture (NI Trace Trimmed). The failure occurs at/after number 18187. It indicates that the viWriteAsync was called successfully. However control never returns to my code to write "Begin Write Kicked Off" nor do any log messages in the callback occur. Insight into what could be causing this would be greatly appreciated.

0 Kudos
Message 1 of 2
(116 Views)

The earlier post did not have the attached files; they are included here

Download All
0 Kudos
Message 2 of 2
(96 Views)