Exemplo n.º 1
0
    public void Port_Connect()
    {
        if (!My_SerialPort.IsOpen)
        {
            if (MainManerger.newestPort.Length > 2)
            {
                try
                {
                    My_SerialPort = new SerialPort();

                    //設定 Serial Port 參數
                    My_SerialPort.PortName    = MainManerger.newestPort;
                    My_SerialPort.BaudRate    = baudRate;
                    My_SerialPort.ReadTimeout = 20;

                    My_SerialPort.Open();

                    if (My_SerialPort.IsOpen)
                    {
                        MainManerger.DevReady(MainManerger.newestPort);
                        isInitial = true;
                        this.CancelInvoke();
                        this.InvokeRepeating("ReadSerialBytes", 0.1f, 0.0005f);
                    }
                }
                catch
                {
                    //Debug.Log("Console Connect Faild");
                    this.Invoke("Port_Connect", 0.2f);
                    return;
                }
            }
            else
            {
                this.Invoke("Port_Connect", 0.2f);
            }
        }
    }