Unity Serial Communication

Unity Serial Communication

Unity Serial Communication

 Hello everyone, in this post I will explain how you can do serial communication with Unity. If you have no idea or knowledge about serial communication, you can read my article here to learn about serial and parallel communication.

What can you do using serial communication protocols with Unity?

By using serial communication protocols in Unity, you can integrate hardware you have developed with development boards such as Arduino into your game, or you can make computers connected to each other via a serial port play games against each other over LAN (I'm just using LAN here as an example). You can use it to make simple multi-player games or to communicate with hardware.

Sending messages to Arduino using Unity Serial Communication

Arduino Codes

int pin8 = 8; // We define the digital pin

void setup() {
  // We start serial communication
  Serial.begin(9600);
  // We set the pin mode to output
  pinMode(pin8, OUTPUT); 
}

void loop() {
  // We check if the serial connection is active
  if(Serial.available()){
    String serialData = Serial.readStringUntil('\r'); // We read the incoming message  
    // If the message from serial communication is 8, we turn on pin 8
    if(serialData == "8"){
      digitalWrite(pin8, HIGH); 
    }
    // If the message from serial communication is 16, we turn off pin 8
    if(serialData == "16"){
    digitalWrite(pin8, LOW);
    }
  }
}

Unity Codes

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports; // The library we need for serial communication.
/* If you cannot add .Ports to your project, change the Api Compatibility Level to Net 4.x or higher in Project Settings.
 * If .Ports still does not get added although you select Net 4.x;
 * Click the Assets > Reimport All button and reimport your project.
 * After this process, it will be added.
 */

public class Unity_Seri_Haberlesme : MonoBehaviour
{
    /* Here, we define the SerialPort variable and create our connection
     * SerialPort("connection port", baudrate)
     * The connection port is the COM ports and is the value of the USB connection attached to your device.
     * Baudrate is the frequency of the communication, you should make sure it is the same as the value set in Arduino
     * If the baud value is not the same, our sent data may transfer incorrectly.
     */

    public SerialPort serialPort = new SerialPort("COM5", 9600); // We use SerialPort and select the port connected to COM5. 9600 is the baud value.

    void Start()
    {
        serialPort.Open(); // We start the serial connection.

        serialPort.Write("8"); // We send the message 8 via the serial connection.

        /* If we want to close the serial connection, we use serialPort.Close();
         * serialPort.Close();
         */

        serialPort.Write("16"); // We send the message 16 via the serial connection.

    }

    void Update()
    {
        Debug.Log(serialPort.ReadLine().ToString()); // We read the message from the device and print it as debug  
    }
}

Solution for System.IO.Ports Not Visible

Note: If you cannot add the "System.IO.Ports" library to your project, you need to choose the "Net 4.x" version from the "Other" section in "Project Settings".

Unity Serial Communication

Note: With the codes above, you can connect and communicate with any device you want to establish serial communication with, not only Arduino. It will be enough to just change the COM ports.

Note: Unfortunately, this method does not work on Android and IOS or, more generally, on mobile devices, it only works on PC-based devices. (Windows, Linux, Mac OS and other x86 & x86_64 devices.)

With the comment lines next to the codes above, I tried to explain everything, but if you have any questions left in your mind, you can ask me in the comments section below.