Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO.Ports;
  5. using UnityEngine.UI;
  6. using System.Threading;
  7. using System.Globalization;
  8.  
  9. public class NewBehaviourScript : MonoBehaviour
  10. {
  11.  
  12.     Text text;
  13.     // Use this for initialization
  14.     SerialPort port;
  15.     string message;
  16.     Thread readThread;
  17.     bool m_continue;
  18.  
  19.     void Start()
  20.     {
  21.         m_continue = true;
  22.         text = GetComponent<Text>();
  23.        
  24.         port = new SerialPort("COM5", 9600);
  25.         port.Open();
  26.         readThread = new Thread(Read);
  27.         readThread.Start();
  28.     }
  29.  
  30.     // Update is called once per frame
  31.     void Update()
  32.     {
  33.         if (message != "")
  34.         {
  35.             text.text = message;
  36.         }
  37.         else
  38.         {
  39.             text.text = "pusto";
  40.         }
  41.    
  42.     }
  43.  
  44.     void Read()
  45.     {
  46.         while(m_continue)
  47.         {
  48.            
  49.             message = port.ReadTo("q");
  50.             var axes = message.Split(' ');
  51.             string axis_x_string = axes[0];
  52.             string axis_y_string = axes[1];
  53.             string axis_z_string = axes[2];
  54.  
  55.             var axis_x = float.Parse(axis_x_string, CultureInfo.InvariantCulture.NumberFormat);
  56.             var axis_y = float.Parse(axis_y_string, CultureInfo.InvariantCulture.NumberFormat);
  57.             var axis_z = float.Parse(axis_z_string, CultureInfo.InvariantCulture.NumberFormat);
  58.  
  59.         }
  60.     }
  61.  
  62.     void OnDestroy()
  63.     {
  64.         m_continue = false;
  65.         port.Close();
  66.         port.Dispose();
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement