예제 #1
0
        /// <summary>
        /// シリアルポート読み取り書き込みスレッド
        /// 読みと書きを別スレッドにするより、まとめて1スレッドにしたほうがシリアルポートのlockが走らない分だけ早い
        /// </summary>
        private void ReadAndWriteThreadFunc()
        {
            Debug.LogWarning("シリアルポート送信スレッド起動");

            var readBuffer = new byte[256 * 3];
            var readCount  = 0;

            while (SerialPortOpen && _serialPort != null && _serialPort.IsOpen)
            {
                //PCから送る予定のキューが入っているかチェック
                if (sendingQueue.IsEmpty == false)
                {
                    var willSendString = string.Empty;
                    if (sendingQueue.TryDequeue(out willSendString))
                    {
                        byte[] willSendBytes =
                            PreMaidUtility.BuildByteDataFromStringOrder(willSendString);

                        _serialPort.Write(willSendBytes, 0, willSendBytes.Length);
                    }
                }


                //プリメイドAIからの受信チェック
                try
                {
                    readCount = _serialPort.Read(readBuffer, 0, readBuffer.Length);

                    if (readCount > 0)
                    {
                        receivedQueue.Enqueue(PreMaidUtility.DumpBytesToHexString(readBuffer, readCount));
                    }
                }
                catch (TimeoutException tEx)
                {
                    //errorQueue.Enqueue("TimeOut Exception:" + tEx.Message);
                    //Thread.Sleep(1);
                    continue;
                }
                catch (System.Exception e)
                {
                    errorQueue.Enqueue(e.Message);
                    //Debug.LogWarning(e.Message);
                }

                Thread.Sleep(1);
            }

            Debug.LogWarning("exit thread");
        }
        /// <summary>
        /// シリアルポート読み取り書き込みスレッド
        /// 読みと書きを別スレッドにするより、まとめて1スレッドにしたほうがシリアルポートのlockが走らない分だけ早い
        /// </summary>
        private void ReadAndWriteThreadFunc()
        {
            Debug.LogWarning("シリアルポート送信スレッド起動");

            var readBuffer   = new byte[256 * 3];
            var readCount    = 0;
            var sendingCache = string.Empty; //送信失敗時に連続送信する

            //バースト転送モード
            bool burstMode = false;

            while (SerialPortOpen && _serialPort != null && _serialPort.IsOpen)
            {
                //PCから送る予定のキューが入っているかチェック
                if (sendingQueue.IsEmpty == false)
                {
                    var willSendString = string.Empty;
                    if (sendingQueue.TryDequeue(out willSendString))
                    {
                        if (burstMode)
                        {
                            burstMode = false;
                        }

                        sendingCache = willSendString;
                        byte[] willSendBytes =
                            PreMaidUtility.BuildByteDataFromStringOrder(willSendString);

                        _serialPort.Write(willSendBytes, 0, willSendBytes.Length);
                    }
                }


                //プリメイドAIからの受信チェック
                try
                {
                    //本当はここのカウントもバッファ溜めつつ見た方が良い…
                    readCount = _serialPort.Read(readBuffer, 0, readBuffer.Length);

                    if (readCount > 0)
                    {
                        var receivedString = PreMaidUtility.DumpBytesToHexString(readBuffer, readCount);
                        //ポーズ送信失敗したらバーストモードに入る
                        if (receivedString.IndexOf("180814") >= 0)
                        {
                            burstMode = true;
                        }

                        if (receivedString.IndexOf("18001C") >= 0 && burstMode == true)
                        {
                            burstMode = false;
                        }

                        receivedQueue.Enqueue(receivedString);
                    }
                }
                //UnityのSerialPortの実装がタコなのでここでTimeout例外を握りつぶす必要があります
                catch (TimeoutException tEx)
                {
                    continue;
                }
                catch (System.Exception e)
                {
                    errorQueue.Enqueue(e.Message);
                    //Debug.LogWarning(e.Message);
                }

                Thread.Sleep(1);
                //送信失敗してた場合、無理矢理にキャッシュしてた最後のポーズ命令を連続送信する
                //これで遅延を最小限にする
                if (burstMode)
                {
                    Thread.Sleep(5);
                    byte[] willSendBytes =
                        PreMaidUtility.BuildByteDataFromStringOrder(sendingCache);

                    _serialPort.Write(willSendBytes, 0, willSendBytes.Length);
                }
            }

            Debug.LogWarning("exit thread");
        }