Hey, I have a question about COM’s in unity.
I’m working with Arduino and my project needs a connection with multiple sensors identifying and setting a function for each one
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
using System.IO;
using System.IO.Ports;
public class Braçodireito : MonoBehaviour
{
public static string Com;
public static GameObject acelerometro0;
public GameObject acelerometro;
readonly SerialPort sensor1 = new SerialPort(Com, 115200);
public Vector3 rotação1;
public float smooth = 0f;
public int a = 0;
public int b = 0;
public GameObject retangulo;
float x = 0;
float y = 0;
float z = 0;
float xca = 0;
float yca = 0;
float zca = 0;
public void sampleFunction()
{
while (true)
{
// execute operation
// for example:
string data = sensor1.ReadLine();
}
}
void Start()
{
sensor1.Open();
sensor1.ReadTimeout = 100;
Thread sampleThread = new Thread(new ThreadStart(sampleFunction));
sampleThread.IsBackground = true;
sampleThread.Start();
if (acelerometro0.name == "Acelerometro1")
{
Com = "COM1";
}
else if (acelerometro0.name == "Acelerometro2")
{
Com = "COM2";
}
else if (acelerometro0.name == "Acelerometro3")
{
Com = "COM3";
}
else if (acelerometro0.name == "Acelerometro4")
{
Com = "COM4";
}
else if (acelerometro0.name == "Acelerometro5")
{
Com = "COM5";
}
else if (acelerometro0.name == "Acelerometro6")
{
Com = "COM6";
}
else if (acelerometro0.name == "Acelerometro7")
{
Com = "COM7";
}
else if (acelerometro0.name == "Acelerometro8")
{
Com = "COM8";
}
else if (acelerometro0.name == "Acelerometro9")
{
Com = "COM9";
}
else if (acelerometro0.name == "Acelerometro10")
{
Com = "COM10";
}
acelerometro0 = acelerometro;
}
void Update()
{
if (sensor1.IsOpen)
{
sensor1.WriteLine("a");
string value = sensor1.ReadLine();
string[] vec3 = value.Split(',');
float xraw = ((float.Parse(vec3[0])) / 100);
float yraw = ((float.Parse(vec3[1])) / 100);
float zraw = ((float.Parse(vec3[2])) / 100);
x = (xraw) - xca;
y = (yraw) - yca;
z = (zraw) - zca;
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(x, z, y), smooth);
if (a == 1)
{
sensor1.Close();
}
}
if (sensor1.IsOpen == false)
{
sensor1.Open();
}
}
public void Calibrar()
{
sensor1.WriteLine("a");
string value = sensor1.ReadLine();
string[] vec3 = value.Split(',');
float xraw = ((float.Parse(vec3[0])) / 100);
float yraw = ((float.Parse(vec3[1])) / 100);
float zraw = ((float.Parse(vec3[2])) / 100);
xca = (xraw)+180;
yca = (yraw);
zca = (zraw);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class trash4 : MonoBehaviour
{
public Image imagem;
public Text texto;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionStay2D(Collision2D col)
{
if (col.collider.gameObject.tag == "items")
{
col.collider.GetComponent<items>().collision = true;
if (!col.collider.GetComponent<items>().dragging)
{
col.collider.gameObject.transform.position = transform.position;
imagem.color = Color.green;
texto.text = "Antebraço Direito: OK " + col.collider.gameObject.name;
Braçodireito.acelerometro0 = col.collider.gameObject;
Destroy(col.collider.gameObject);
}
}
}
private void OnCollisionExit2D(Collision2D col)
{
col.collider.GetComponent<items>().collision = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class trash4 : MonoBehaviour
{
public Image imagem;
public Text texto;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionStay2D(Collision2D col)
{
if (col.collider.gameObject.tag == "items")
{
col.collider.GetComponent<items>().collision = true;
if (!col.collider.GetComponent<items>().dragging)
{
col.collider.gameObject.transform.position = transform.position;
imagem.color = Color.green;
texto.text = "Antebraço Direito: OK " + col.collider.gameObject.name;
Braçodireito.acelerometro0 = col.collider.gameObject;
Destroy(col.collider.gameObject);
}
}
}
private void OnCollisionExit2D(Collision2D col)
{
col.collider.GetComponent<items>().collision = false;
}
}
Please help, I really don’t know how to make this work