-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serial_filter.cs
76 lines (59 loc) · 1.72 KB
/
Serial_filter.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
74
75
76
using UnityEngine;
using System.Collections;
using System;
using System.IO.Ports;
public class Serial_filter : MonoBehaviour
{
SerialPort stream;
public GameObject target;
float gx , gy, gz = 0;
//float speedFactor = 7.0f;
string port = "COM3";
int baudrate = 9600;
int readTimeout = 25;
void Start()
{
stream = new SerialPort(port, baudrate);
try
{
stream.ReadTimeout = readTimeout;
}
catch (System.IO.IOException ioe)
{
Debug.Log("IOException: " + ioe.Message);
}
stream.Open();
}
void Update()
{
string dataString = "null received";
if (stream.IsOpen)
{
try
{
dataString = stream.ReadLine();
}
catch (System.IO.IOException ioe)
{
Debug.Log("IOException: " + ioe.Message);
}
}
else
dataString = "NOT OPEN";
Debug.Log("RCV_ : " + dataString);
if (!dataString.Equals("NOT OPEN"))
{
char splitChar = ';' ;
string[] dataRaw = dataString.Split(splitChar);
Debug.Log(dataRaw[0]+";"+ dataRaw[1] + ";" + dataRaw[2] + ";");
// normalized accelerometer values
gx = float.Parse(dataRaw[0]);
gy = float.Parse(dataRaw[1]);
gz = float.Parse(dataRaw[2]);
}
//target.transform.rotation = Quaternion.Euler(gx * factor, gy * factor, gz * factor);
target.transform.eulerAngles = new Vector3(gx, gy,gz);
}
}
//42.522 //-1.274 //-1.715
//67 // 42 //96