Connecting arduino to unity

I want to get a data from arduino serial monitor and use it in unity
But when I use the below code i get the timeexcpetion error
May I ask what is going wrong?

My unity code

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

public class Move : MonoBehaviour
{
    SerialPort sp = new SerialPort("COM9", 9600);

    void Start()
    {
        sp.Open();
        sp.ReadTimeout = 1000;
    }

    void Update()
    {
        Debug.Log("running");
        try
        {
            Debug.Log(sp.ReadLine());
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex);
        }
    }
}

My arduino code


void setup() {
   Serial.begin(9600);
 }
 
 void loop() {
   Serial.println("Hello");
   delay(1000);
 }

Can't you lengthen this?

I lengthen it from 1 to 1000 but I will try more thanks!

It's still didn't work...
Is it possible that my cable that connecting arduino to my laptop has low data sending speed so it doens't work?

Possible, yes, likely, no.

hmm... okay Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.