Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Surfninja · Sep 20, 2015 at 12:12 PM · stringopenstreamserialportport

Sending Data from Arduino Uno to Unity

Hello guys, I have a question about one thing, sending a variable from my Arduino Uno to the Unity Game Engine.

As of now, I have been able to communicate the two devices successfully by sending byte info so a magnetic reed switch on my Arduino Uno to move my object in Unity. Now I am trying to send something more complex, a variable that might be positive or negative and include a decimal.

In Arduino, I am sending the data through the Serial Port and in unity i opened the Serial Port to receive it. In the Console Log in Unity, i am receiving data but the numbers are not matching up to the ones displayed in the Arduino Console Log.

It was suggested to me from the Arduino community that it must be something on the Unity end since they have signed off on everything Im doing from the Arduino. They suggested there might be some kind of conversion going on during the transfer through the SerialPort.

Hopefully I made myself clear in my explanation. Here is the Unity code:

     using UnityEngine;
     using System.Collections;
     using System.IO.Ports;
     using Uniduino;
     
     public class Movement : MonoBehaviour {
     
         public Arduino arduino;
         private GameObject cube;
     
         public float speed;
         private float amountToMove;
         private SerialPort sp = new SerialPort("COM9",57600);
         
         void Start () {
             sp.Open();
             sp.ReadLine();
             sp.ReadTimeout = 1;
         }
     
         void Update () {
         } 
 }


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

5 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Farneze · Sep 21, 2015 at 12:09 AM

Hello Surfninja,

I was doing some tests on my own and found this code working for me. On arduino side, I used this code:

int count = 1000;

 void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
 }
 
 void loop() {
   // put your main code here, to run repeatedly:
   count = count +1;
   if(count == 10000)
     count = 0;
   Serial.print("Teste: ");
   Serial.println(count);
   delay(1000);
 }

And on Unity side, I used this code:

 using UnityEngine;
 using System.Collections;
 using System.IO.Ports;
 
 public class Move : MonoBehaviour {
     SerialPort sp = new SerialPort("COM3", 9600);
 
     void Start () {
         sp.Open ();
         sp.ReadTimeout = 1;
     }
 
     void Update () 
     {
         try{
             print (sp.ReadLine());
         }
         catch(System.Exception){
         }
     }
 }

So, I can confirm that arduino sends count from 1000 through serial port to Unity, and the unity shows on its console the output correctly.

Best regards,

Comment
Add comment · Show 1 · Share
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
avatar image Surfninja · Sep 21, 2015 at 08:45 AM 0
Share

Farneze: Thank you very much for that, I was able to get my variables sent over exactly as they appear in my Arduino Serial Log.

$$anonymous$$y next hurdle is now that I have the proper information strea$$anonymous$$g into Unity, is there a way save that into variables in Unity that can be utilized.

For Example, if I had Arduino send out: float UnitA float UnitB

Is there a way to receive those in Unity, save them as: public float UnitA public float UnitB

And then from there use those updated Units in Unity to perform a task? I'm sure this is possible, just outside my program$$anonymous$$g knowledge base. Thank you for your help.

avatar image
0

Answer by Surfninja · Sep 21, 2015 at 09:43 AM

Farneze: Thank you very much for that, I was able to get my variables sent over exactly as they appear in my Arduino Serial Log.

My next hurdle is now that I have the proper information streaming into Unity, is there a way save that into variables in Unity that can be utilized.

For Example, if I had Arduino send out: float UnitA float UnitB

Is there a way to receive those in Unity, save them as: public float UnitA public float UnitB

And then from there use those updated Units in Unity to perform a task? I'm sure this is possible, just outside my programming knowledge base. Thank you for your help.

Comment
Add comment · Share
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
avatar image
0

Answer by WilmUGameDev · Jan 16, 2016 at 12:21 AM

I can't get anything but hex numbers out of the Arduino...no strings, int, bool, nada just random numbers......what the heck am I missing

Comment
Add comment · Show 1 · Share
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
avatar image WilmUGameDev · Jan 16, 2016 at 12:37 AM 0
Share

UPDATE: Apparently the code for the arduino I was using created HEX values, so when I was reading them into Unity I got the "random" numbers - I thought that it was the way I was parsing them into unity, but I used the code above and was able to recreate the same results......I'm reading IR codes from a remote control......at least now I can get my variables out...

avatar image
0

Answer by maxparadox · Apr 22, 2016 at 05:36 PM

hope this helps i know its an old post ... but i thought its best to Share the answer .The code is working properly ..

using UnityEngine; using System.Collections; using System.IO.Ports;

 public class RotScript : MonoBehaviour
     {
         private float calcInput = 0f;
         private float epsilon = 0.01f;
         private float factor = 2f;
         private float currentInput = 0f;
        /* private float timer = 0f;
         private float timeToUpdate = 1f;
     */
         SerialPort serial = new SerialPort("COM3", 9600);
  
     void Start () {
  
     }
     
     void Update ()
  
     {
         if (!serial.IsOpen)
             serial.Open ();
             currentInput = int.Parse (serial.ReadLine ());
  
         // use this block for linear increase or decrease
         if (calcInput < currentInput - epsilon)
         {
             calcInput += Time.deltaTime * factor;
         }
         else if (calcInput > currentInput + epsilon)
         {
             calcInput -= Time.deltaTime * factor;
         }
         else // calcInput is really close to currentInput so we are ready to update currentInput
         {
             // get input from arduino
             currentInput = float.Parse(serial.ReadLine ());
         }
         transform.localEulerAngles = new Vector3(0,calcInput,0);
         /*timer += Time.deltaTime;
         if (timer >= timeToUpdate)
         {
             timer = 0f; // reset timer
             //get input from arduino
         }*/
     }
 }
Comment
Add comment · Share
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
avatar image
0

Answer by Tony686 · Nov 16, 2018 at 03:41 PM

connect arduino to unity on android with this plugin:

https://www.assetstore.unity3d.com/#!/content/98960

Comment
Add comment · Share
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

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

32 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 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

How can i open txt file outside the unity editor. When i have already finshed the game. 1 Answer

Is it possible to write to a port in C++ and read the data in Unity? And if so...how? 0 Answers

Read Arduino Stream with Js or C# 0 Answers

HELP: Arduino Genuino 101 to Unity3D. Serial Communication 0 Answers

Memory problem on android/ios 1GB ram with string[]? 0 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