コード例 #1
0
        /// <summary>
        /// 受信時の処理
        /// </summary>
        /// <param name="receivedString"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void OnReceivedFromPreMaidAi(string receivedString)
        {
            //4文字以下なら不正
            if (receivedString.Length < 4)
            {
                return;
            }
            //3-4文字目が命令種類
            string orderKind = receivedString.Substring(2, 2);

            //Debug.Log("orderKind:"+ orderKind);
            switch (orderKind)
            {
            //バッテリー残量
            case "01":
                if (receivedString.Length >= 10)
                {
                    int rawValtageValue =
                        PreMaidUtility.HexStringToInt(PreMaidUtility.ConvertEndian(receivedString.Substring(6, 4)));
                    Debug.Log($"バッテリー残量{rawValtageValue} で電圧は{rawValtageValue / 216f} V");

                    if (rawValtageValue / 216.0f < 9f)
                    {
                        Debug.LogError("バッテリー残量が9V以下です!!!!");
                    }
                }

                break;

            //モーション転送結果
            case "18":
                if (receivedString == "0418001C")
                {
                }
                else
                {
                    Debug.Log("PoseError:" + receivedString);
                }

                break;

            default:
                Debug.Log(receivedString);
                break;
            }
        }
コード例 #2
0
        // Update is called once per frame
        void Update()
        {
            if (errorQueue.IsEmpty == false)
            {
                var errorString = string.Empty;
                if (errorQueue.TryDequeue(out errorString))
                {
                    Debug.LogError(errorString);
                }
            }

            if (SerialPortOpen == false)
            {
                return;
            }

            //受信バッファ、バイナリで届くので区切りをどうしようか悩み中
            //一旦、素朴に先頭に命令長が来るでしょう、というつもりで書きます。
            if (receivedQueue.IsEmpty == false)
            {
                var receivedString = string.Empty;
                if (receivedQueue.TryDequeue(out receivedString))
                {
                    bufferedString += receivedString;

                    if (bufferedString.Length < 2)
                    {
                        return;
                    }

                    //異様にバッファが溜まったら捨てる
                    if (bufferedString.Length > 100)
                    {
                        Debug.Log("破棄します:" + bufferedString);
                        bufferedString = string.Empty;
                        return;
                    }

                    int orderLength = PreMaidUtility.HexStringToInt(bufferedString.Substring(0, 2));

                    //先頭0だったら命令ではないと判断して2文字読み捨て
                    //なぜなら0004051Fみたいな文字列が入っているので
                    if (orderLength == 0)
                    {
                        bufferedString = bufferedString.Substring(2);
                    }
                    //命令長が足りないので待つ
                    else if (orderLength > bufferedString.Length * 2)
                    {
                        return;
                    }
                    else if (bufferedString.Length >= orderLength * 2)
                    {
                        var targetOrder = bufferedString.Substring(0, orderLength * 2);
                        if (OnReceivedFromPreMaidAI != null)
                        {
                            OnReceivedFromPreMaidAI.Invoke(targetOrder);
                        }
                        else
                        {
                            Debug.Log(targetOrder);
                        }

                        //まだ余りバッファが有るならツメます
                        if (orderLength * 2 < bufferedString.Length)
                        {
                            bufferedString = bufferedString.Substring(orderLength * 2 + 1);
                        }
                        else
                        {
                            bufferedString = string.Empty;
                        }
                    }
                }
            }
        }