- Home /
Problem identifying USB
Hello everyone. I'm having a problem with my code. I'm using a YEI 3 Space Embedded for head tracking. I've found a code here on UnityAnswers for using this device and adapted for what I need. In the code I tell in which port the device is connected. I tried to use WMI to make the code automatically look for the port where the device is connected, but I'm getting the error:
NullReferenceException: Object reference not set to an instance of an object System.Management.MTAHelper.WorkerThread () System.Management.ManagementPath.SetWbemPath (IWbemPath wbemPath, System.String path) System.Management.ManagementPath.CreateWbemPath (System.String path) System.Management.ManagementPath..ctor (System.String path) System.Management.ManagementPath..cctor () Rethrow as TypeInitializationException: An exception was thrown by the type initializer for System.Management.ManagementPath System.Management.ManagementScope._Clone (System.Management.ManagementScope scope, System.Management.IdentifierChangedEventHandler handler) System.Management.ManagementScope._Clone (System.Management.ManagementScope scope) System.Management.ManagementObjectSearcher..ctor (System.Management.ManagementScope scope, System.Management.ObjectQuery query, System.Management.EnumerationOptions options) System.Management.ManagementObjectSearcher..ctor (System.String queryString) (wrapper remoting-invoke-with-check) System.Management.ManagementObjectSearcher:.ctor (string) YEIHeadTracking.SetPort () (at Assets/YEIHeadTracking.cs:149) YEIHeadTracking.Start () (at Assets/YEIHeadTracking.cs:79)
Also, Unity chashes when I execute the scene through play button. If I use the option Run with Unity Debugger through the MonoDevelop the error described above happens.
Does anyone know how I can fix this?
Here is the code:
using UnityEngine;
using System;
using System.Collections;
using System.IO.Ports;
using System.Management;
public class YEIHeadTracking : MonoBehaviour{
public static SerialPort sp = new SerialPort();
// Command packet for getting the filtered tared orientation as a quaternion
// {header byte, command byte, [data bytes], checksum byte}
// checksum = (command byte + data bytes) % 256
public static byte[] send_bytes = {0xf7, 0x00, 0x00};
public static byte[] button_bytes = {0xf7, 0xfa, 0xfa};
public static byte[] tare_bytes = {0xf7, 0x60, 0x60};
public int counter = 0;
// Use this for initialization
void Start(){
SetPort ();
sp.WriteTimeout = 100;
sp.ReadTimeout = 100;
sp.Open();
sp.Write(send_bytes,0,3);
}
// Helper function for taking the bytes read from the 3-Space Sensor and converting them into a float
float bytesToFloat(byte[] raw_bytes, int offset){
byte[] big_bytes = new byte[4];
big_bytes[0] = raw_bytes[offset+3];
big_bytes[1] = raw_bytes[offset+2];
big_bytes[2] = raw_bytes[offset+1];
big_bytes[3] = raw_bytes[offset+0];
return BitConverter.ToSingle(big_bytes,0);
}
//private SerialPort port;
private void SetPort()
{
string[] portNames = SerialPort.GetPortNames();
string spCaption = string.Empty;
string spName = string.Empty;
bool portFound = false;
for (int y = 0; y < portNames.Length; y++)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_SerialPort");
foreach (ManagementObject queryObj in searcher.Get())
{
spCaption = queryObj["Caption"].ToString();
if (spCaption.Contains("3 Space Embedded"))
{
spName = queryObj["DeviceID"].ToString();
sp = new SerialPort(spName, 115200, Parity.None, 8, StopBits.One);
portFound = true;
break;
}
}
if (portFound)
break;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Cant change material texture through C# script 1 Answer
How to put null & non-null value calculation in 1 if statement 2 Answers