Home > other >  About the unity of serial communication problems
About the unity of serial communication problems

Time:10-08

I want to do is via a serial port to send data to the arduino unity but don't know where is the data unity has not just run to the read data
Below is a program on the arduino;
 
Const int sensorPin=0;
Int t;

Void setup ()
{
Serial. The begin (19200);
PinMode (sensorPin, INPUT);

}

Void loop () {
T=analogRead (sensorPin)/4;
If (t<10)
{
Serial. Write (1);
Serial. Println (1);
Serial. Flush ();
delay(500); }
If (t>=10)
{
Serial. Write (2);
Serial. Println (2);
Serial. Flush ();
delay(500); }}


The unity of the program
 using System. The Collections; 
Using System. Collections. Generic;
Using UnityEngine;
Using System. IO. Ports;
Using UnityEngine. UI;

Public class change: MonoBehaviour {
Public GameObject lightA;
//Use this for initialization
SerialPort sp=new SerialPort (" COM4 ", 19200);

Void the Start () {
Sp. The Open ();
Sp. ReadTimeout=1;


}

//Update is called once per frame
Void the Update () {
If (sp. IsOpen) {

Try {
LightA. GetComponent (a). The text="test";//this is my used to test the sentences on the ReadByte (before) will run on the back will not, ask the great spirit guide
Changelight (sp. ReadByte ());

Print (sp. ReadByte ());

}
The catch (System. Exception) {

}
}
}
Void changelight (int state)
{if (state==1)
{
LightA. GetComponent (a). The text="0";
}
If (state==2) {
LightA. GetComponent (a). The text="1";
}
}
}
  • Related