首页 > 解决方案 > 如何与传入消息进行字符串比较

问题描述

我知道这听起来很简单,但我遇到了一些麻烦。我正在尝试使用 pic 微控制器 (MCU) 和 xamarin android 应用程序制作系统。从应用程序到 pic MCU 的发送部分已解决,但是当我想将数据从 MCU 发送到应用程序时,它不会完美无缺。我使用 HC-06 作为蓝牙设备来接收和发送消息。

从 MCU 接收到应用程序的代码是:

public void beginListenForData()
    {
        try
         {
             inStream = btSocket.InputStream;
         }
         catch (IOException ex)
         {
             Console.WriteLine(ex.Message);
         }
         Task.Factory.StartNew(() => {
             byte[] buffer = new byte[1024];
             int bytes;
             while (true)
             {
                 try
                 {
                     Array.Reverse(buffer, 0, buffer.Length);
                     bytes = inStream.Read(buffer, 0, buffer.Length);
                     if (bytes > 0)
                     {
                         string valor = Encoding.ASCII.GetString(buffer);
                         System.Diagnostics.Debug.WriteLine(buffer);
                         System.Diagnostics.Debug.WriteLine(bytes);
                         System.Diagnostics.Debug.WriteLine(valor);
                         if (valor == "D0O")
                         {
                             System.Diagnostics.Debug.WriteLine("Vergelijking gelukt!");
                             break;
                         }
                         //Result.Text = Result.Text + "\n" + valor;
                     }
                 }
                 catch (Java.IO.IOException)
                 {
                     //Result.Text = string.Empty;
                     break;
                 }
             }
         });
    }

您可能会猜到我尝试从 MCU 发送的消息是 D0O (valor),当比较与传入消息一起工作时,我想调试写入成功:

System.Diagnostics.Debug.WriteLine("Vergelijking gelukt!");

下一部分是检查数据的输入:

System.Diagnostics.Debug.WriteLine(buffer);
System.Diagnostics.Debug.WriteLine(bytes);
System.Diagnostics.Debug.WriteLine(valor);

我注意到的是奇怪的输出(见图):

在此处输入图像描述

如您所见,消息每次都被分成两部分。有谁知道为什么以及如何解决它?

我确实改变了数组顺序:

Array.Reverse(buffer, 0, buffer.Length);

因为我确实注意到它输入的顺序错误。这确实使它按正确的顺序排列。

小更新:

我更改了一些代码行,它的工作更加“完美”

while ((count = inStream.Read(buffer, 0, buffer.Length)) > 0)

但是奇怪的是第一位与接收字符串的其余部分分开。如果有人有想法,我不确定是什么导致了这个问题?

提前致谢。

标签: c#androidxamarinbluetooth

解决方案


我找到了解决我面临的问题的方法。因此,我将分享我的答案和思考过程,以便其他人可以使用相同的方法。

所以我认为有一个接收缓冲区可以保存传入的字符。如果使用 streamReader.Read 读取缓冲区,它会返回读取的字符的整数。所以我制作了数据类型字符串 [] 的第二个缓冲区。

如果字符串 [0] 为空,我会将其放入 streamReader.Read 读取的第一个字符中。如果字符串 [0] 不为空,则表示第一个字符已被读取,因此我将传入的字符放入字符串 [1]。这意味着被拆分的消息现在分为 string[0] 和 string[1]。那么如果我可以将它组合起来并将其保存到一个字符串变量中呢?这是由:完成的string eindtekst = string.Join("", buf);,这给了我一个字符串,所以我可以比较它。在完成比较时清除两个数组很重要,否则会添加新数据。你也许可以告诉 string[0] == null 永远不会是真的。因此,只有 string[1] 始终被覆盖,这意味着您正在丢失数据。

public void beginListenForData()
    {
        try
        {
            inStream = btSocket.InputStream;
            streamReader = new StreamReader(inStream);
        }
        catch (IOException ex)
        {
            Console.WriteLine(ex.Message);
        }
        char[] buffer = new char[256];
        string[] buf = new string[2];
        int bytes;
        while (1)
        {
            try
            {
                if ((bytes = streamReader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    string tekst = new string(buffer, 0, bytes);
                    if(buf[0] == null)
                    {
                        buf[0] = tekst;
                    }
                    else
                    {
                        buf[1] = tekst; 
                    }
                    string eindtekst = string.Join("", buf);

                    if (eindtekst == "D0O")
                    {
                        System.Diagnostics.Debug.WriteLine("Vergelijking gelukt!");
                        System.Diagnostics.Debug.WriteLine(eindtekst);
                        Array.Clear(buffer, 0, buffer.Length);
                        Array.Clear(buf, 0, buf.Length);
                        writeData("D2O");
                    }

                    streamReader.DiscardBufferedData();

                }
            }
            catch (Java.IO.IOException)
            {
                break;
            }
        }
    }

感谢所有的帮助


推荐阅读