Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Re: MAX and Async Read/Write

Hi, I am trying to use the SimpleAsynchronousReadWrite examle for reading data asynchronously from a serial resource using "SerialSession" instead of "MessageBasedSession". I am able to read some data but after a while I get the following exception.

Managed Debugging Assistant 'CallbackOnCollectedDelegate' has detected a problem in 'C:\Users\Public\Documents\National Instruments\NI-VISA\Examples\.NET\SimpleAsynchronousReadWrite\bin\Debug\SimpleAsynchronousReadWrite.vshost.exe'.

Additional information: A callback was made on a garbage collected delegate of type 'NationalInstruments.Visa!NationalInstruments.Visa.Internal.VisaDllEventHandler::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.

Can you please help me. I am using NI VISA verion 15.5. 

 

0 Kudos
Message 1 of 10
(3,845 Views)

Hi Aneek231,

 

Whenever it throws the exception after gathering data, does it stop your acquisition? Has it ever worked before all the way through without getting the exception? Also, can we see a screenshot of the exception? 

 

Pro tip -- since this forum thread was made in 2014, you might get higher visibility if you make a new post since the older posts don't get as much visibility to those that actively look at the forums.

Aulia V.
0 Kudos
Message 2 of 10
(3,833 Views)

I’m using the VISA session BeginRead and BeginWrite methods with VB.NET and my callbacks are completing without any problems. My VISA session is opened using:

mRsrcName = "ASRL" & numComPort.Value & "::INSTR"

gSession = New NationalInstruments.VisaNS.SerialSession(mRsrcName)

 

But most of the time my code uses the synchronous methods (Write, ReadByteArray and ReadString).

 

BeginRead and BeginWrite may fail if called more often but I haven’t tested it. The reason I mention this is I stumbled across this post when working on a different project (it didn’t use NI-VISA). That project was crashing after many serial port read/write operations due to a memory leak. In my case, all I had to do was call WriteAsync from BaseStream. If NI-VISA calls the Windows serial port driver then it may also have problems.

0 Kudos
Message 3 of 10
(3,828 Views)

hi, I am reading 1024 bytes at a time with readtermination set to none and my buffersize = 32768. It works for some chunks (of 1024) but after 7-8 chunks, it throws exception.

I have attached my Mainform.cs file from the sample code which i modified slightly, Selectresource.cs is the same. I also attached the sreenshot of the exception. it just gives the message as i mentioned earlier.

Download All
0 Kudos
Message 4 of 10
(3,818 Views)

Hi, I am also opening the session in the same way. The only difference is I am only trying to call asynchronous methods. It works for some iterations but then throws the exception. 

I was previously calling synchronous methods and it was working fine. I wonder if it has to do with the MS Visual studio version? I am using Visual studio 2015.

0 Kudos
Message 5 of 10
(3,816 Views)

aneek231,

 

It's hard to tell if the Visual Studio version will have an impact or not. Is there a reason you are needing to go from synchronous to asynchronous?

 

Also, I'll agree with Aulia here, you may want to open a new forum about the issue you are having so that more people will be able to see it!

0 Kudos
Message 6 of 10
(3,801 Views)

The problem I experienced due to the memory leak problem when directly using System.IO.Ports.SerialPort in the .NET framework only occurred after MANY read/writes. The Widows Task Manager clearly showed my application was hogging more memory after each serial port transaction then it eventually failed. Calling the BaseStream object as described in that post solved that problem.

 

I can confirm the NI-VISA serial port session is very reliable when using the sync functions. The few async calls I make also work but I haven't stress tested it. Unless you see a memory leak in the Task Manager, the problem you're experiencing is unrelated so I also vote you start a new discussion. I will be checking because I want to avoid what you're experiencing!

0 Kudos
Message 7 of 10
(3,784 Views)

Yes, I split the thread and wish I could change the subject line.

 

First, I wish to point out that it is BAD practice to edit shipping examples.  RENAME them and save them elsewhere is fine!  But, now you've gone and lost source code for a good example of how to do something!  WORSE you replaced it with a poor example that works with buggy behavior!  

 

Spoiler
OK stop beating yourself up and don't do that again...
WHAT! you were not self-flagellating? Well, in that case beat yourself up for a while - then stop after you learned your lesson!

Asynchronous calls merely request asynchronous behavior.  They may return synchronously.  

 

Now, explain why you believe asynchronous calls will improve your code.  Sometimes they will but more often there are other problems like having no termination character or miss-sized buffers.

 


"Should be" isn't "Is" -Jay
0 Kudos
Message 8 of 10
(3,778 Views)

I already have started a new topic here "http://forums.ni.com/t5/Instrument-Control-GPIB-Serial/Problem-for-VISA-async-read-write-for-serial-..." but didn't get any response there. Can you please discuss that topic there as well.

0 Kudos
Message 9 of 10
(3,767 Views)

I already have saved my shipping example elsewhere. Can you please verify what's buggy in this example. 

If there is any example for async read/write in serial session for .NET application. I searched a lot but was not able to find any similar example.


@JÞB wrote:

Yes, I split the thread and wish I could change the subject line.

 

First, I wish to point out that it is BAD practice to edit shipping examples.  RENAME them and save them elsewhere is fine!  But, now you've gone and lost source code for a good example of how to do something!  WORSE you replaced it with a poor example that works with buggy behavior!  

 

Spoiler
OK stop beating yourself up and don't do that again...
WHAT! you were not self-flagellating? Well, in that case beat yourself up for a while - then stop after you learned your lesson!

Asynchronous calls merely request asynchronous behavior.  They may return synchronously.  

 

Now, explain why you believe asynchronous calls will improve your code.  Sometimes they will but more often there are other problems like having no termination character or miss-sized buffers.

 



Any help will be appreciated.

0 Kudos
Message 10 of 10
(3,764 Views)