Beispiel #1
0
    // ------------------------------------------------------------------------
    // Invoked whenever the SerialController gameobject is deactivated.
    // It stops and destroys the thread that was reading from the serial device.
    // ------------------------------------------------------------------------
    void OnDisable()
    {
        // If there is a user-defined tear-down function, execute it before
        // closing the underlying COM port.
        if (userDefinedTearDownFunction != null)
        {
            userDefinedTearDownFunction();
        }

        // The serialThread reference should never be null at this point,
        // unless an Exception happened in the OnEnable(), in which case I've
        // no idea what face Unity will make.
        if (serialThread != null)
        {
            serialThread.RequestStop();
            serialThread = null;
        }

        // This reference shouldn't be null at this point anyway.
        if (thread != null)
        {
            thread.Join();
            thread = null;
        }
    }
Beispiel #2
0
 // ------------------------------------------------------------------------
 // Invoked whenever the SerialController gameobject is activated.
 // It creates a new thread that tries to connect to the serial device
 // and start reading from it.
 // ------------------------------------------------------------------------
 void OnEnable()
 {
     serialThread = new SerialThreadLines(portName,
                                          baudRate,
                                          reconnectionDelay,
                                          maxUnreadMessages);
     thread = new Thread(new ThreadStart(serialThread.RunForever));
     thread.Start();
 }
    // ------------------------------------------------------------------------
    // Invoked whenever the SerialController gameobject is activated.
    // It creates a new thread that tries to connect to the serial device
    // and start reading from it.
    // ------------------------------------------------------------------------
    void OnEnable()
    {
        serialThread = new SerialThreadLines(portName,
                                             baudRate,
                                             reconnectionDelay,
                                             maxUnreadMessages);
        thread = new Thread(new ThreadStart(serialThread.RunForever));
        thread.Start();

        gameManager = GameObject.Find("GameManager").GetComponent <GameManager>();
    }
Beispiel #4
0
    public void StartListeningToPort(string portName)
    {
        serialThread = new SerialThreadLines(portName,
                                             baudRate,
                                             reconnectionDelay,
                                             maxUnreadMessages);
        thread = new Thread(new ThreadStart(serialThread.RunForever));
        thread.Start();

        exists = true;
    }
    void Update()
    {
        if (portName != "COM3")
        {
            if (firstTime)
            {
                serialThread = new SerialThreadLines(portName,
                                                     baudRate,
                                                     reconnectionDelay,
                                                     maxUnreadMessages);
                thread = new Thread(new ThreadStart(serialThread.RunForever));
                thread.Start();
                firstTime = false;
            }
            // If the user prefers to poll the messages instead of receiving them
            // via SendMessage, then the message listener should be null.
            if (messageListener == null)
            {
                return;
            }

            // Read the next message from the queue
            string message = (string)serialThread.ReadMessage();
            if (message == null)
            {
                return;
            }

            // Check if the message is plain data or a connect/disconnect event.
            if (ReferenceEquals(message, SERIAL_DEVICE_CONNECTED))
            {
                messageListener.SendMessage("OnConnectionEvent", true);
            }
            else if (ReferenceEquals(message, SERIAL_DEVICE_DISCONNECTED))
            {
                messageListener.SendMessage("OnConnectionEvent", false);
            }
            else
            {
                messageListener.SendMessage("OnMessageArrived", message);
            }
        }
    }