LabJack U3 and Unity | LabJack
 

LabJack U3 and Unity

7 posts / 0 new
Last post
apboone
apboone's picture
LabJack U3 and Unity

Hello,

I am attempting to integrate the LabJack U3-LV with Unity to send numerical event codes to an EEG acquisition device on a Win10 machine. Typically we use Matlab to handle this process via the labjack.m file. Now we are moving over to Unity to display stimuli. To start, Im looking for a way to test that the labjack is working through Unity. Does something similar to the labjack.m file exist for Unity?

Thanks!

LabJack Support
labjack support's picture
It looks like Unity supports

It looks like Unity supports C# scripting. We don't provide Unity specific examples, but we do provide Windows C# .NET examples:

https://labjack.com/support/software/examples/ud/dotnet

Keep in in mind that we have not tried Unity ourselves with our libraries.  Our C# examples provide Visual Studios projects, and project source code is in the .cs files which will be useful to you. In Unity using C#, you will need to use our UD .NET dll, LJUDDotNet.dll, which is installed to your "C:\Program Files (x86)\LabJack\Drivers" folder with our installer. Here is Unity documentation on using a managed dll/plugin (which LJUDDotNet.dll is) in your project:

https://docs.unity3d.com/Manual/UsingDLL.html

am_ga
am_ga's picture
Hi, 

Hi, 

I'd like to revive this old thread for some closure, as I'm also trying to integrate a LabJack U3-LV with Unity, but currently having trouble getting the UD library working with Unity.

I have the LJUDDotNet.dll and LabJackUD.dll in my Unity project's Assets/Plugins/ directory. From a script, I'm successfully able to use the namespace `using LabJack.LabJackUD;` however, when attempting to do anything with it I get this error:

DllNotFoundException: Assets/Plugins/LabJackUD.dll

Here is the script in full:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using LabJack.LabJackUD;

public class LJUDHandler : MonoBehaviour
{
    private U3 u3;

    private void Start()
    {
        /// Throws error:
        Debug.Log("UD Driver version = " + LJUD.GetDriverVersion().ToString("F4")); 

        try
        {
            Debug.Log("Instantiating u3...");

            /// Throws error:
            u3 = new U3(LJUD.CONNECTION.USB, "0", true); 
        }
        catch (LabJackUDException e)
        {
            Debug.LogError(e);
        }
    }
}

I've tried uninstalling and reinstalling all Labjack software from the installers page, trying both beta and release versions. I suspect the DLLs have dependencies that I must also include in the Assets/Plugins/ folder. How can I find out exactly what these dependencies are? Alternatively, is there a different approach that I should take to integrating with Unity? Any help is much appreciated.

LabJack Support
labjack support's picture
If you have the LabJack

If you have the LabJack software installed on your system, and are using C#/.NET in Unity, you do not need to add LabJackUD.dll to your project as LJUDDotNet.dll will load it on its own from the appropriate folder in \Windows. Remove LabJackUD.dll from your project and Assets/Plugins/ folder, and only have the LJUDDotNet.dll in your project and Assets/Plugins/ folder. See if that helps.

If you do have the LabJackUD.dll and LJUDDotNet.dll in the same folder (Assets/Plugins/), LJUDDotNet.dll will try to load the LabJackUD.dll from the same folder instead of the system installed one. In that case, that LabJackUD.dll needs to be the correct version for your application's bitness. By that I mean if your application is 32-bits, the 32-bit LabJackUD.dll is needed. For a 64-bit application the 64-bit LabJackUD.dll is needed. Also, the LabJackUD.dll requires the LabJackWUSB.dll. 32-bit versions of LabJackUD.dll and LabJackWUSB.dll are installed to \Windows\SysWOW64, and 64-bit versions are in \Windows\System32.

am_ga
am_ga's picture
Thank you, that makes sense.

Thank you, that makes sense. Simply removing LabJackUD.dll from Assets/Plugins/ resolved the error.

Javier Martinez Avila
jmavila's picture
Hello @am_ga,

Hello @am_ga,

I'm trying to connect a Labjack U3 to Unity as well. In Runtime, Unity recognizes the device, throws the driver version but when it starts streaming an ask to retreive data the first time, Unity crashes. I took the code from the Simplestream and adapted it to Unity. Did it work for you?

Thanks in advance,

LabJack Support
labjack support's picture
Replace your eGet call that

Replace your eGet call that uses GET_STREAM_DATA with eGetPtr instead. So in the example, replace:

LJUD.eGet(u3.ljhandle, LJUD.IO.GET_STREAM_DATA, LJUD.CHANNEL.ALL_CHANNELS, ref numScansRequested, adblData);

With:

LJUD.eGetPtr(u3.ljhandle, LJUD.IO.GET_STREAM_DATA, LJUD.CHANNEL.ALL_CHANNELS, ref numScansRequested, adblData);

eGet is not 64-bit pointer safe when passing an array to it (adblData), which can crash a 64-bit application. We later added the eGetPtr function which is 32 and 64-bit safe. The UD .NET examples are a bit old and still use eGet.