- Home /
,"sp.ReadLine();" make unity game simulation not work
Hi I am using Arduino to communicate con Unity, I have problems when Arduino tries to send more then one number, for example if I try to send a "10" then unity get 1 and 0 separately.
I tough the solution could be to ad the code line "sp.Readline();" cause I saw in some other codes, the problem is that when I add this line of code and I press the play button the game simulation doesn't start, but continue charging the scene endlessly. Can you help me with this, if not, how can I make Arduino comunicate properly with Unity? Let me know if I ave been clear. Thank you! here the code i wrote on Mac with Visual Studio. using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO.Ports;
public class ControlsM : MonoBehaviour
{
int managerValue = 0;
public GameObject buttonShoot;
public GameObject buttonMove;
public GameObject buttonMove24;
bool buttonMoveVerify2 = true;
bool buttonMoveVerify3 = true;
bool buttonMoveVerify24 = true;
public GameObject rotator;
//bool buttonShootVerify = false; it will be useful later
public float speed;
//public bool boottonExist = false; need to check if it has any relevance
private float amountToMove;
SerialPort sp = new SerialPort("/dev/cu.usbmodem14201", 9600);
void Start()
{
sp.Open();
sp.ReadLine();
sp.ReadTimeout = 1;
}
// Update is called once per frame
void Update()
{
//this allow to move according to the setted speed and the time the input continue
amountToMove = speed * Time.deltaTime;
if (sp.IsOpen)
{
try
{
managerValue = sp.ReadByte();
print(managerValue);
}
catch (System.Exception) { }
}
if (Time.time < 5) //this will check if the component is on and in case turn it on when it get an input
{
if (managerValue == 55)
{
buttonMoveVerify2 = false;
}
if (managerValue == 56)
{
buttonMoveVerify3 = false;
}
if (managerValue == 57)
{
buttonMoveVerify24 = false;
}
}
if (Time.time > 5)
{
if (buttonMoveVerify2 == true)
{
buttonMove.SetActive(true);
}
if (buttonMoveVerify3 == true)
{
buttonShoot.SetActive(true);
}
if (buttonMoveVerify24 == true)
{
buttonMove24.SetActive(true);
}
}
}
}
,Hi, I am using Arduino to communicate with unity, all the code seems to work till now, but then I noticed when I have a number bigger then 9 from Arduino the values are sent separately (e.g. 10 is sent 1 and 0), so I though that the command sp.Readline may be the answer from the moment other codes use it. The problem is when I add it, even only as a comment, it stop my game simulation to be played.
In the beginning i tough it was related to the warning about the "line ending inconsistency", but after i manage to solve it, when I press the play button I still get a long loading that stop only when arduino get disconnected from the usb. Can you please help me? here my code in Visual Studio, let me know if everything is clear, thank you! using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO.Ports;
public class ControlsM : MonoBehaviour
{
int managerValue = 0;
public GameObject buttonShoot;
public GameObject buttonMove;
public GameObject buttonMove24;
bool buttonMoveVerify2 = true;
bool buttonMoveVerify3 = true;
bool buttonMoveVerify24 = true;
public GameObject rotator;
//bool buttonShootVerify = false; it will be useful later
public float speed;
//public bool boottonExist = false; need to check if it has any relevance
private float amountToMove;
SerialPort sp = new SerialPort("/dev/cu.usbmodem14201", 9600);
void Start()
{
sp.Open();
sp.ReadLine(); //THIS SEEMS TO BE THE BAD GUY
sp.ReadTimeout = 1;
}
// Update is called once per frame
void Update()
{
//this allow to move according to the setted speed and the time the input continue
amountToMove = speed * Time.deltaTime;
if (sp.IsOpen)
{
try
{
managerValue = sp.ReadByte();
print(managerValue);
}
catch (System.Exception) { }
}
if (Time.time < 5) //this will check if the component is on and in case turn it on when it get an input
{
if (managerValue == 55)
{
buttonMoveVerify2 = false;
}
if (managerValue == 56)
{
buttonMoveVerify3 = false;
}
if (managerValue == 57)
{
buttonMoveVerify24 = false;
}
}
if (Time.time > 5)
{
if (buttonMoveVerify2 == true)
{
buttonMove.SetActive(true);
}
if (buttonMoveVerify3 == true)
{
buttonShoot.SetActive(true);
}
if (buttonMoveVerify24 == true)
{
buttonMove24.SetActive(true);
}
}
}
}
Sorry but we have too many questions of people who use "common stuff" and didn't bother to read the documentation carefully:
By default, the ReadLine method will block until a line is received. If this behavior is undesirable, set the ReadTimeout property to any non-zero value to force the ReadLine method to throw a TimeoutException if a line is not available on the port.
Likewise reading the ReadTimeout documentation:
The read time-out value was originally set at 500 milliseconds in the Win32 Communications API. This property allows you to set this value. The time-out can be set to any value greater than zero, or set to InfiniteTimeout, in which case no time-out occurs. InfiniteTimeout is the default.
So we know by default the read timeout is set to infinity. so the ReadLine method will block the current thread infinitely until a newline character is available. Since Unity's callbacks all run on the main thread this will freeze your whole application until a new line character is received.
Any sort of direct hardware communication should be done on a seperate thread. This is true for network, serial port or any other hardware communication. This is a quite advanced topic, so please take the time and read the documentation.
Thank you a lot for your time, yes I didn't study a lot the topic but I read about the timeout working and $$anonymous$$e is not set on infinite, but on 25, so i guess it should work. Anyway, I will check the documentation, thank you
In the code you presented you did not set the readtimeout before you call ReadLine. You do know that code in a method is executed in order? ^^