LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabView C# DLL Wrapper

Solved!
Go to solution

I need to receive data from an Eye tracking device and I've been having a horrible time with the SDK.  First I found out that LabView doesn't respond to events that have a null source, and then I found out that LabView doesn't show methods or properties that have (or are?) generic.  I guess I have two options...I can make a LabView DLL using shared variables to communicate the data to my program, or I can make a very small DLL using the .NET SDK.

 

I'm not a C# person and am having a hard time picking it up.  The code below works as an EXE.  Would someone here be able to change it around to be a DLL that sends the e.* args to an event that LabView picks up?

 

//-----------------------------------------------------------------------
// Copyright 2014 Tobii Technology AB. All rights reserved.
//-----------------------------------------------------------------------

namespace MinimalFixationDataStream
{
    using EyeXFramework;
    using System;
    using Tobii.EyeX.Framework;

    public static class Program
    {
        public static void Main(string[] args)
        {
            using (var eyeXHost = new EyeXHost())
            {
                using (var FixationDataStream = eyeXHost.CreateFixationDataStream(FixationDataMode.Slow))
                {
                    eyeXHost.Start();

                    FixationDataStream.Next += (s, e) =>
                    {
                        Console.Write("Fixation ");
                        switch(e.EventType)
                        {
                            case FixationDataEventType.Begin:
                                Console.Write("Begin");
                                break;
                            case FixationDataEventType.Data:
                                Console.Write("Data");
                                break;
                            case FixationDataEventType.End:
                                Console.Write("End");
                                break;
                        }
                        Console.WriteLine(": ({0:0.00}, {1:0.00}) @{2:0}", e.X, e.Y, e.Timestamp);
                    };
                    

                    // Let it run until a key is pressed.
                    Console.In.Read();
                }
            }
        }
    }
}

 I'm trying to do it, and it is making me feel stupid.  😞

0 Kudos
Message 1 of 5
(4,373 Views)

I don't understand everything I did, but this seems to almost work.  I am getting "FixationDataStream.Next" events into LabView exactly as desired with the correct data, but when I try to close LabView or all of the LabView windows, LabView crashes.  Sometimes it immediately asks me to close the program and send something to Microsoft, and sometimes it takes a few minutes.  

 

Does it sound like a LabView problem or am I not doing something right in the DLL?

 

namespace FixationWrapperDLL
{
    using EyeXFramework;
    using System;
    using Tobii.EyeX.Framework;

    public class GetFixationDataStream
    {
        public event ChangedEventMethodHandler NewFixationDataEvent;
        public delegate void ChangedEventMethodHandler(GetFixationDataStream m, FixationEventArgs e);
        public FixationEventArgs LastFixationData;

        public EyeXHost eyeXHost;

        public GetFixationDataStream()
        {
            eyeXHost = new EyeXHost();
            
            var FixationDataStream = eyeXHost.CreateFixationDataStream(FixationDataMode.Slow);   //Slow or Sensitive
                    eyeXHost.Start();

                    FixationDataStream.Next += (s, e) =>
                    {
                        LastFixationData = e;
                        if(NewFixationDataEvent != null) NewFixationDataEvent(this, LastFixationData);
                    };
       
        }
        public void DisposeFixationObjects()
        {
            eyeXHost.Dispose();
        }
    }
}

 

0 Kudos
Message 2 of 5
(4,339 Views)
Solution
Accepted by topic author eissug

I think I fixed the problem when I remembered to call my own Dispose method.  🙂

 

I'm pretty sure I didn't do everything right, but it works now.  I'm pretty annoyed that I've had to figure all this out when the vendor has a nice SDK with events that LabView ignores...  😞

0 Kudos
Message 3 of 5
(4,338 Views)

@eissug wrote:

I need to receive data from an Eye tracking device and I've been having a horrible time with the SDK.  First I found out that LabView doesn't respond to events that have a null source, and then I found out that LabView doesn't show methods or properties that have (or are?) generic.  I guess I have two options...I can make a LabView DLL using shared variables to communicate the data to my program, or I can make a very small DLL using the .NET SDK.

 

I'm not a C# person and am having a hard time picking it up.  The code below works as an EXE.  Would someone here be able to change it around to be a DLL that sends the e.* args to an event that LabView picks up?

 

//-----------------------------------------------------------------------
// Copyright 2014 Tobii Technology AB. All rights reserved.
//-----------------------------------------------------------------------

namespace MinimalFixationDataStream
{
    using EyeXFramework;
    using System;
    using Tobii.EyeX.Framework;

    public static class Program
    {
        public static void Main(string[] args)
        {
            using (var eyeXHost = new EyeXHost())
            {
                using (var FixationDataStream = eyeXHost.CreateFixationDataStream(FixationDataMode.Slow))
                {
                    eyeXHost.Start();

                    FixationDataStream.Next += (s, e) =>
                    {
                        Console.Write("Fixation ");
                        switch(e.EventType)
                        {
                            case FixationDataEventType.Begin:
                                Console.Write("Begin");
                                break;
                            case FixationDataEventType.Data:
                                Console.Write("Data");
                                break;
                            case FixationDataEventType.End:
                                Console.Write("End");
                                break;
                        }
                        Console.WriteLine(": ({0:0.00}, {1:0.00}) @{2:0}", e.X, e.Y, e.Timestamp);
                    };
                    

                    // Let it run until a key is pressed.
                    Console.In.Read();
                }
            }
        }
    }
}

 I'm trying to do it, and it is making me feel stupid.  😞


None of the C# code I see here contains any generic types at the public interface (private / internal types are not exposed). To my knowledge LabVIEW can call methods / properties that return generic types such as List<T> and operate on said types if you are using LabVIEW 2013 and onwards. I have never tried generic methods but they are typically much more rare.

 

From what I can see you should be able to do the following:

  1. Make sure you have a project for this application so make integrating the external .NET dlls easier. Ideally copy the relevant dlls into the project folder or sub-folder.
  2. Using a constructor node create a new EyeXHost object. You will need to close this refnum before your application shuts down.
  3. Using a invoke node call the CreateFixationDataStream method to obtain a FixationDataStream object. You will need to close this refnum before your application shuts down.
  4. Register for the Next event on the FixationDataStream object. Given the naming convention used in the source I expect that the arguments that callback has are the sender (type object) and some derived class of EventArgs. You can use the callback VI to do your processing if you desire, or post relevant data back to your main application VI. Don't forget that this callback is executed in a different thread and can't be debugged by LabVIEW (it is marshalled to a .NET thread and can't be marshalled to the LabVIEW UI thread for debugging). If you want to check it's execution you can use messages etc.or post the data to your main app VI.
  5. Your main app sits there waiting for exit and/or data from your callback.
  6. Once your application is ready to close don't forget to close the references to the .NET objects created (FixationDataStream and EyeXHost) as well as de-register the event callback. LabVIEW doesn't have a nice version of the "using" syntatic sugar that C# has.

If you are still having trouble feel free to post some code.

0 Kudos
Message 4 of 5
(4,334 Views)

Hi Eissug,

just came across your feed whilst looking for solutions for the same problem myself. Rather than inventing the wheel could you share your dll and labview solution? Thanks in advance and all the best!

0 Kudos
Message 5 of 5
(3,295 Views)