Advertisement
codewing

C# | Unity | Autodetection of serial GameControllers

Nov 21st, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.IO.Ports;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6.  
  7. class PlayerStream
  8. {
  9.     public int playerID;
  10.     public SerialPort stream;
  11.     public string recievedData = "EMPTY";
  12. }
  13.  
  14. public class ControllerDetection : MonoBehaviour {
  15.  
  16.     List<PlayerStream> streams = new List<PlayerStream>();
  17.  
  18.     // Use this for initialization
  19.     void Start () {
  20.         for (int i = 1; i < 10; i++)
  21.         {
  22.             SerialPort stream = new SerialPort("COM"+i, 115200);
  23.             try
  24.             {
  25.                 stream.Open();
  26.  
  27.                 Debug.Log("COM" + i + " is open.");
  28.                 PlayerStream ps = new PlayerStream();
  29.                 ps.playerID = i;
  30.                 ps.stream = stream;
  31.                 streams.Add(ps);
  32.             }
  33.             catch
  34.             {
  35.                 Debug.Log("COM" + i + " is not open.");
  36.             }
  37.         }
  38.  
  39.         foreach (PlayerStream ps in streams)
  40.         {
  41.             Debug.Log("Got player "+ps.playerID);
  42.         }
  43.     }
  44.    
  45.     // Update is called once per frame
  46.     void Update () {
  47.    
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement