Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

My unity won't read a string data from Arduino to Unity

Discussion in 'Scripting' started by feit0719, Jan 8, 2020.

  1. feit0719

    feit0719

    Joined:
    Dec 5, 2019
    Posts:
    6
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generics;
    4. using UnityEngine;
    5. using System.IO.Ports;
    6. using System.Threadings;
    7.  
    8. public class sample: MonoBehavior
    9. {
    10. SerialPort sp = new SerialPort("COM3", 9600)
    11. public GameObject Image;
    12.  
    13. void Start()
    14. {
    15. sp.close();
    16. sp.Open();
    17. sp.ReadTimeout = 1;
    18. }
    19.  
    20. void Update()
    21. {
    22. if(sp.IsOpen)
    23. {
    24. try
    25. {
    26. ArduinoString(sp.ReadLine());
    27. print(ArduinoString);
    28. }
    29. catch(System.Exception)
    30. {
    31. }
    32. }
    33.  
    34. void ArduinoString(string txt)
    35. {
    36. if(txt == "r")
    37. {
    38. Image.setActive(true);
    39. }

    Arduino Code

    Code (Arduino):
    1.  
    2. void setup()
    3. {
    4. Serial.begin(9600);
    5. Serial.write("r");
    6. Serial.flush();
    7. }
    8. void loop()
    9. {
    10.  
    11. }
    12.  
     
    Last edited: Jan 8, 2020
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
  3. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    You are calling sp.ReadLine(), which, according to the docs, reads until it reaches a newline character. Since your Arduino doesn't send a newline, sp.ReadLine() will throw a TimeoutException after your assigned read timeout of 1ms (which might not be enough time to read something substantial anyways)
     
    JeffDUnity3D likes this.
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
  5. feit0719

    feit0719

    Joined:
    Dec 5, 2019
    Posts:
    6
  6. feit0719

    feit0719

    Joined:
    Dec 5, 2019
    Posts:
    6
    And About the video you suggested when I try on Serial Monitor the "r" appeared but when it's comes to unity it doesn't appear on my console. print(ArduinoString);
     
  7. feit0719

    feit0719

    Joined:
    Dec 5, 2019
    Posts:
    6
    What should I do?
    Should I use Serial.println();?
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    What was the result when you tried our suggestions?
     
    eisenpony likes this.
  9. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Or, in your Arduino code, try

    Code (csharp):
    1. Serial.write("r\n")
    You may also want to increase your sp.ReadTimeout. 10 is still quite fast.

    Also, don't discount the suggestion that your Arduino is sending the message before Unity is listening. You could try moving the Serial.write into the loop.