unity3d串口延迟更新问题

时间:2016-05-03 11:49:33

标签: unity3d serial-port delay

我需要通过串口发送一些东西并继续发送这些值大约10秒,发送10秒后不同的值等等等等。

下面是我的代码到目前为止它只需要延迟

的帮助
    using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using System.Threading;
using System.ComponentModel;
using System;


public class Mindwave : MonoBehaviour
{
    TGCConnectionController controller;


    private SerialPort sp = new SerialPort("COM9", 9600);
    string strIn;


    public Texture2D[] signalIcons;

    private int indexSignalIcons = 1;

    private int poorSignal1;
    private int attention1;
    private int meditation1;

    private float delta;




        // Use this for initialization
        void Start ()
    {
        controller = GameObject.Find("NeuroSkyTGCController").GetComponent<TGCConnectionController>();

        controller.UpdatePoorSignalEvent += OnUpdatePoorSignal;
        controller.UpdateAttentionEvent += OnUpdateAttention;
        controller.UpdateMeditationEvent += OnUpdateMeditation;

        controller.UpdateDeltaEvent += OnUpdateDelta;

        sp.Open();
        sp.ReadTimeout = 50;


    }


    // Update is called once per frame
    void Update ()
    {
        print ("Attention1:" + attention1);

        if (sp.IsOpen)
        {
            try
            {
                //Read incoming data
                //strIn = sp.ReadLine();
                //print(strIn);
                if (attention1 < 50)
                {
                    sp.Write("4");
                }else if (attention1 > 60)
                {
                    // sp.Write("3");

                    Invoke("DoSomething", 10);//this will happen after 2 seconds
                    sp.Write("1");
                    Invoke("DoSomething", 10);//this will happen after 2 seconds
                    yield WaitForSeconds(0.3);
                }


            }

            catch (System.Exception)
            {


            }


        }



    }


    void OnUpdatePoorSignal(int value){
        poorSignal1 = value;
        if(value < 25){
            indexSignalIcons = 0;
        }else if(value >= 25 && value < 51){
            indexSignalIcons = 4;
        }else if(value >= 51 && value < 78){
            indexSignalIcons = 3;
        }else if(value >= 78 && value < 107){
            indexSignalIcons = 2;
        }else if(value >= 107){
            indexSignalIcons = 1;
        }
    }
    void OnUpdateAttention(int value){
        attention1 = value;
    }
    void OnUpdateMeditation(int value){
        meditation1 = value;
    }
    void OnUpdateDelta(float value){
        delta = value;
    }


    void OnGUI()
    {
        GUILayout.BeginHorizontal();


        if (GUILayout.Button("Connect"))
        {
            controller.Connect();
        }
        if (GUILayout.Button("DisConnect"))
        {
            controller.Disconnect();
            indexSignalIcons = 1;
        }

        GUILayout.Space(Screen.width-250);
        //GUILayout.Label(signalIcons[indexSignalIcons]);

        GUILayout.EndHorizontal();


        GUILayout.Label("PoorSignal1:" + poorSignal1);
        GUILayout.Label("Attention1:" + attention1);
        GUILayout.Label("Meditation1:" + meditation1);
        GUILayout.Label("Delta:" + delta);


    }

    public void StartClicked()
    {
        sp.Write("5");
    }
}

上面的代码是我的代码到目前为止,下面是我要添加延迟的部分

if (attention1 < 50)
            {
                sp.Write("4");
            }else if (attention1 > 60)
            {
                // sp.Write("3");

                Invoke("DoSomething", 10);//this will happen after 2 seconds
                sp.Write("1");
                Invoke("DoSomething", 10);//this will happen after 2 seconds
                yield WaitForSeconds(0.3);
            }

你可以看到我想发送3秒10秒然后发送1秒10秒依旧

如果有人能提供帮助,那将非常感谢你

0 个答案:

没有答案