From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

.NET ImaqDx Producer Consumer Model

I have a GigE camera I'm trying to capture a series of images from.  The frames will be hardware triggered.  I can't miss frames, and I need to process the captured frames while it is still capturing.  I see lots of examples for LabView, but none for .net. Can anyone point me in the right direction?

 

I've made 2 unsuccessful attempts. I tried to start a low-level acquisition and subscribe to the ImageAcquired event.  Not sure exactly what happens. If I GetImageAt(0), I get an image where 4 images are smashed together into 1 image.  But when i watch the memory, the buffer never increases to the size necessary to hold all images.  Additionally, the ImageAcquired  is never fired. 

 

using (var session = new ImaqdxEthernetSession(_mainCamera.Name))
{
	int imagesToCapture = 25;

	session.Acquisition.ImageAcquired += AcquisitionOnImageAcquired;
	session.Acquisition.Configure(ImaqdxAcquisitionType.SingleShot, imagesToCapture);
	session.Acquisition.Start();

	//I manually trigger frame capture here. 25 frames for my test.
}

private void AcquisitionOnImageAcquired(object sender, ImaqdxImageAcquiredEventArgs e)
{
	Trace.WriteLine("This event is never fired!");
}

 

 

I've also tried to start an async sequence.  I understand the userstate variable can be anything, but I need to know how the variable is used in the function so I can provide the proper info/parameters. The docs don't say what the userstate object needs to contain.  When the first camera frame is triggered, a ImaqdxException is thrown. However since this comes from a async void method I have no way to catch the error.

using (var session = new ImaqdxEthernetSession(_mainCamera.Name))
{
	int imagesToCapture = 25;
	var imageArray = new VisionImage[imagesToCapture];

	object state = "What does the function need this to contain?";
	try
	{
		session.SequenceAsync(imageArray, imagesToCapture, state);
		session.SequenceCompleted += Session_SequenceCompleted;
		Trace.WriteLine("Starting sequence");
	}
	catch (ImaqdxException ex)
	{
//This is never thrown despite the output window showing that an error occurred Trace.WriteLine(ex); } }

 

A resular sequence works fine, but of course that's synchronous and I can't process the images until after the sequence is complete.

0 Kudos
Message 1 of 4
(3,949 Views)

I don't think we have example code for doing this - this would rely on multithreading in .NET, which isn't something we have resources on. You would essentially need one thread capturing frames, and another thread running simulataneously to process the frames, using some data structure shared by both threads in order to pass frames from the producer to consumer thread. Have you looked into any general .NET multithreading examples?

0 Kudos
Message 2 of 4
(3,901 Views)

Agreed, I didn't see any example code for it either.  You mention multithreading, and that's why I was attempting to use the SequenceAsync() in my second code snippet.  However, every time I call it, it throws an exception.  Since it's an async void function, there's no way to trap/view the exception it throws. It's essentially a black box.  I'm guessing here, but I'm assuming it fails because 3rd argument I pass to the function is in the wrong format.  The function doesn't define what the object needs to contain, so I don't know what it's looking for.

0 Kudos
Message 3 of 4
(3,897 Views)

One thing you could do is "manually" initiate the multithreading by managing threads yourself, and in a new thread you explicitly create, calling the synchronous function instead of the async function. This should also allow you to capture the exception. 

 

Beyond that, I'm not sure what I can suggest - we have relatively limited resources on this API as it has been discontinued. It's supported in Visual Studio 2005/2008. One thing I can suggest is that if your vision software installer is run when Visual Studio is installed, you should be able to select the option to install help documentation, which may have additional resources to reference.

0 Kudos
Message 4 of 4
(3,874 Views)