Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SergioAAV · Jan 15, 2014 at 06:58 PM · c#nullreferenceexceptiondeviceusbport

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;
        }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Referencing variables from another script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges