Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Get data from arduino

Discussion in 'Scripting' started by unity_2558386, Jan 19, 2020.

  1. unity_2558386

    unity_2558386

    Joined:
    Jan 10, 2020
    Posts:
    1
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Timers;
    4. using System.IO;
    5. using System.IO.Ports;
    6. using System.Linq;
    7. using System.Threading;
    8. using System;
    9.  
    10. public class PlayerController : MonoBehaviour
    11. {
    12.     public float speed;
    13.     SerialPort sp;
    14.     string[] stringDelimiters = new string[] { ":", "R", }; //Items we want to ignore in strings.
    15.     public Transform target; //The item we want to affect with our accelerometer
    16.     private Rigidbody rb;
    17.     bool portFound;
    18.     void Start()
    19.     {
    20.         sp = new SerialPort("COM4");
    21.         //sp.PortName = SetPortName(sp.PortName);
    22.         sp.DtrEnable = false; //Prevent the Arduino from rebooting once we connect to it.
    23.                               //A 10 uF cap across RST and GND will prevent this. Remove cap when programming.
    24.         sp.ReadTimeout = 1; //Shortest possible read time out.
    25.         sp.WriteTimeout = 1; //Shortest possible write time out.
    26.         sp.Open();
    27.         rb = GetComponent<Rigidbody>();
    28.  
    29.         //try
    30.         //{
    31.             //string[] ports = SerialPort.GetPortNames();
    32.             //foreach (string port in ports)
    33.             //{
    34.                 //sp = new SerialPort(port, 9600);
    35.                 //if (ArduinoDetected())
    36.                 //{
    37.                     //Debug.Log("12");
    38.                     //portFound = true;
    39.                     //sp.WriteLine("1");
    40.                     //break;
    41.                 //}
    42.                 //else
    43.                 //{
    44.                     //portFound = false;
    45.                     //sp.WriteLine("0");
    46.                 //}
    47.             //}
    48.         //}
    49.         //catch
    50.         //{
    51.         //}
    52.  
    53.     }
    54.     void Update()
    55.     {
    56.         //if (portFound == true) {
    57.        string data = sp.ReadLine();
    58.        Console.Write(data);
    59.        string[] words = data.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
    60.        int x = Int32.Parse(words[0]);
    61.        Debug.Log(x);
    62.        int x1 = Int32.Parse(words[1]);
    63.         //float moveHorizontal = Input.GetAxis("Horizontal");
    64.         //float moveVertical = Input.GetAxis("Vertical");
    65.         //Vector3 movement = new Vector3(x, 0.0f, x1);
    66.         //rb.AddForce(movement * speed);
    67.         //}
    68.     }
    69.  
    70.     //private bool ArduinoDetected()
    71.     //{
    72.         //try
    73.         //{
    74.             //sp.Open();
    75.             //System.Threading.Thread.Sleep(1000);
    76.             // небольшая пауза, ведь SerialPort не терпит суеты
    77.  
    78.             //string returnMessage = sp.ReadLine();
    79.             //sp.Close();
    80.  
    81.             // необходимо чтобы void loop() в скетче содержал код Serial.println("Info from Arduino");
    82.             //if (returnMessage.Contains("123456"))
    83.             //{
    84.                 //return true;
    85.             //}
    86.             //else
    87.             //{
    88.                 //return false;
    89.             //}
    90.         //}
    91.         //catch
    92.         //{
    93.             //return false;
    94.         //}
    95.     //}
    96.  
    97. }

    Unity gives me this error:
    TimeoutException: The operation has timed out.
    System.IO.Ports.WinSerialStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) (at <d2957de1c3fd4781a43d89572183136c>:0)
    System.IO.Ports.SerialPort.read_byte () (at <d2957de1c3fd4781a43d89572183136c>:0)
    System.IO.Ports.SerialPort.ReadTo (System.String value) (at <d2957de1c3fd4781a43d89572183136c>:0)
    System.IO.Ports.SerialPort.ReadLine () (at <d2957de1c3fd4781a43d89572183136c>:0)
    (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort.ReadLine()
    PlayerController.Update () (at Assets/Scripts/PlayerController.cs:57)

    Why i can't assing data from serial port to string?
    Maybe somebody know how to autodetect Arduino?
    Thank you for help.