如何将字符串与传入消息进行比较 [英] How to do a string compare with the incoming message

查看:76
本文介绍了如何将字符串与传入消息进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know it sounds simple but I got some trouble with it. I am trying to make a system with a pic Microcontroller (MCU) and an xamarin android app. The sending part from app to the pic MCU is solved but when I want to send data from the MCU to the app it won't go as flaweless. I am using a HC-06 as a bluetooth device for receiving and sending messages.

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

The code for receiving from the MCU to the app is:

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(勇气)).

As you perhaps could geuss the message I try to sent from the MCU is D0O (valor) when the comparison worked with the incoming message I want to debug write that is was successful with:

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

下一部分用于检查要输入什么数据:

The next part is for checking what for data is coming in:

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

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

What I noticed is the strange output (see image):

如您所见,每次都会将消息分为两部分.有谁知道为什么以及如何解决这个问题?

As you can see the message is cut into 2 parts every time. Does anyone has any idea why and how to solve it?

我确实使用以下命令更改了数组顺序:

I did change the array order with:

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

因为我确实注意到它以错误的顺序输入.确实可以按正确的顺序进行操作.

Because I did notice it entered in the wrong order. This did work to put it in the right order.

小更新:

我更改了一些代码行,它的工作方式更加无精打采"

I changed some line of code and it works more "flaweless"

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

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

But what is strange that the first bit gets sepparated from the rest of the receiving string. I am not sure what causes this problem if anyone has a idea?

谢谢.

推荐答案

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

I found a solution for the problem I was facing. So I will share my answer and thought process so perhaps other people can use the same.

所以我以为是有一个接收缓冲区来保存传入的char的.如果使用streamReader.Read读取缓冲区,它将返回读取的char的整数.因此,我将数据类型为string []的第二个缓冲区.

So what I thought was there is a receiving buffer that saves the incoming char's. If the buffer is read with streamReader.Read it returns an integer of the readed char's. So I made second buffer of the datatype string[].

如果字符串[0]为空,我将放置在streamReader.Read读取的第一个Char中.如果string [0]不为空,则意味着第一个字符已被读取,因此我将传入的char放入string [1].这意味着被拆分的消息现在分为字符串[0]和字符串[1].那么,如果我可以将其组合并保存到字符串变量中,该怎么办?这是通过:string eindtekst = string.Join("", buf);完成的,这给了我一个完整的字符串,因此我可以对其进行比较.在完成比较后,清除两个数组很重要,否则将添加新数据.也许您可以说出string [0] == null永远不会是真的.因此,只有string [1]会被覆盖,这意味着您将丢失数据.

If the string[0] is empty I would place in my first Char that was read by the streamReader.Read. If the string[0] is NOT empty it means that the first char is already been read so I put the incoming char into string[1]. This means that the message that was split up is now into string[0] and string[1]. So what if I could combine it and save it into a string variable. This was done by: string eindtekst = string.Join("", buf); and this gives me the string in one piece so I can compare it. It is importent to clear the both array's as you're done with the comparing otherwise there would be new data added. And as you perhaps can tell string[0] == null would never be true. So only string[1] get's overridden al the time and that means you're losing out on data.

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;
            }
        }
    }

感谢所有帮助

这篇关于如何将字符串与传入消息进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆