示例#1
0
        /// <summary>
        /// 非同期受信を開始して、結果を返却します。
        /// </summary>
        public async Task <string> ReadAsync()
        {
            Program.Info("1");

            await Semaphore.WaitAsync().ConfigureAwait(false);

            try
            {
                Program.Info("2");

                await ConnectAsync();

                using (var buffer = new MemoryStream())
                {
                    var bytes     = new byte[2048];
                    var size      = 0;
                    var endstring = EndString.Left(EndString.Length - 1);

                    Program.Info("3");

                    // 読取データがある間繰り返す。
                    while ((size = COM.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        await buffer.WriteAsync(bytes, 0, size);

                        if (buffer.Length < endstring.Length)
                        {
                            // 終端文字の数だけ読み取っていない場合は次ループへ
                            continue;
                        }

                        // 終端文字の数だけシーク位置を戻す。
                        buffer.Seek(endstring.Length * -1, SeekOrigin.End);

                        if (endstring.All(c => c == buffer.ReadByte()))
                        {
                            // 読み取った最後の文字がEndStringと同値なら終了
                            break;
                        }
                        else
                        {
                            // シーク位置を戻す。
                            buffer.Seek(0, SeekOrigin.End);
                        }
                    }

                    Program.Info("4");

                    // メッセージ取得
                    var message = buffer.ToArray().ToString(Encoding).ToUpper();

                    // 終端文字を除外して返却
                    return(message.Left(message.Length - endstring.Length));
                }
            }
            finally
            {
                Semaphore.Release();
            }
        }
        /// <summary>
        /// 非同期受信を開始して、結果を返却します。
        /// </summary>
        public string ReadAsync()
        {
            try
            {
                if (Status != ConnectionStatuses.Connect)
                {
                    return(string.Empty);
                }

                using (var buffer = new MemoryStream())
                {
                    var bytes     = new byte[2048];
                    var size      = 0;
                    var endstring = EndString.Left(EndString.Length - 1);

                    // 読取データがある間繰り返す。
                    while ((size = COM.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        buffer.Write(bytes, 0, size);

                        if (buffer.Length < endstring.Length)
                        {
                            // 終端文字の数だけ読み取っていない場合は次ループへ
                            continue;
                        }

                        // 終端文字の数だけシーク位置を戻す。
                        buffer.Seek(endstring.Length * -1, SeekOrigin.End);

                        if (buffer.ToArray().ToString(Encoding).Contains(EndString))
                        {
                            // 読み取った最後の文字がEndStringと同値なら終了
                            break;
                        }
                        else
                        {
                            // シーク位置を戻す。
                            buffer.Seek(0, SeekOrigin.End);
                        }
                    }

                    // メッセージ取得
                    var message = buffer.ToArray().ToString(Encoding).ToUpper();

                    Console.WriteLine("Read終了");

                    // 終端文字を除外して返却
                    return(message.Left(message.Length - endstring.Length));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());

                // 例外発生時は切断
                DisConnect();

                // 空文字を返却
                return(string.Empty);
            }
        }