1

I'm trying to get my arduino working with Unity but I keep getting errors about my port not being open.

Here's my Unity code:

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

public class movingTest : MonoBehaviour
{
SerialPort data_stream = new SerialPort("/dev/cu.usbmodem142401", 19200);
public string receivedstring;
public GameObject test_data;
public Rigidbody rb;
public float sensitivity = 0.01f;

public string[] datas;

// Start is called before the first frame update
void Start()
{
    data_stream.Open();
}

// Update is called once per frame
void Update()
{
    receivedstring = data_stream.ReadLine();

    string[] datas = receivedstring.Split(',');
    rb.AddForce(0, 0, float.Parse(datas[0])*sensitivity * Time.deltaTime, ForceMode.VelocityChange);
    rb.AddForce(float.Parse(datas[1]) * sensitivity * Time.deltaTime, 0, 0, ForceMode.VelocityChange);

    transform.Rotate(0, float.Parse(datas[2]), 0);
}
}

It shows a red squiggly line under "using System.IO.Ports" (The type of namespace name 'Ports' does not exist in the namespace 'System.IO' (are you missing an assembly reference?))

There's also another red squiggly line under "SerialPort" (The type of namespace name 'SerialPort' could not be found (are you missing a using directive or an assembly reference?))

My Api Compatibility is .NET Standard 2.0

When the game runs on Unity, the only error I get is "Specified port is not open".

Here's how my ports look like. Every youtube tutorial I watched had ports like "COM4" or something, while I get this weird /dev/cu.usbmodem... monstrosity. I think this is my main problem.

Can anyone tell me what I'm doing wrong?

Arduino ports

3
  • Are you sure thats the device name? It sounds awfully machine specific so likely wouldn’t work on other machines Commented Oct 30, 2021 at 13:55
  • i literally dont know. I am using a Chinese arduino knockoff, so maybe that is the device's name? Commented Oct 30, 2021 at 14:02
  • Probably caused because it is not finding the System.IO.Ports assembly, check this link from the unity forums System.IO.Ports missing for Unity with .NET 4.x Commented Oct 30, 2021 at 17:30

1 Answer 1

1

Well the error is most probably due to System.IO.Ports is not part of .NET Standard but only in .NET Framework.

enter image description here

You want to chose

API Compatibility Level -> 4.x

enter image description here

I wonder though how you run your game if you have compiler errors.

Sign up to request clarification or add additional context in comments.

3 Comments

I changed my Api compatibility level to .NET 4.x now and I still get the red squiggly lines under the System.IO.Ports and SerialPort. However, when I press play, my Unity freezes and the only thing I can do is force quit it. Do I have to do something in Visual Studio, so VS knows i use .NET 4.x now?
OK, after deleting the code in the Update() function (except the receivedstring), the code runs but stills says that the specified port is not open
receivedstring = data_stream.ReadLine(); is a blocking call which freezes the code until actually something was received

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.