- Home /
Unity to Arduino
Is there a way to send data from Unity to Arduino and back?
Answer by adellelin · Aug 16, 2014 at 03:04 PM
COM4 is for windows. For Mac - change " COM4" to the full serial port name for mac. Mine is "/dev/tty.usbmodem1421"
Answer by ellens · Jun 01, 2012 at 11:48 PM
If you are on a Mac you can use the following example will oscP5 to send and recieve data between Arduino and Unity: http://www.sundh.com/blog/2012/05/unity-processing-arduino/
Hello, I cannot seem to find your arduino unity processing tutorial with the force sensor. I urgently need that tutorial
Answer by LastTemplar · Oct 29, 2011 at 07:39 AM
Here is an example I've been working with. This is an example C# code for Unity:
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Threading;
public class SerialPortTest : MonoBehaviour
{
//Setup parameters to connect to Arduino
public static SerialPort sp = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
public static string strIn;
// Use this for initialization
void Start ()
{
OpenConnection();
}
void Update()
{
//Read incoming data
strIn = sp.ReadLine();
print(strIn);
//You can also send data like this
//sp.Write("1");
}
//Function connecting to Arduino
public void OpenConnection()
{
if (sp != null)
{
if (sp.IsOpen)
{
sp.Close();
message = "Closing port, because it was already open!";
}
else
{
sp.Open(); // opens the connection
sp.ReadTimeout = 50; // sets the timeout value before reporting error
message = "Port Opened!";
}
}
else
{
if (sp.IsOpen)
{
print("Port is already open");
}
else
{
print("Port == null");
}
}
}
void OnApplicationQuit()
{
sp.Close();
}
}
This is an example code for the Arduino:
void setup()
{
Serial.begin(9600);
}
void loop() {
int val = 2;
//Sending value
Serial.write(val);
delay(1000);
//Receiving value
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
Hope this helps ;)
Hey this didn't really work out. I got an error trying to run the script: " Assets/NewBehaviourScript.cs(3,17): error CS0234: The type or namespace name Ports' does not exist in the namespace
System.IO'. Are you missing an assembly reference?"
I am using Unity 3 on $$anonymous$$ac OS 10.7.
You should use 'comment' rather than 'answer' unless you found an answer
In Unity go to File -> Build Settings -> Player Settings, you will see new options will open in the right side of the screen, there, for the API Comp. Level choose .NET 2.0 (at the moment I think its something like >NET 2.0 subset)
System.IO.Ports cannot be used on $$anonymous$$ac's currently therefore this won't work on a $$anonymous$$ac has something to do with Serial Ports and the $$anonymous$$ono version implemented in Unity
Change as said to NET 2.0 but what is the "message" supposed to be? "The name `message' does not exist in the current context"
Answer by HandHGraphics · Jul 18, 2013 at 06:57 AM
The Kinect2Arduino Package does just that; it can send serial messages to and from the Arduino and Unity. It's available in the asset store here: https://www.assetstore.unity3d.com/#/content/9680