-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadData.cs
73 lines (63 loc) · 1.79 KB
/
ReadData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
///////////////////////////////////////////////////////
// STM32 Pong Project - Léo Séry && Matthias Flament
// ####
// Script to retrieve data via the Serial port and convert them into decimal values.
// script by Léo Séry - 17/03/2022
// ####
///////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class ReadData : MonoBehaviour
{
SerialPort stream = new SerialPort("COM7", 115200);
private string inputData;
private string inputX;
private string inputX2;
public enum PlayerState {Up, Static, Down,}
public string Player1_State;
public string Player2_State;
void Start()
{
stream.Open();
}
void Update()
{
inputData = stream.ReadLine();
stream.BaseStream.Flush();
string[] data = inputData.Split('|');
if (data[0] != "" && data[2] != "")
{
inputX = data[0];
inputX2 = data[2];
}
int xValue = int.Parse(inputX, System.Globalization.NumberStyles.HexNumber);
int x2Value = int.Parse(inputX2, System.Globalization.NumberStyles.HexNumber);
//Player 1
if (xValue < 128)
{
Player1_State = PlayerState.Down.ToString();
}
else if (xValue > 128)
{
Player1_State = PlayerState.Up.ToString();
}
else
{
Player1_State = PlayerState.Static.ToString();
}
//Player 2
if (x2Value < 128)
{
Player2_State = PlayerState.Down.ToString();
}
else if (x2Value > 128)
{
Player1_State = PlayerState.Up.ToString();
}
else
{
Player1_State = PlayerState.Static.ToString();
}
}
}