Tanoda
SampleUserPolling_ReadWrite.cs
Go to the documentation of this file.
1
9using UnityEngine;
10
14public class SampleUserPolling_ReadWrite : MonoBehaviour
15{
16#if !UNITY_WEBGL
18
19 // Initialization
20 void Start()
21 {
22 serialController = GameObject.Find("SerialController").GetComponent<SerialController>();
23
24 Debug.Log("Press A or Z to execute some actions");
25 }
26
27 // Executed each frame
28 void Update()
29 {
30 //---------------------------------------------------------------------
31 // Send data
32 //---------------------------------------------------------------------
33
34 // If you press one of these keys send it to the serial device. A
35 // sample serial device that accepts this input is given in the README.
36 if (Input.GetKeyDown(KeyCode.A))
37 {
38 Debug.Log("Sending A");
40 }
41
42 if (Input.GetKeyDown(KeyCode.Z))
43 {
44 Debug.Log("Sending Z");
46 }
47
48
49 //---------------------------------------------------------------------
50 // Receive data
51 //---------------------------------------------------------------------
52
53 var message = serialController.ReadSerialMessage();
54
55 if (message == null)
56 return;
57
58 // Check if the message is plain data or a connect/disconnect event.
59 if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED))
60 Debug.Log("Connection established");
61 else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED))
62 Debug.Log("Connection attempt failed or disconnection detected");
63 else
64 Debug.Log("Message arrived: " + message);
65 }
66#endif
67}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
const string SERIAL_DEVICE_CONNECTED
string ReadSerialMessage()
const string SERIAL_DEVICE_DISCONNECTED
void SendSerialMessage(string message)