Search Unity

Serial communication between Unity 5 and Arduino

Discussion in 'Scripting' started by rjth, Jun 5, 2015.

  1. rjth

    rjth

    Joined:
    Dec 6, 2014
    Posts:
    8
    I am trying to send some triggers through serial to an Arduino on OS X. Uniduino is not an option for me, because I need to use arbitrary libaries on Arduino.

    Is there a way to establish communication between the two?
     
  2. rjth

    rjth

    Joined:
    Dec 6, 2014
    Posts:
    8
    I got it to work.
    Thanks for the examples to LastTemplar and Ryanas.

    Change the API Compatibility Level from ".NET 2.0 Subset" to ".NET 2.0" in Player Settings.



    Example code compatible with OS X:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System.IO.Ports;
    5. using System.Threading;
    6. using System.Collections.Generic;
    7.  
    8. public class serialExample : MonoBehaviour
    9. {
    10.     public static SerialPort serialPort = new SerialPort("/dev/tty.usbmodem1411", 9600, Parity.None, 8, StopBits.One);
    11.     public static string strIn;
    12.  
    13.     void Start()
    14.     {
    15.         GetPortNames();
    16.         OpenConnection();
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         serialPort.Write("1");
    22.     }
    23.  
    24.     void GetPortNames()
    25.     {
    26.         int p = (int)System.Environment.OSVersion.Platform;
    27.         List<string> serial_ports = new List<string>();
    28.    
    29.         if(p == 4 || p == 128 || p == 6)
    30.         {
    31.             string[] ttys = Directory.GetFiles("/dev/", "tty.*");
    32.             foreach(string dev in ttys)
    33.             {
    34.                 if (dev.StartsWith("/dev/tty.*"))
    35.                     serial_ports.Add(dev);
    36.                 Debug.Log (System.String.Format(dev));
    37.             }
    38.         }
    39.     }
    40.  
    41.     public void OpenConnection()
    42.     {
    43.         if(serialPort != null)
    44.         {
    45.             if(serialPort.IsOpen)
    46.             {
    47.                 serialPort.Close();
    48.                 Debug.Log("Closing port, because it was already open!");
    49.             }
    50.             else
    51.             {
    52.                 serialPort.Open();
    53.                 serialPort.ReadTimeout = 50;
    54.                 Debug.Log("Port Opened!");
    55.             }
    56.          }
    57.          else
    58.          {
    59.             if(serialPort.IsOpen)
    60.             {
    61.                 print("Port is already open");
    62.             }
    63.             else
    64.             {
    65.                 print("Port == null");
    66.             }
    67.         }
    68.     }
    69.        
    70.     void OnApplicationQuit()
    71.     {
    72.         serialPort.Close();
    73.         Debug.Log("Port closed!");
    74.     }
    75. }
     
    Last edited: Jun 5, 2015