- Home /
Unity arduino connection
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System;
using System.Threading;
public class Connection : MonoBehaviour {
public static SerialPort sp = new SerialPort("COM5",9600);
private String strIn;
private bool result;
private int val;
public Flying x;
Thread thread;
void Start ()
{
//Cursor.visible = false;
sp.Open ();//opens port
sp.BaseStream.ReadTimeout = 1;//wait time for the input
thread = new Thread (ardConn);
thread.Start ();
//sp.ReadTimeout=2000;
}
//void fixedUpdate ()
void Update(){
//delay();
}
void ardConn(){
while (true) {
if (!sp.IsOpen) {
sp.Open ();
//try {
//delay ();
strIn = sp.ReadLine ();//Get the value from port
int number;
result = Int32.TryParse (strIn, out number);//Try to convert it to int
if (result) {
if (!x.jmp1) {
val = Int32.Parse (strIn);//convert it to int
Debug.Log (val);
if (val > 830) {
Debug.Log ("if");
x.jmp1 = true;
//delay ();
}
}
}
//} catch (TimeoutException e) {
// x.rg.AddForce (0f, 10f, 0f);
//}
} else {
sp.Close ();//close the port
}
}
}
void OnApplicationQuit(){
sp.Close ();
}
}
I'm trying to get data that comes rapidly via USB port from pressure sensor but it reads one or two times than stops reading
Comment