- Home /
ReadLine() Function Crash
Hey guys, good day!
I am having a kind of trouble with the function ReadLine(), because when I am reading a string which comes from the serial port (Por4, 115200), sometimes it read less information and sometimes it read more information than the information that I am sending to the port.
I am working with Arduino to send the information to the serial port, I am sending this string to the port: b' 26531 26525 26533 25377 \r\n'
I know that unity should read just this line because I can see in the Arduino IDE that this line always comes in the correct form (Without loss of information) for this reason I think that should be a problem with the .ReadLine() function.
If any has an advice for me, please don hesitate!
Thanks!
This is my C# code: using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO.Ports;
public class Rotation : MonoBehaviour { SerialPort myData = new SerialPort("COM4", 19200); //
public float speed = 5f;
public float voltage = 0;
public float value_serial = 0;
float x_position = 180f;
float y_position = 7.4f;
float z_position = 122f;
public string value;
public string[] value1;
char[] charSeparators = new char[] { ' ' };
// Start is called before the first frame update
void Start()
{
myData.Open(); //****************************************************
}
// Update is called once per frame
void Update()
{
value = "";
value = myData.ReadLine(); //****************************************************
value1 = value.Split(charSeparators, System.StringSplitOptions.None);
voltage = float.Parse(value1[4])*0.0001875f;
Debug.Log($"Value = {value}");
Debug.Log($"Value = {voltage}");
transform.eulerAngles = new Vector3(x_position, y_position, z_position);
if (voltage <= 4.0f)
{
transform.eulerAngles = new Vector3(x_position, 0, z_position);
}
else
{
if (voltage <= 4.5f)
{
transform.eulerAngles = new Vector3(x_position, 90, z_position);
}
else
{
transform.eulerAngles = new Vector3(x_position, 180, z_position);
}
}
}
}
Your answer
Follow this Question
Related Questions
Send many ints from Unity To Arduino. 2 Answers
parse serial data from arduino 1 Answer
Arduino serial Read 0 Answers
Problems With Sending Data through Serial Port 1 Answer
Long delay sending data to Arduino via Serial Communication 0 Answers