Aryan Bhasin
  • Blog
  • IB Resources
  • Standardised Testing
    • ACT Resources
    • SAT Resources

THE THOUGHT CATALOGUE

An organised entropy of ideas

Accessing serial ports in Unity

5/31/2017

0 Comments

 
The .NET framework offers a way to access and use Serial Ports connected to your system. This can be done by importing System.IO.Ports​

    
After importing, you can access devices connected to Virtual Serial Ports and use them to retrieve or send data. However, using the current framework in Unity will throw an error of not recognising System.IO.Ports. This is because Unity operates on the .NET 2.0 subset framework on default 
In order to fix this, you need to change settings to .NET 2.0
Click on Edit in the menu toolbar. Head over to Project Settings, and select Player. On the inspector window, click on Other Settings. Scroll Down until you find the Api Compatibility Level under Configuration. Change this from subset to the .NET 2.0 framework.
Now, you need to define your serial port and give arguments in the Start() function, and access data in the Update() function. Declare your serial port outside the functions using the command 'SerialPort <name>;'  
This code sample defines a port named sampleport using the SerialPort() function. It defines the properties of PortName and BaudRate for the gyroscope. The Open() command opens the virtual serial port for use. You can also close the port with Close().

    
The final step involves using the Update() function to continuously read the data from the gyroscope. The code below reads the data into a String called read-data, and prints it to the console. You can split the output, or print them all line-by-line using the for-each loop.

    
 You can now access the gyroscope's data in Unity.
0 Comments
    Picture

    Aryan Bhasin

    Categories

    All
    Computer Science
    Economics
    Literature
    Opinions
    Reflective
    Reviews
    Television

    Two roads diverged in a wood, and I--
    I took the one less travelled by
    ​

    And that has made all the difference

    Archives

    November 2017
    October 2017
    July 2017
    June 2017
    May 2017
    March 2017
    February 2017
    November 2016
    September 2016

  • Blog
  • IB Resources
  • Standardised Testing
    • ACT Resources
    • SAT Resources