How to do a string compare with the incoming message

跟風遠走 提交于 2020-02-16 06:33:10

问题


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.

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

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.

Little update:

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?

Thanks in advance.


回答1:


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.

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[].

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

Thanks for all the help



来源:https://stackoverflow.com/questions/60119308/how-to-do-a-string-compare-with-the-incoming-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!